#P10992. [2015杭电多校]Persistent Link/cut Tree

[2015杭电多校]Persistent Link/cut Tree

Persistent Link/cut Tree

Problem Description

Teacher Mai has m+1m+1 trees, T0,T1,,TmT_0,T_1,\cdots,T_m. T0T_0 consists one vertex numbered 00. He generated the TiT_i in this way. Get a copy of TaiT_{a_i} and TbiT_{b_i}. Add an edge with length lil_i between vertex numbered cic_i in TaiT'_{a_i} and did_i in TbiT'_{b_i}. Relabel the vertices in the new tree. Let kk be the number of vertices in TaiT'_{a_i}. He keeps labels of vertices in TaiT'_{a_i} the same, and adds kk to labels of vertices in TbiT'_{b_i}. If there is a tree TT with nn vertices v0,v1,v2,,vn1v_0,v_1,v_2,\cdots,v_{n-1}, $F(T)=\sum_{i=0}^{n-1} \sum_{j=i+1}^{n-1} d(v_i,v_j)$(d(vi,vj)d(v_i,v_j) means the distance between the viv_i and vjv_j). For every i(1im)i(1\leq i\leq m), he wants to know F(Ti)F(T_i).

Input

There are multiple test cases(about 100100). For each test case, the first line contains one number m(1m60)m(1\leq m\leq 60), the following are mm lines. The ii-th lines contains five numbers $a_i,b_i,c_i,d_i,l_i(0\leq a_i,b_i<i,0\leq l_i\leq 10^9)$. It's guarenteed that there exists a vertex numbered cic_i in TaiT_{a_i} and there exists a vertex numbered did_i in TbiT_{b_i}.

Output

For each test case, print F(Ti)F(T_i) modulo 109+710^9+7 in the ii-th line.

Sample Input

3
0 0 0 0 2
1 1 0 0 4
2 2 1 0 3

Sample Output

2
28
216

Author

xudyh

Source

2015 Multi-University Training Contest 9