Be a human first
Time limit5sMemory limit1024 MB
Given a weighted tree, compute the sum over all simple paths of (path length sum) times (max edge weight on path), modulo 1e9+7.
- Level
Medium7 of 10
- Topics
- Union-find, Sorting, Tree, Math
- Solved
- No attempts yet
Problem
The motto of Songdo High School is "Be a human first."
Dohun, who entered Songdo High School the year before last, read this motto carved on a rock on his way to school. He took it literally to mean that he should become a real human, so he started looking for the best garlic and mugwort to eat for 100 days.

This is the rock with the motto, photographed at the front gate of Songdo High School in winter 2021.
First, Dohun spent two and a half years comparing garlic from around the world and found the best one. Now only the mugwort was left to choose. But Dohun had planned to eat garlic and mugwort for 100 days and become a human before graduation. After choosing the garlic, very little time was left!
Mugwort can be represented as a tree with vertices and weighted edges. The value of the mugwort tree is defined as follows.
- For each simple path, the taste is the sum of the edge weights on the path.
- For each simple path, the aroma is the maximum edge weight on the path.
- The value of the mugwort is the sum, over all simple paths containing at least one edge, of taste multiplied by aroma.
- The direction of a simple path is not considered.
Write a program that computes the value of the given mugwort so that Dohun can choose the mugwort quickly. Since the value can become very large, print it modulo .
Input
The first line contains the number of vertices .
Each of the next lines contains integers , , and , meaning that an edge connects and and has weight .
Output
Print the value of the mugwort modulo .
Constraints
- .
- .
- For each triple, .
- .
- The given graph is guaranteed to be a tree.
Hints
- A tree is a connected graph with no cycles.
- In a tree, a simple path is the path between two vertices that uses the fewest edges.