Score : $500$ points
We have a weighted tree with $N$ vertices. The $i$-th edge connects Vertex $u_i$ and Vertex $v_i$ bidirectionally and has a weight $w_i$.
For a pair of vertices $(x,y)$, let us define $\text{dist}(x,y)$ as follows:
Find $\text{dist}(i,j)$ for every pair $(i,j)$ such that $1 \leq i \lt j \leq N$, and print the sum of those values modulo $(10^9+7)$.
The bitwise $\mathrm{XOR}$ of integers $A$ and $B$, $A\ \mathrm{XOR}\ B$, is defined as follows:
Input is given from Standard Input in the following format:
$N$ $u_1$ $v_1$ $w_1$ $u_2$ $v_2$ $w_2$ $\vdots$ $u_{N-1}$ $v_{N-1}$ $w_{N-1}$
Print the sum of $\text{dist}(i,j)$, modulo $(10^9+7)$.
3 1 2 1 1 3 3
6
We have $\text{dist}(1,2)=1,$ $\text{dist}(1,3)=3,$ and $\text{dist}(2,3)=2$, for the sum of $6$.
5 3 5 2 2 3 2 1 5 1 4 5 13
62
10 5 7 459221860242673109 6 8 248001948488076933 3 5 371922579800289138 2 5 773108338386747788 6 10 181747352791505823 1 3 803225386673329326 7 8 139939802736535485 9 10 657980865814127926 2 4 146378247587539124
241240228
Print the sum modulo $(10^9+7)$.