Under construction

Still being built. Things may change

Be a human first

Time limit5sMemory limit1024 MB

Summary
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 nn vertices and n−1n-1 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 109+710^9+7.

Input

The first line contains the number of vertices nn.

Each of the next n−1n-1 lines contains integers uu, vv, and ww, meaning that an edge connects uu and vv and has weight ww.

Output

Print the value of the mugwort modulo 109+710^9+7.

Constraints

  • 2≤n≤200 0002 \le n \le 200\,000.
  • 1≤u,v≤n1\le u, v\le n.
  • For each (u,v,w)(u,v,w) triple, u≠vu\neq v.
  • 1≤w≤1091\le w\le 10^9.
  • 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.

Examples2

  1. Example 1

    Input
    4
    1 2 1
    1 3 1
    1 4 2
    
    Expected output
    20
    
  2. Example 2

    Input
    6
    1 3 6
    2 3 5
    3 4 11
    4 5 4
    4 6 6
    
    Expected output
    2021