#P6813. 「MCOI-02」Glass 玻璃

「MCOI-02」Glass 玻璃

Description

First, a tree is given. Each node has ViV_i pieces of glass, and each edge has a weight WiW_i.

In each operation, Xiao S can choose two nodes u,v(uv)u, v (u \ne v). On the unique path from node uu to node vv, the edge sum is the sum of the weights of all edges on the path, i.e. Wi\sum W_i. The node sum is the sum of the numbers of glass on all nodes on the path (including u,vu, v), i.e. Vi\sum V_i. Xiao S can obtain a score equal to the product of the edge sum and the node sum, i.e. Wi×Vi\sum W_i \times \sum V_i.

Any two operations cannot be exactly the same, and (u,v)(u, v) and (v,u)(v, u) are considered two different operations.

However, sometimes the tree is too large, and Xiao S needs your help. He wants you to tell him: after N(N1)N(N-1) operations, how many points can be obtained in total. The result may be very large; you only need to output the answer modulo 998244353998244353.

Input Format

The first line contains an integer NN, denoting the number of nodes in the tree.
The second line contains NN integers, where the ii-th number ViV_i denotes the number of glass at node ii.
The next N1N-1 lines each contain three numbers x,y,zx, y, z, indicating that there is a tree edge between nodes xx and yy with weight zz.

Output Format

Output one integer, the total score modulo 998244353998244353.

5
1 2 1 2 1
1 5 1
1 2 2
2 3 1
2 4 2
256

Hint

Sample Explanation

For sample 11, the tree looks like this:

Constraints

This problem uses bundled testdata.

Subtask ID NN Vi,WiV_i, W_i Special Property Score Time Limit
1 200\le 200 <23\lt 2^3 33 1s\rm 1s
2 103\le 10^3 66
3 6×103\le 6 \times 10^3 <28\lt 2^8 1111
4 2×105\le 2 \times 10^5 There exists a node with degree N1N-1 1212
5 The tree is a chain 1313
6 2121
7 2×106\le 2 \times 10^6 3434 2s\rm 2s

For 100%100\% of the data, 0Vi,Wi<2160 \le V_i, W_i \lt 2^{16} and 1N2×1061 \le N \le 2 \times 10^6.

Notes

Minecraft OI Round 2 D

  • Maker: Inf_Voltage
  • Tester: tarjin

Translated by ChatGPT 5