Universal Cup Judging System

Universal Cup

时间限制: 4.0 s 内存限制: 256 MB 总分: 100 可 Hack ✓
统计

Given a tree with $N$ nodes and $N - 1$ edges, where each edge $i$ has a complexity level $l_i$. The distance $d(u, v)$ between two nodes $u$ and $v$ is the sum of the complexity levels along the unique path between them in the tree. Note that $d(u, v) = d(v, u)$ and $d(v, v) = 0$.

There are $M$ puzzles, and each puzzle $i$ requires $k_i$ pieces of information, which are located along specific edges in the tree. It's possible for multiple pieces of information to be on the same edge!

For any two nodes $S$ and $T$, a journey between $S$ and $T$ is successful if, along the path from $S$ to $T$, all necessary pieces of information for any puzzle encountered are collected. Specifically, a puzzle is considered solvable during a journey if all required pieces of information for that puzzle are encountered along the path.

The task is to compute the sum of distances for all successful journeys between any two nodes $S$ and $T$ (where $1 \le S \le T \le N$), modulo $10^9 + 7$.

Formally, the task is to calculate:

$$\sum_{\substack{1 \le S \le T \le N, \\ \text{journey } (S, T) \text{ is successful}}} d(S, T)$$

Starting from a node $S$, you need to travel to another node $T$ along the unique path in the tree. Along the way, any piece of information located on the edges of this path will automatically be collected. For the journey from $S$ to $T$ to be considered successful, it must meet the following condition:

  • Let $O$ be the set of puzzles for which at least one piece of information has been collected during the journey. The journey is successful if and only if all the pieces of information required to solve every puzzle in the set $O$ have been collected. In other words, you must have gathered every piece of information needed to completely solve each puzzle in $O$.

Input

The first line contains two space-separated integers $N$ and $M$ ($2 \le N \le 10^6, 1 \le M \le 10^6$).

Then, $N - 1$ lines follow, each containing three space-separated integers $a_i, b_i, l_i$ ($1 \le a_i, b_i \le N, 1 \le l_i \le 10^9$), describing an edge $i$ which connects nodes $a_i$ and $b_i$ with complexity $l_i$.

Then, the definitions of $M$ puzzles follow. For each puzzle $i$, the input starts with one line containing $k_i$ followed by $k_i$ space-separated integers $u_{i_1}, u_{i_2}, \dots, u_{i_{k_i}}$ ($0 \le k_i \le K, 1 \le u_{i_j} \le N - 1$), where $u_{i_j}$ are the indices of the edges on which the pieces of information required to solve the puzzle are located.

Here, $K$ is the sum of all $k_i$ values across all puzzles, and it is guaranteed that $1 \le K \le 10^6$.

Output

Output one line, containing one integer, the answer for the problem modulo $10^9 + 7$.

Example

Sample Input 1

5 4
1 2 3
1 4 1
1 5 6
4 3 2
2 4 4
1 1
0
2 3 4

Sample Output 1

17

Note

problem_15524_1.png

Figure 1. The structure of the tree. The diamonds are the pieces of information, and the values on the edges are the complexities.

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.