#878. SWW and Probability
SWW and Probability
Background
SWW is a master of Probability. One day, she thought of a problem in her dream and she wants to challenge you.
She gives you a tree with nodes, labeled from to . Each node of the tree has a positive integer weight . For any connected subgraph of , denoted as , you could collect the weights of all nodes in and calculate the variance of these weights.
Now, SWW wants you to calculate the sum of variances of all connected subgraphs of T. Since the answer may be very large, you should print your result modulo .
Recall that for a given set of numbers , the mean µx and variance is defined as
$$σ^2_x=\frac{(x_1 − µ_x)^2 + (x_2 − µ_x)^2 + · · · + (x_n − µ_x)^2}{n} $$Input
The first line contains an integer , representing the number of tree nodes.
The second line contains integers , representing the weights of each node.
Each line of the next lines contains two integers , representing an edge in the tree. It is guaranteed that the given graph is a tree.
Output
Output a single integer, representing the answer modulo .
Samples
3
1 2 3
1 2
2 3
166374060
Note
The tree in the example has six different connected subgraphs: .
Their variance is . So the answer is, i.e. mod .