eolymp
bolt
Try our new interface for solving problems
Problems

Kitty's Calculations on a Tree

Kitty's Calculations on a Tree

Kitty has a tree $T$, consisting of $n$ nodes where each node is uniquely labeled from $1$ to $n$. Her friend Alex gave her $q$ sets, where each set contains distinct nodes. Kitty needs to calculate the following expression on each set: $$ (\sum_{\{u,v\}} u \cdot v \cdot dist(u,v)) \mod (10^9 + 7) $$ where: \begin{itemize} \item $\{u, v\}$ denotes an unordered pair of nodes belonging to the set. \item $dist(u, v)$ denotes the number of edges on the unique (shortest) path between nodes $u$ and $v$. \end{itemize} Given $T$ and $q$ sets of $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. \InputFile The first line contains two integers: the number $n~(1 \le n \le 2 \cdot 10^5)$ of nodes in tree $T$ and $q~(1 \le q \le 10^5)$ --- the number of nodes in the query set. Each of the $n - 1$ subsequent lines contains two integers $a$ and $b~(1 \le a, b \le n)$, that describe an undirected edge between nodes $a$ and $b$. The $2 \cdot q$ subsequent lines define each set over two lines in the following format: \begin{itemize} \item The first line contains an integer $k$ --- the size of the set; \item The second line contains $k$ integers, the set's elements $(1 \le k_i \le 10^5$, the sum of $k_i$ over all $q$ does not exceed $2 \cdot 10^5)$. \end{itemize} \OutputFile Print $q$ lines where each line $i$ contains the expression for the $i$-th query, modulo $10^9 + 7$.
Time limit 1 second
Memory limit 128 MiB
Input example #1
7 3
1 2
1 3
1 4
3 5
3 6
3 7
2
2 4
1
5
3
2 4 5
Output example #1
16
0
106