#P7581. 「RdOI R2」路径权值(distance)

「RdOI R2」路径权值(distance)

Description

You are given a rooted tree with nn nodes and weighted edges. The root is node 11. Define the kk-son of a node uu as all nodes in the subtree of uu whose depth (number of edges on the path) is exactly kk greater than that of uu.
There are mm queries. For each query, compute the sum of distances between every pair of nodes among the kk-sons of a node uu. You need to output the result modulo mod(109+7)\bmod\left(10^9+7\right).

Input Format

The first line contains two integers n,mn, m.
The next n1n - 1 lines each contain three integers u,v,wu, v, w, indicating that there is an edge of weight ww between uu and vv.
The next mm lines each contain two integers u,ku, k, indicating a query.

Output Format

For each query, output one line containing the answer.

5 3 
1 2 2
1 3 1
2 4 1
2 5 2
1 1
1 2
2 1
3
3
3
10 5
1 2 1
1 3 3
2 4 2
2 5 2
3 6 3
3 7 1
5 8 2
6 9 1
6 10 3
1 2
3 2
6 1
1 3
2 2
40
4
4
30
0

Hint

Sample 11 Explanation

Below is the tree in the sample.


Sample 22 Explanation

Below is the tree in the sample.


Constraints

For 20%20\% of the testdata, n,m,k100n, m, k \le 100.
For 50%50\% of the testdata, n,m,k103n, m, k \le 10^3.
For 80%80\% of the testdata, n,m,k105n, m, k \le 10^5.
For 100%100\% of the testdata, 1n,m,k1061 \le n, m, k \le 10^6, 1kn1 \le k \le n, 1w1051 \le w \le 10^5, 1u,vn1 \le u, v \le n. It is guaranteed that the given graph is a tree.

Translated by ChatGPT 5