#P7247. 教材运送

教材运送

Description

The teaching building of Modu High School is a tree with nn nodes. Node ii corresponds to classroom ii, with aia_i students. Each undirected edge ii is described as (ui,vi,bi)(u_i,v_i,b_i), meaning it connects classrooms uiu_i and viv_i, and the stairs between them have a vertical height difference of bi mb_i\ \text{m}.

At the beginning of the new semester, textbooks need to be distributed to students, but a failure in the teleportation system caused the textbooks to be scattered across different classrooms. When Tianyi is in classroom uu, she will randomly pick up ava_v textbooks belonging to classroom v (v[1,n])v~(v\in[1,n]), and deliver them to classroom vv along the unique simple path between classrooms uu and vv. Suppose the height differences of the stairs Tianyi walks through are b1,b2,,bm mb_1,b_2,\cdots,b_m\ \text{m}, and the weight of each textbook is 1N1\text{N}. Then the absolute value of the work done by Tianyi’s lifting force during this delivery (taking positive work for going up and the absolute value of negative work for going down) is defined as W=av(i=1mbi)JW=a_v\left( \sum_{i=1}^{m} b_i \right)\text{J}. In other words, the cost of one delivery is the product of the endpoint node weight and the sum of edge weights along the path.

Initially, Tianyi is in classroom 11, but she does not know the way, so Ayane will guide Tianyi to deliver textbooks such that Tianyi visits every classroom at least once. Before achieving this goal, what is the expected total absolute work done by Tianyi’s lifting force, in J\text{J}?

Since the answer may be a decimal, to avoid precision loss, output the value of the answer modulo 998244353998244353.


Simplified statement

Given an unrooted tree with nn nodes, with node weights and edge weights. Let the current position be ss (initially s=1s=1). Each time, randomly choose a target node tt among the nn nodes, pay a cost of (“sum of edge weights on the simple path from ss to tt”) ×\times (“node weight of tt”), mark node tt (marking can repeat), and set s=ts=t. Find the expected total cost when every node has been marked at least once (node 11 is marked from the start). Output the answer modulo 998244353998244353.

Input Format

The first line contains an integer nn, as described above.

The second line contains nn integers, representing the number of students in each classroom.

The next n1n-1 lines each contain three integers ui,vi,biu_i,v_i,b_i, meaning there is a staircase with vertical height difference bi mb_i\ \text{m} between classrooms uiu_i and viv_i.

Output Format

One line with one integer, representing the answer modulo 998244353998244353.

3
1 2 3
1 2 1
2 3 1
332748127
5
2 3 4 2 5
1 2 3
2 4 2
2 5 5
1 3 3
615584181
2
1 2
1 2 2
4

Hint


Constraints and notes

This problem uses bundled testdata.

For 100%100\% of the testdata, 1n1061\le n\le 10^6, 1ai,bi<9982443531\le a_i,b_i\lt 998244353, 1ui,vin1\le u_i,v_i\le n, and it is guaranteed that uiviu_i\ne v_i.

Subtask Points nn Special constraints
1 5 3 \le 3 /
2 10 13 \le 13
3 20 \le 20
4 25 5×103 \le 5\times 10^3
5 / For 1in\forall 1\le i\le n, ai=1a_i=1.
6 10 For every edge, vi=ui+1v_i=u_i+1, i.e. the tree is a chain.
7 35 /

Translated by ChatGPT 5