#P7815. 「小窝 R3」自傷無色

「小窝 R3」自傷無色

Description

You are given a tree with nn nodes, rooted at 11, with edge weights. Define the path length d(u,v)d(u,v) between two nodes u,vu, v on the tree as the sum of edge weights along the path from uu to vv.

For an unordered pair (u,v)(u,v), define a "tree triangle" if and only if all of the following hold:

  • Let ww be the lowest common ancestor of u,vu, v. Then wuw\neq u and wvw\neq v.
  • Using d(u,w)d(u,w), d(v,w)d(v,w), and some positive integer xx as side lengths, a triangle can be formed. The value xx can be chosen arbitrarily, so a pair (u,v)(u,v) may produce multiple tree triangles.

In this case, d(u,w)+d(v,w)+xd(u,w)+d(v,w)+x is called the size of this tree triangle. See the sample explanation for a concrete example.

Two tree triangles are considered different if at least one of the following conditions holds:

  • The unordered pair (u,v)(u,v) is different.
  • The size of the tree triangle is different.

For a weighted tree TT, define its sine value sinT\sin T as the ratio of:

  • the sum of sizes of all tree triangles in TT,

to

  • the total number of distinct tree triangles in TT.

Little H gives you TT and wants you to compute sinT\sin T. To avoid precision issues, output the result modulo 109+710^9+7. In particular, if there are no tree triangles in TT, then sinT=0\sin T=0.

Input Format

The first line contains a positive integer nn, the number of nodes in the tree.

The next n1n-1 lines each contain three positive integers u,v,wu, v, w, indicating that there is an edge of weight ww between nodes uu and vv.

Output Format

Output one line: the value of sinT\sin T modulo 109+710^9+7. The testdata guarantees that the total number of distinct tree triangles in TT is not a multiple of 109+710^9+7.

5
1 2 2
1 3 3
2 4 1
2 5 2
214285725
9
1 2 9
1 3 3
2 4 5
2 5 7
2 6 2
1 7 1
3 8 6
3 9 4
662721928

Hint

Sample Explanation

For sample 1, TT is shown in the figure below.

The triangle cycles formed by nodes 1,2,31,2,3 are: $\underline{2,3},2;~\underline{2,3},3;~\underline{2,3},4$.

The triangle cycles formed by nodes 1,3,41,3,4 are: $\underline{3,3},1;~\underline{3,3},2;~\underline{3,3},3;~\underline{3,3},4;~\underline{3,3},5$.

The triangle cycles formed by nodes 1,3,51,3,5 are: $\underline{3,4},2;~\underline{3,4},3;~\underline{3,4},4;~\underline{3,4},5;~\underline{3,4},6$.

The triangle cycle formed by nodes 2,4,52,4,5 is: 1,2,2\underline{1,2},2.

Sum of all triangle cycle sizes: (7+8+9)+(7+8++11)+(9+10++13)+5=129(7+8+9)+(7+8+\dots+11)+(9+10+\dots+13)+5=129.

Total number of triangle cycles: 3+5+5+1=143+5+5+1=14.

sinT=12914\sin T=\dfrac{129}{14}, and the result modulo 109+710^9+7 is 214285725214285725.

Constraints

This problem uses bundled testdata.

  • Special Property A: There exists a node in TT with degree n1n-1.
  • Special Property B: Except for leaf nodes, every node has degree 22.
  • Special Property C: TT is a perfect binary tree.
Subtask Score 1n1\le n\le Special Property
11 55 33 None
22 1313 10310^3
33 1111 7×1037\times10^3
44 1717 10510^5 A
55 B
66 C
77 2020 None

For all testdata, 1n1051\le n\le 10^5 and 1w1091\le w\le 10^9.

Notes

In the attached file depression_sample.zip:

  • depression_sample1.in is sample #1.
  • depression_sample2.in satisfies Special Property A.
  • depression_sample3.in satisfies Special Property B.
  • depression_sample4.in satisfies Special Property C.
  • depression_sample5.in does not satisfy any special property.

Translated by ChatGPT 5