题目描述
Kitty has a tree, $\mathbf{T}$, consisting of $n$ nodes where each node is uniquely labeled from $1$ to $n$. Her friend Alex gave her $\textit{q}$ sets, where each set contains $\boldsymbol{\mbox{k}}$ distinct nodes. Kitty needs to calculate the following expression on each set:
$\bigg(\sum_{\{u,v\}}u\cdot v\cdot dist(u,v)\bigg)\text{mod}\ (10^9+7)$
where:
$\{u,v\}$ denotes an unordered pair of nodes belonging to the set.
$dist(u,v)$ denotes the number of edges on the unique (shortest) path between nodes $\mbox{u}$ and $\boldsymbol{\nu}$.
Given $\mathbf{T}$ and $\textit{q}$ sets of $\boldsymbol{\mbox{k}}$ distinct nodes, calculate the expression for each set. For each set of nodes, print the value of the expression modulo $10^9+7$ on a new line.
Example
$edges=[[1,2],[1,3],[1,4],[3,5],[3,6],[3,7]]$
$queries=[4,5,7]$
The graph looks like this:
There are three pairs that can be created from the query set: $[4,5],[4,7],[5,7]$. The distance from $\begin{array}{c}4\end{array}$ to $5$ is $3$, from $\begin{array}{c}4\end{array}$ to $7$ is $3$, and from $5$ to $7$ is $2$.
Now do the summation:
$\begin{aligned}(4\cdot5\cdot dist(4,5)+4\cdot7\cdot dist(4,7)+5\cdot7\cdot dist(5,7))\ \text{mod}\ (10^9+7)\\ \Rightarrow(4\cdot5\cdot3+4\cdot7\cdot3+5\cdot7\cdot2)\ \text{mod}\ (10^9+7)\\ \Rightarrow214\end{aligned}$
Input Format
The first line contains two space-separated integers, the respective values of $n$ (the number of nodes in tree $\mathbf{T}$) and $\textit{q}$ (the number of nodes in the query set).
Each of the $n-1$ subsequent lines contains two space-separated integers, $\class{ML__boldsymbol}{\boldsymbol{a}}$ and $\boldsymbol{b}$, that describe an undirected edge between nodes $\class{ML__boldsymbol}{\boldsymbol{a}}$ and $\boldsymbol{b}$.
The $2\cdot q$ subsequent lines define each set over two lines in the following format:
The first line contains an integer, $\boldsymbol{\mbox{k}}$, the size of the set.
The second line contains $\boldsymbol{\mbox{k}}$ space-separated integers, the set's elements.
Constraints
$1\leq n\leq2\cdot10^5$
$1\leq a,b\leq n$
$1\leq q\leq10^5$
$1\leq k_i\leq10^5$
The sum of $k_i$ over all $\textit{q}$ does not exceed $2\cdot10^{5}$.
All elements in each set are distinct.
Subtasks
$1\leq n\leq2000$ for $24\%$ of the maximum score.
$1\leq n\leq5\cdot10^4$ for $45\%$ of the maximum score.
$1\leq n\leq2\cdot10^5$ for $\textbf{100\%}$ of the maximum score.
Output Format
Print $\textit{q}$ lines of output where each line $\boldsymbol{i}$ contains the expression for the $i^{\mbox{th}}$ query, modulo $10^9+7$.
Sample Input 0
7 3
1 2
1 3
1 4
3 5
3 6
3 7
2
2 4
1
5
3
2 4 5
Sample Output 0
16
0
106
Explanation 0
Tree $\mathbf{T}$ looks like this:
We perform the following calculations for $q=3$ sets:
Set $0$: Given set $\{2,4\}$, the only pair we can form is $(u,v)=(2,4)$, where $dist(2,4)=2$. We then calculate the following answer and print it on a new line:
$\begin{aligned}(2\cdot4\cdot dist(2,4))\ \text{mod}\ (10^9+7)\\ \Rightarrow(2\cdot4\cdot2)\ \text{mod}\ (10^9+7)\\ \Rightarrow16\end{aligned}$
Set $1$: Given set $\{5\}$, we cannot form any pairs because we don't have at least two elements. Thus, we print $0$ on a new line.
Set $2$: Given set $\{2,4,5\}$, we can form the pairs $(2,4)$, $(2,5)$, and $(4,5)$. We then calculate the following answer and print it on a new line:
$\begin{aligned}(2\cdot4\cdot dist(2,4)+2\cdot5\cdot dist(2,5)+4\cdot5\cdot dist(4,5))\ \text{mod}\ (10^9+7)\\ \Rightarrow(2\cdot4\cdot2+2\cdot5\cdot3+4\cdot5\cdot3)\ \text{mod}\ (10^9+7)\\ \Rightarrow106\end{aligned}$
输入格式
见题面
输出格式
见题面