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}$

Difficulty: HARD
Tags: Tree algorithms

Starter code:

Solutions:
from collections import Counter, defaultdict
MOD = 10 ** 9 + 7

def read_row():
	return (int(x) for x in input().split())

def mul(x, y):
	return x * y % MOD

def add(*args):
	return sum(args) % MOD

def sub(x, y):
	return (x - y) % MOD
(n, q) = read_row()
adj_list = defaultdict(list)
for _ in range(n - 1):
	(u, v) = read_row()
	adj_list[u].append(v)
	adj_list[v].append(u)
elements = {v: set() for v in adj_list}
for set_no in range(q):
	read_row()
	for x in read_row():
		elements[x].add(set_no)
root = next(iter(adj_list))
current = [root]
current_depth = 0
order = []
parent = {root: None}
depth = {root: current_depth}
while current:
	current_depth += 1
	order.extend(current)
	nxt = []
	for node in current:
		for neighbor in adj_list[node]:
			if neighbor not in parent:
				parent[neighbor] = node
				depth[neighbor] = current_depth
				nxt.append(neighbor)
	current = nxt
score = Counter()
state = {}
for node in reversed(order):
	states = [state[neighbor] for neighbor in adj_list[node] if neighbor != parent[node]]
	largest = {s: [depth[node], node, 0] for s in elements[node]}
	if states:
		max_index = max(range(len(states)), key=lambda x: len(states[x]))
		if len(states[max_index]) > len(largest):
			(states[max_index], largest) = (largest, states[max_index])
	sets = defaultdict(list)
	for cur_state in states:
		for (set_no, v) in cur_state.items():
			sets[set_no].append(v)
	for (set_no, states) in sets.items():
		if len(states) == 1 and set_no not in largest:
			largest[set_no] = states[0]
			continue
		if set_no in largest:
			states.append(largest.pop(set_no))
		total_flow = 0
		total_node_sum = 0
		for (node_depth, node_sum, node_flow) in states:
			flow_delta = mul(node_depth - depth[node], node_sum)
			total_flow = add(total_flow, flow_delta, node_flow)
			total_node_sum += node_sum
		set_score = 0
		for (node_depth, node_sum, node_flow) in states:
			node_flow = add(mul(node_depth - depth[node], node_sum), node_flow)
			diff = mul(sub(total_flow, node_flow), node_sum)
			set_score = add(set_score, diff)
		score[set_no] = add(score[set_no], set_score)
		largest[set_no] = (depth[node], total_node_sum, total_flow)
	state[node] = largest
print(*(score[i] for i in range(q)), sep='\n')


def put(d, a, b):
	if a in d:
		d[a].append(b)
	else:
		d[a] = [b]

def main():
	for n in ns[::-1]:
		r = [tt[s] for s in tree[n] if s != f[n]]
		bst = {s: [gl[n], n, 0] for s in queries[n]}
		if r:
			o = max(range(len(r)), key=lambda a: len(r[a]))
			if len(r[o]) > len(bst):
				(r[o], bst) = (bst, r[o])
		ry = {}
		for ae in r:
			for (y, v) in ae.items():
				put(ry, y, v)
		for (y, r) in ry.items():
			(eq, z, t) = (0, 0, 0)
			if len(r) == 1 and y not in bst:
				bst[y] = r[0]
				continue
			if y in bst:
				r.append(bst.pop(y))
			for (d, v, c) in r:
				eq += (d - gl[n]) * v + c
				z += v
			for (d, v, c) in r:
				c += (d - gl[n]) * v
				diff = (eq - c) * v
				t += diff
			returns[y] += t
			bst[y] = (gl[n], z, eq)
		tt[n] = bst

def locate():
	q = [r]
	level = 0
	while q:
		level += 1
		tmp = []
		ns.extend(q)
		for n in q:
			for s in tree[n]:
				if s not in f:
					f[s] = n
					gl[s] = level
					tmp.append(s)
		q = tmp
tree = {}
tt = {}
(n, q) = map(int, input().split())
returns = [0] * q
for _ in range(n - 1):
	(a, b) = map(int, input().split())
	put(tree, a, b)
	put(tree, b, a)
queries = {a: set() for a in tree}
for y in range(q):
	input()
	for x in map(int, input().split()):
		queries[x].add(y)
r = next(iter(tree))
ns = []
f = {r: None}
gl = {r: 0}
locate()
main()
for s in returns:
	print(s % (10 ** 9 + 7))


from collections import Counter, defaultdict
MOD = 10 ** 9 + 7

def read_row():
	return (int(x) for x in input().split())

def mul(x, y):
	return x * y % MOD

def add(*args):
	return sum(args) % MOD

def sub(x, y):
	return (x - y) % MOD
(n, q) = read_row()
adj_list = defaultdict(list)
for _ in range(n - 1):
	(u, v) = read_row()
	adj_list[u].append(v)
	adj_list[v].append(u)
elements = {v: set() for v in adj_list}
for set_no in range(q):
	read_row()
	for x in read_row():
		elements[x].add(set_no)
root = next(iter(adj_list))
current = [root]
current_depth = 0
order = []
parent = {root: None}
depth = {root: current_depth}
while current:
	current_depth += 1
	order.extend(current)
	nxt = []
	for node in current:
		for neighbor in adj_list[node]:
			if neighbor not in parent:
				parent[neighbor] = node
				depth[neighbor] = current_depth
				nxt.append(neighbor)
	current = nxt
score = Counter()
state = {}
for node in reversed(order):
	states = [state[neighbor] for neighbor in adj_list[node] if neighbor != parent[node]]
	largest = {s: [depth[node], node, 0] for s in elements[node]}
	if states:
		max_index = max(range(len(states)), key=lambda x: len(states[x]))
		if len(states[max_index]) > len(largest):
			(states[max_index], largest) = (largest, states[max_index])
	sets = defaultdict(list)
	for cur_state in states:
		for (set_no, v) in cur_state.items():
			sets[set_no].append(v)
	for (set_no, states) in sets.items():
		if len(states) == 1 and set_no not in largest:
			largest[set_no] = states[0]
			continue
		if set_no in largest:
			states.append(largest.pop(set_no))
		total_flow = 0
		total_node_sum = 0
		for (node_depth, node_sum, node_flow) in states:
			flow_delta = mul(node_depth - depth[node], node_sum)
			total_flow = add(total_flow, flow_delta, node_flow)
			total_node_sum += node_sum
		set_score = 0
		for (node_depth, node_sum, node_flow) in states:
			node_flow = add(mul(node_depth - depth[node], node_sum), node_flow)
			diff = mul(sub(total_flow, node_flow), node_sum)
			set_score = add(set_score, diff)
		score[set_no] = add(score[set_no], set_score)
		largest[set_no] = (depth[node], total_node_sum, total_flow)
	state[node] = largest
print(*(score[i] for i in range(q)), sep='\n')


from collections import Counter, defaultdict
MOD = 10 ** 9 + 7

def read_row():
	return (int(x) for x in input().split())

def mul(x, y):
	return x * y % MOD

def add(*args):
	return sum(args) % MOD

def sub(x, y):
	return (x - y) % MOD
(n, q) = read_row()
adj_list = defaultdict(list)
for _ in range(n - 1):
	(u, v) = read_row()
	adj_list[u].append(v)
	adj_list[v].append(u)
elements = {v: set() for v in adj_list}
for set_no in range(q):
	read_row()
	for x in read_row():
		elements[x].add(set_no)
root = next(iter(adj_list))
current = [root]
current_depth = 0
order = []
parent = {root: None}
depth = {root: current_depth}
while current:
	current_depth += 1
	order.extend(current)
	nxt = []
	for node in current:
		for neighbor in adj_list[node]:
			if neighbor not in parent:
				parent[neighbor] = node
				depth[neighbor] = current_depth
				nxt.append(neighbor)
	current = nxt
score = Counter()
state = {}
for node in reversed(order):
	states = [state[neighbor] for neighbor in adj_list[node] if neighbor != parent[node]]
	largest = {s: [depth[node], node, 0] for s in elements[node]}
	if states:
		max_index = max(range(len(states)), key=lambda x: len(states[x]))
		if len(states[max_index]) > len(largest):
			(states[max_index], largest) = (largest, states[max_index])
	sets = defaultdict(list)
	for cur_state in states:
		for (set_no, v) in cur_state.items():
			sets[set_no].append(v)
	for (set_no, states) in sets.items():
		if len(states) == 1 and set_no not in largest:
			largest[set_no] = states[0]
			continue
		if set_no in largest:
			states.append(largest.pop(set_no))
		total_flow = 0
		total_node_sum = 0
		for (node_depth, node_sum, node_flow) in states:
			flow_delta = mul(node_depth - depth[node], node_sum)
			total_flow = add(total_flow, flow_delta, node_flow)
			total_node_sum += node_sum
		set_score = 0
		for (node_depth, node_sum, node_flow) in states:
			node_flow = add(mul(node_depth - depth[node], node_sum), node_flow)
			diff = mul(sub(total_flow, node_flow), node_sum)
			set_score = add(set_score, diff)
		score[set_no] = add(score[set_no], set_score)
		largest[set_no] = (depth[node], total_node_sum, total_flow)
	state[node] = largest
print(*(score[i] for i in range(q)), sep='\n')


def put(d, a, b):
	if a in d:
		d[a].append(b)
	else:
		d[a] = [b]

def main():
	for n in ns[::-1]:
		r = [tt[s] for s in tree[n] if s != f[n]]
		bst = {s: [gl[n], n, 0] for s in queries[n]}
		if r:
			o = max(range(len(r)), key=lambda a: len(r[a]))
			if len(r[o]) > len(bst):
				(r[o], bst) = (bst, r[o])
		ry = {}
		for ae in r:
			for (y, v) in ae.items():
				put(ry, y, v)
		for (y, r) in ry.items():
			(eq, z, t) = (0, 0, 0)
			if len(r) == 1 and y not in bst:
				bst[y] = r[0]
				continue
			if y in bst:
				r.append(bst.pop(y))
			for (d, v, c) in r:
				eq += (d - gl[n]) * v + c
				z += v
			for (d, v, c) in r:
				c += (d - gl[n]) * v
				diff = (eq - c) * v
				t += diff
			returns[y] += t
			bst[y] = (gl[n], z, eq)
		tt[n] = bst

def locate():
	q = [r]
	level = 0
	while q:
		level += 1
		tmp = []
		ns.extend(q)
		for n in q:
			for s in tree[n]:
				if s not in f:
					f[s] = n
					gl[s] = level
					tmp.append(s)
		q = tmp
tree = {}
tt = {}
(n, q) = map(int, input().split())
returns = [0] * q
for _ in range(n - 1):
	(a, b) = map(int, input().split())
	put(tree, a, b)
	put(tree, b, a)
queries = {a: set() for a in tree}
for y in range(q):
	input()
	for x in map(int, input().split()):
		queries[x].add(y)
r = next(iter(tree))
ns = []
f = {r: None}
gl = {r: 0}
locate()
main()
for s in returns:
	print(s % (10 ** 9 + 7))


from collections import Counter, defaultdict
MOD = 10 ** 9 + 7

def read_row():
	return (int(x) for x in input().split())

def mul(x, y):
	return x * y % MOD

def add(*args):
	return sum(args) % MOD

def sub(x, y):
	return (x - y) % MOD
(n, q) = read_row()
adj_list = defaultdict(list)
for _ in range(n - 1):
	(u, v) = read_row()
	adj_list[u].append(v)
	adj_list[v].append(u)
elements = {v: set() for v in adj_list}
for set_no in range(q):
	read_row()
	for x in read_row():
		elements[x].add(set_no)
root = next(iter(adj_list))
current = [root]
current_depth = 0
order = []
parent = {root: None}
depth = {root: current_depth}
while current:
	current_depth += 1
	order.extend(current)
	nxt = []
	for node in current:
		for neighbor in adj_list[node]:
			if neighbor not in parent:
				parent[neighbor] = node
				depth[neighbor] = current_depth
				nxt.append(neighbor)
	current = nxt
score = Counter()
state = {}
for node in reversed(order):
	states = [state[neighbor] for neighbor in adj_list[node] if neighbor != parent[node]]
	largest = {s: [depth[node], node, 0] for s in elements[node]}
	if states:
		max_index = max(range(len(states)), key=lambda x: len(states[x]))
		if len(states[max_index]) > len(largest):
			(states[max_index], largest) = (largest, states[max_index])
	sets = defaultdict(list)
	for cur_state in states:
		for (set_no, v) in cur_state.items():
			sets[set_no].append(v)
	for (set_no, states) in sets.items():
		if len(states) == 1 and set_no not in largest:
			largest[set_no] = states[0]
			continue
		if set_no in largest:
			states.append(largest.pop(set_no))
		total_flow = 0
		total_node_sum = 0
		for (node_depth, node_sum, node_flow) in states:
			flow_delta = mul(node_depth - depth[node], node_sum)
			total_flow = add(total_flow, flow_delta, node_flow)
			total_node_sum += node_sum
		set_score = 0
		for (node_depth, node_sum, node_flow) in states:
			node_flow = add(mul(node_depth - depth[node], node_sum), node_flow)
			diff = mul(sub(total_flow, node_flow), node_sum)
			set_score = add(set_score, diff)
		score[set_no] = add(score[set_no], set_score)
		largest[set_no] = (depth[node], total_node_sum, total_flow)
	state[node] = largest
print(*(score[i] for i in range(q)), sep='\n')


from dataclasses import dataclass
from threading import Thread

class FBRMQ:

	def __init__(self, values):
		self.values = values
		value_length = len(values)
		block_size = value_length.bit_length() // 2
		block_count = (value_length + block_size - 1) // block_size
		table_sizes = [-1] * (block_count + 1)
		for i in range(1, block_count + 1):
			table_sizes[i] = table_sizes[i // 2] + 1
		sparse_table = [[0] * (table_sizes[-block - 1] + 1) for block in range(block_count)]
		block_mask = [0] * block_count
		for index in range(value_length):
			block = index // block_size
			block_index = index % block_size
			if not block_index:
				sparse_table[block][0] = index
			elif values[index] < values[sparse_table[block][0]]:
				sparse_table[block][0] = index
			if block_index > 0 and values[index - 1] <= values[index]:
				block_mask[block] += 1 << block_index - 1
		for size in range(1, table_sizes[block_count] + 1):
			block_index = 0
			while block_index + (1 << size) - 1 < block_count:
				previous_min_index = sparse_table[block_index][size - 1]
				next_min_index = sparse_table[block_index + (1 << size - 1)][size - 1]
				if values[previous_min_index] <= values[next_min_index]:
					sparse_table[block_index][size] = previous_min_index
				else:
					sparse_table[block_index][size] = next_min_index
				block_index += 1
		blocks = [[]] * (1 << block_size - 1)
		for (block, mask) in enumerate(block_mask):
			if not blocks[mask]:
				blocks[mask] = [[0] * block_size for _ in range(block_size)]
				for left in range(block_size):
					blocks[mask][left][left] = left
					for right in range(left + 1, block_size):
						blocks[mask][left][right] = blocks[mask][left][right - 1]
						if block * block_size + right < value_length:
							blocks[mask][left][right] = min(block * block_size + blocks[mask][left][right], block * block_size + right, key=self.value) - block * block_size
		self.known_lca = {}
		self.block_size = block_size
		self.block_count = block_count
		self.blocks = blocks
		self.block_mask = block_mask
		self.sparse_table = sparse_table
		self.table_sizes = table_sizes

	def lca_in_block(self, block, start, end):
		return self.blocks[self.block_mask[block]][start][end] + block * self.block_size

	def value(self, index):
		return self.values[index]

	def minimum(self, left, right):
		if left > right:
			(left, right) = (right, left)
		if (left, right) in self.known_lca:
			return self.known_lca[left, right]
		left_block = left // self.block_size
		right_block = right // self.block_size
		if left_block == right_block:
			return self.lca_in_block(left_block, left % self.block_size, right % self.block_size)
		left_min = self.lca_in_block(left_block, left % self.block_size, self.block_size - 1)
		right_min = self.lca_in_block(right_block, 0, right % self.block_size)
		if left_block + 1 < right_block:
			table_size = self.table_sizes[right_block - left_block - 1]
			answers = [left_min, right_min, self.sparse_table[left_block + 1][table_size], self.sparse_table[right_block - (1 << table_size)][table_size]]
		else:
			answers = [left_min, right_min]
		try:
			lca = min(answers, key=self.value)
		except IndexError:
			lca = 0
		self.known_lca[left, right] = lca
		return lca

@dataclass
class KittyResult:
	root_distances: int
	index: int
	level: int = 0
	sum: int = 0
	distance: int = 0

class KittyNode:

	def __init__(self, label, parent=None):
		self.label = label
		self.parent = parent
		self.children = []
		self.level = 1 + parent.level if parent else 0
		self.traversal_index = 0
		self.known_lca = {}

	def add_child(self, label):
		child = KittyNode(label, self)
		self.children.append(child)
		return child

class KittyTree:

	def __init__(self, length):
		self.root = None
		self.nodes = [-1] * (length + 1)
		self.euler_list = [0] * (2 * length - 1)

	def update_traversal(self):
		nodes = [self.root, self.root]
		known = set()
		euler_index = 0
		euler_list = self.euler_list
		while nodes:
			node = nodes.pop()
			if node not in known:
				euler_list[euler_index] = node
				node.euler_index = euler_index
				euler_index += 1
				known.add(node)
				for child in node.children:
					nodes.extend([child, child])
			else:
				try:
					parent = node.parent
					parent.euler_index = euler_index
					euler_list[euler_index] = parent
				except AttributeError:
					pass
				euler_index += 1
		self.rmq_tree = FBRMQ([node.level for node in euler_list])

	def insert_edge(self, first, second):
		if self.root is None:
			self.root = KittyNode(first)
			self.nodes[first] = self.root
			node = self.root
		else:
			node = self.nodes[first]
			if node == -1:
				node = self.nodes[second]
				second = first
		new_node = node.add_child(second)
		self.nodes[second] = new_node

	def kitty_sum(self, node_map):
		results = [KittyResult(level=node.level, root_distances=node.label, index=node.euler_index) for node in sorted([self.nodes[label] for label in node_map], key=lambda node: node.euler_index)]
		get_lca = self.lca
		results_length = len(results)
		while results_length > 1:
			cursor = 0
			pending_cursors = []
			while cursor + 1 < results_length:
				(r_u, r_v) = results[cursor:cursor + 2]
				lca = get_lca(r_u.index, r_v.index)
				if pending_cursors and pending_cursors[-1][0] >= lca.level:
					cursor = pending_cursors.pop()[1]
					(r_u, r_v) = results[cursor:cursor + 2]
					lca = get_lca(r_u.index, r_v.index)
				else:
					while results_length > cursor + 2:
						r_next = results[cursor + 2]
						lca_next = get_lca(r_v.index, r_next.index)
						if lca.level < lca_next.level:
							pending_cursors.append((lca.level, cursor))
							cursor += 1
							(r_u, r_v, lca) = (r_v, r_next, lca_next)
						else:
							break
				del results[cursor + 1]
				result = results[cursor]
				u_increment = r_u.sum + r_u.root_distances * (r_u.level - lca.level)
				v_increment = r_v.sum + r_v.root_distances * (r_v.level - lca.level)
				result.distance = r_u.distance + r_v.distance + r_u.root_distances * v_increment + r_v.root_distances * u_increment
				result.sum = u_increment + v_increment
				result.root_distances = r_v.root_distances + r_u.root_distances
				result.level = lca.level
				result.node = lca
				results_length -= 1
		return results[0].distance

	def lca(self, u_index, v_index):
		return self.euler_list[self.rmq_tree.minimum(u_index, v_index)]

def read_query():
	_ = input()
	return map(int, input().split())
(node_number, query_number) = map(int, input().split())
tree = KittyTree(node_number)
insert_edge = tree.insert_edge
for (u_label, v_label) in (map(int, input().split()) for _ in range(node_number - 1)):
	insert_edge(u_label, v_label)
tree.update_traversal()
queries = [read_query() for _ in range(query_number)]
[print(tree.kitty_sum(query) % 1000000007) for query in queries]


from collections import Counter, defaultdict
MOD = 10 ** 9 + 7

def read_row():
	return (int(x) for x in input().split())

def mul(x, y):
	return x * y % MOD

def add(*args):
	return sum(args) % MOD

def sub(x, y):
	return (x - y) % MOD
(n, q) = read_row()
adj_list = defaultdict(list)
for _ in range(n - 1):
	(u, v) = read_row()
	adj_list[u].append(v)
	adj_list[v].append(u)
elements = {v: set() for v in adj_list}
for set_no in range(q):
	read_row()
	for x in read_row():
		elements[x].add(set_no)
root = next(iter(adj_list))
current = [root]
current_depth = 0
order = []
parent = {root: None}
depth = {root: current_depth}
while current:
	current_depth += 1
	order.extend(current)
	nxt = []
	for node in current:
		for neighbor in adj_list[node]:
			if neighbor not in parent:
				parent[neighbor] = node
				depth[neighbor] = current_depth
				nxt.append(neighbor)
	current = nxt
score = Counter()
state = {}
for node in reversed(order):
	states = [state[neighbor] for neighbor in adj_list[node] if neighbor != parent[node]]
	largest = {s: [depth[node], node, 0] for s in elements[node]}
	if states:
		max_index = max(range(len(states)), key=lambda x: len(states[x]))
		if len(states[max_index]) > len(largest):
			(states[max_index], largest) = (largest, states[max_index])
	sets = defaultdict(list)
	for cur_state in states:
		for (set_no, v) in cur_state.items():
			sets[set_no].append(v)
	for (set_no, states) in sets.items():
		if len(states) == 1 and set_no not in largest:
			largest[set_no] = states[0]
			continue
		if set_no in largest:
			states.append(largest.pop(set_no))
		total_flow = 0
		total_node_sum = 0
		for (node_depth, node_sum, node_flow) in states:
			flow_delta = mul(node_depth - depth[node], node_sum)
			total_flow = add(total_flow, flow_delta, node_flow)
			total_node_sum += node_sum
		set_score = 0
		for (node_depth, node_sum, node_flow) in states:
			node_flow = add(mul(node_depth - depth[node], node_sum), node_flow)
			diff = mul(sub(total_flow, node_flow), node_sum)
			set_score = add(set_score, diff)
		score[set_no] = add(score[set_no], set_score)
		largest[set_no] = (depth[node], total_node_sum, total_flow)
	state[node] = largest
print(*(score[i] for i in range(q)), sep='\n')


from collections import Counter, defaultdict
MOD = 10 ** 9 + 7

def read_row():
	return (int(x) for x in input().split())

def mul(x, y):
	return x * y % MOD

def add(*args):
	return sum(args) % MOD

def sub(x, y):
	return (x - y) % MOD
(n, q) = read_row()
adj_list = defaultdict(list)
for _ in range(n - 1):
	(u, v) = read_row()
	adj_list[u].append(v)
	adj_list[v].append(u)
elements = {v: set() for v in adj_list}
for set_no in range(q):
	read_row()
	for x in read_row():
		elements[x].add(set_no)
root = next(iter(adj_list))
current = [root]
current_depth = 0
order = []
parent = {root: None}
depth = {root: current_depth}
while current:
	current_depth += 1
	order.extend(current)
	nxt = []
	for node in current:
		for neighbor in adj_list[node]:
			if neighbor not in parent:
				parent[neighbor] = node
				depth[neighbor] = current_depth
				nxt.append(neighbor)
	current = nxt
score = Counter()
state = {}
for node in reversed(order):
	states = [state[neighbor] for neighbor in adj_list[node] if neighbor != parent[node]]
	largest = {s: [depth[node], node, 0] for s in elements[node]}
	if states:
		max_index = max(range(len(states)), key=lambda x: len(states[x]))
		if len(states[max_index]) > len(largest):
			(states[max_index], largest) = (largest, states[max_index])
	sets = defaultdict(list)
	for cur_state in states:
		for (set_no, v) in cur_state.items():
			sets[set_no].append(v)
	for (set_no, states) in sets.items():
		if len(states) == 1 and set_no not in largest:
			largest[set_no] = states[0]
			continue
		if set_no in largest:
			states.append(largest.pop(set_no))
		total_flow = 0
		total_node_sum = 0
		for (node_depth, node_sum, node_flow) in states:
			flow_delta = mul(node_depth - depth[node], node_sum)
			total_flow = add(total_flow, flow_delta, node_flow)
			total_node_sum += node_sum
		set_score = 0
		for (node_depth, node_sum, node_flow) in states:
			node_flow = add(mul(node_depth - depth[node], node_sum), node_flow)
			diff = mul(sub(total_flow, node_flow), node_sum)
			set_score = add(set_score, diff)
		score[set_no] = add(score[set_no], set_score)
		largest[set_no] = (depth[node], total_node_sum, total_flow)
	state[node] = largest
print(*(score[i] for i in range(q)), sep='\n')


from collections import Counter, defaultdict
MOD = 10 ** 9 + 7

def read_row():
	return (int(x) for x in input().split())

def mul(x, y):
	return x * y % MOD

def add(*args):
	return sum(args) % MOD

def sub(x, y):
	return (x - y) % MOD
(n, q) = read_row()
adj_list = defaultdict(list)
for _ in range(n - 1):
	(u, v) = read_row()
	adj_list[u].append(v)
	adj_list[v].append(u)
elements = {v: set() for v in adj_list}
for set_no in range(q):
	read_row()
	for x in read_row():
		elements[x].add(set_no)
root = next(iter(adj_list))
current = [root]
current_depth = 0
order = []
parent = {root: None}
depth = {root: current_depth}
while current:
	current_depth += 1
	order.extend(current)
	nxt = []
	for node in current:
		for neighbor in adj_list[node]:
			if neighbor not in parent:
				parent[neighbor] = node
				depth[neighbor] = current_depth
				nxt.append(neighbor)
	current = nxt
score = Counter()
state = {}
for node in reversed(order):
	states = [state[neighbor] for neighbor in adj_list[node] if neighbor != parent[node]]
	largest = {s: [depth[node], node, 0] for s in elements[node]}
	if states:
		max_index = max(range(len(states)), key=lambda x: len(states[x]))
		if len(states[max_index]) > len(largest):
			(states[max_index], largest) = (largest, states[max_index])
	sets = defaultdict(list)
	for cur_state in states:
		for (set_no, v) in cur_state.items():
			sets[set_no].append(v)
	for (set_no, states) in sets.items():
		if len(states) == 1 and set_no not in largest:
			largest[set_no] = states[0]
			continue
		if set_no in largest:
			states.append(largest.pop(set_no))
		total_flow = 0
		total_node_sum = 0
		for (node_depth, node_sum, node_flow) in states:
			flow_delta = mul(node_depth - depth[node], node_sum)
			total_flow = add(total_flow, flow_delta, node_flow)
			total_node_sum += node_sum
		set_score = 0
		for (node_depth, node_sum, node_flow) in states:
			node_flow = add(mul(node_depth - depth[node], node_sum), node_flow)
			diff = mul(sub(total_flow, node_flow), node_sum)
			set_score = add(set_score, diff)
		score[set_no] = add(score[set_no], set_score)
		largest[set_no] = (depth[node], total_node_sum, total_flow)
	state[node] = largest
print(*(score[i] for i in range(q)), sep='\n')


from collections import Counter, defaultdict
MOD = 10 ** 9 + 7

def read_row():
	return (int(x) for x in input().split())

def mul(x, y):
	return x * y % MOD

def add(*args):
	return sum(args) % MOD

def sub(x, y):
	return (x - y) % MOD
(n, q) = read_row()
adj_list = defaultdict(list)
for _ in range(n - 1):
	(u, v) = read_row()
	adj_list[u].append(v)
	adj_list[v].append(u)
elements = {v: set() for v in adj_list}
for set_no in range(q):
	read_row()
	for x in read_row():
		elements[x].add(set_no)
root = next(iter(adj_list))
current = [root]
current_depth = 0
order = []
parent = {root: None}
depth = {root: current_depth}
while current:
	current_depth += 1
	order.extend(current)
	nxt = []
	for node in current:
		for neighbor in adj_list[node]:
			if neighbor not in parent:
				parent[neighbor] = node
				depth[neighbor] = current_depth
				nxt.append(neighbor)
	current = nxt
score = Counter()
state = {}
for node in reversed(order):
	states = [state[neighbor] for neighbor in adj_list[node] if neighbor != parent[node]]
	largest = {s: [depth[node], node, 0] for s in elements[node]}
	if states:
		max_index = max(range(len(states)), key=lambda x: len(states[x]))
		if len(states[max_index]) > len(largest):
			(states[max_index], largest) = (largest, states[max_index])
	sets = defaultdict(list)
	for cur_state in states:
		for (set_no, v) in cur_state.items():
			sets[set_no].append(v)
	for (set_no, states) in sets.items():
		if len(states) == 1 and set_no not in largest:
			largest[set_no] = states[0]
			continue
		if set_no in largest:
			states.append(largest.pop(set_no))
		total_flow = 0
		total_node_sum = 0
		for (node_depth, node_sum, node_flow) in states:
			flow_delta = mul(node_depth - depth[node], node_sum)
			total_flow = add(total_flow, flow_delta, node_flow)
			total_node_sum += node_sum
		set_score = 0
		for (node_depth, node_sum, node_flow) in states:
			node_flow = add(mul(node_depth - depth[node], node_sum), node_flow)
			diff = mul(sub(total_flow, node_flow), node_sum)
			set_score = add(set_score, diff)
		score[set_no] = add(score[set_no], set_score)
		largest[set_no] = (depth[node], total_node_sum, total_flow)
	state[node] = largest
print(*(score[i] for i in range(q)), sep='\n')


from collections import Counter, defaultdict
MOD = 10 ** 9 + 7

def read_row():
	return (int(x) for x in input().split())

def mul(x, y):
	return x * y % MOD

def add(*args):
	return sum(args) % MOD

def sub(x, y):
	return (x - y) % MOD
(n, q) = read_row()
adj_list = defaultdict(list)
for _ in range(n - 1):
	(u, v) = read_row()
	adj_list[u].append(v)
	adj_list[v].append(u)
elements = {v: set() for v in adj_list}
for set_no in range(q):
	read_row()
	for x in read_row():
		elements[x].add(set_no)
root = next(iter(adj_list))
current = [root]
current_depth = 0
order = []
parent = {root: None}
depth = {root: current_depth}
while current:
	current_depth += 1
	order.extend(current)
	nxt = []
	for node in current:
		for neighbor in adj_list[node]:
			if neighbor not in parent:
				parent[neighbor] = node
				depth[neighbor] = current_depth
				nxt.append(neighbor)
	current = nxt
score = Counter()
state = {}
for node in reversed(order):
	states = [state[neighbor] for neighbor in adj_list[node] if neighbor != parent[node]]
	largest = {s: [depth[node], node, 0] for s in elements[node]}
	if states:
		max_index = max(range(len(states)), key=lambda x: len(states[x]))
		if len(states[max_index]) > len(largest):
			(states[max_index], largest) = (largest, states[max_index])
	sets = defaultdict(list)
	for cur_state in states:
		for (set_no, v) in cur_state.items():
			sets[set_no].append(v)
	for (set_no, states) in sets.items():
		if len(states) == 1 and set_no not in largest:
			largest[set_no] = states[0]
			continue
		if set_no in largest:
			states.append(largest.pop(set_no))
		total_flow = 0
		total_node_sum = 0
		for (node_depth, node_sum, node_flow) in states:
			flow_delta = mul(node_depth - depth[node], node_sum)
			total_flow = add(total_flow, flow_delta, node_flow)
			total_node_sum += node_sum
		set_score = 0
		for (node_depth, node_sum, node_flow) in states:
			node_flow = add(mul(node_depth - depth[node], node_sum), node_flow)
			diff = mul(sub(total_flow, node_flow), node_sum)
			set_score = add(set_score, diff)
		score[set_no] = add(score[set_no], set_score)
		largest[set_no] = (depth[node], total_node_sum, total_flow)
	state[node] = largest
print(*(score[i] for i in range(q)), sep='\n')


from collections import Counter, defaultdict
MOD = 10 ** 9 + 7

def read_row():
	return (int(x) for x in input().split())

def mul(x, y):
	return x * y % MOD

def add(*args):
	return sum(args) % MOD

def sub(x, y):
	return (x - y) % MOD
(n, q) = read_row()
adj_list = defaultdict(list)
for _ in range(n - 1):
	(u, v) = read_row()
	adj_list[u].append(v)
	adj_list[v].append(u)
elements = {v: set() for v in adj_list}
for set_no in range(q):
	read_row()
	for x in read_row():
		elements[x].add(set_no)
root = next(iter(adj_list))
current = [root]
current_depth = 0
order = []
parent = {root: None}
depth = {root: current_depth}
while current:
	current_depth += 1
	order.extend(current)
	nxt = []
	for node in current:
		for neighbor in adj_list[node]:
			if neighbor not in parent:
				parent[neighbor] = node
				depth[neighbor] = current_depth
				nxt.append(neighbor)
	current = nxt
score = Counter()
state = {}
for node in reversed(order):
	states = [state[neighbor] for neighbor in adj_list[node] if neighbor != parent[node]]
	largest = {s: [depth[node], node, 0] for s in elements[node]}
	if states:
		max_index = max(range(len(states)), key=lambda x: len(states[x]))
		if len(states[max_index]) > len(largest):
			(states[max_index], largest) = (largest, states[max_index])
	sets = defaultdict(list)
	for cur_state in states:
		for (set_no, v) in cur_state.items():
			sets[set_no].append(v)
	for (set_no, states) in sets.items():
		if len(states) == 1 and set_no not in largest:
			largest[set_no] = states[0]
			continue
		if set_no in largest:
			states.append(largest.pop(set_no))
		total_flow = 0
		total_node_sum = 0
		for (node_depth, node_sum, node_flow) in states:
			flow_delta = mul(node_depth - depth[node], node_sum)
			total_flow = add(total_flow, flow_delta, node_flow)
			total_node_sum += node_sum
		set_score = 0
		for (node_depth, node_sum, node_flow) in states:
			node_flow = add(mul(node_depth - depth[node], node_sum), node_flow)
			diff = mul(sub(total_flow, node_flow), node_sum)
			set_score = add(set_score, diff)
		score[set_no] = add(score[set_no], set_score)
		largest[set_no] = (depth[node], total_node_sum, total_flow)
	state[node] = largest
print(*(score[i] for i in range(q)), sep='\n')


from collections import Counter, defaultdict
MOD = 10 ** 9 + 7

def read_row():
	return (int(x) for x in input().split())

def mul(x, y):
	return x * y % MOD

def add(*args):
	return sum(args) % MOD

def sub(x, y):
	return (x - y) % MOD
(n, q) = read_row()
adj_list = defaultdict(list)
for _ in range(n - 1):
	(u, v) = read_row()
	adj_list[u].append(v)
	adj_list[v].append(u)
elements = {v: set() for v in adj_list}
for set_no in range(q):
	read_row()
	for x in read_row():
		elements[x].add(set_no)
root = next(iter(adj_list))
current = [root]
current_depth = 0
order = []
parent = {root: None}
depth = {root: current_depth}
while current:
	current_depth += 1
	order.extend(current)
	nxt = []
	for node in current:
		for neighbor in adj_list[node]:
			if neighbor not in parent:
				parent[neighbor] = node
				depth[neighbor] = current_depth
				nxt.append(neighbor)
	current = nxt
score = Counter()
state = {}
for node in reversed(order):
	states = [state[neighbor] for neighbor in adj_list[node] if neighbor != parent[node]]
	largest = {s: [depth[node], node, 0] for s in elements[node]}
	if states:
		max_index = max(range(len(states)), key=lambda x: len(states[x]))
		if len(states[max_index]) > len(largest):
			(states[max_index], largest) = (largest, states[max_index])
	sets = defaultdict(list)
	for cur_state in states:
		for (set_no, v) in cur_state.items():
			sets[set_no].append(v)
	for (set_no, states) in sets.items():
		if len(states) == 1 and set_no not in largest:
			largest[set_no] = states[0]
			continue
		if set_no in largest:
			states.append(largest.pop(set_no))
		total_flow = 0
		total_node_sum = 0
		for (node_depth, node_sum, node_flow) in states:
			flow_delta = mul(node_depth - depth[node], node_sum)
			total_flow = add(total_flow, flow_delta, node_flow)
			total_node_sum += node_sum
		set_score = 0
		for (node_depth, node_sum, node_flow) in states:
			node_flow = add(mul(node_depth - depth[node], node_sum), node_flow)
			diff = mul(sub(total_flow, node_flow), node_sum)
			set_score = add(set_score, diff)
		score[set_no] = add(score[set_no], set_score)
		largest[set_no] = (depth[node], total_node_sum, total_flow)
	state[node] = largest
print(*(score[i] for i in range(q)), sep='\n')


from collections import Counter, defaultdict
MOD = 10 ** 9 + 7

def read_row():
	return (int(x) for x in input().split())

def mul(x, y):
	return x * y % MOD

def add(*args):
	return sum(args) % MOD

def sub(x, y):
	return (x - y) % MOD
(n, q) = read_row()
adj_list = defaultdict(list)
for _ in range(n - 1):
	(u, v) = read_row()
	adj_list[u].append(v)
	adj_list[v].append(u)
elements = {v: set() for v in adj_list}
for set_no in range(q):
	read_row()
	for x in read_row():
		elements[x].add(set_no)
root = next(iter(adj_list))
current = [root]
current_depth = 0
order = []
parent = {root: None}
depth = {root: current_depth}
while current:
	current_depth += 1
	order.extend(current)
	nxt = []
	for node in current:
		for neighbor in adj_list[node]:
			if neighbor not in parent:
				parent[neighbor] = node
				depth[neighbor] = current_depth
				nxt.append(neighbor)
	current = nxt
score = Counter()
state = {}
for node in reversed(order):
	states = [state[neighbor] for neighbor in adj_list[node] if neighbor != parent[node]]
	largest = {s: [depth[node], node, 0] for s in elements[node]}
	if states:
		max_index = max(range(len(states)), key=lambda x: len(states[x]))
		if len(states[max_index]) > len(largest):
			(states[max_index], largest) = (largest, states[max_index])
	sets = defaultdict(list)
	for cur_state in states:
		for (set_no, v) in cur_state.items():
			sets[set_no].append(v)
	for (set_no, states) in sets.items():
		if len(states) == 1 and set_no not in largest:
			largest[set_no] = states[0]
			continue
		if set_no in largest:
			states.append(largest.pop(set_no))
		total_flow = 0
		total_node_sum = 0
		for (node_depth, node_sum, node_flow) in states:
			flow_delta = mul(node_depth - depth[node], node_sum)
			total_flow = add(total_flow, flow_delta, node_flow)
			total_node_sum += node_sum
		set_score = 0
		for (node_depth, node_sum, node_flow) in states:
			node_flow = add(mul(node_depth - depth[node], node_sum), node_flow)
			diff = mul(sub(total_flow, node_flow), node_sum)
			set_score = add(set_score, diff)
		score[set_no] = add(score[set_no], set_score)
		largest[set_no] = (depth[node], total_node_sum, total_flow)
	state[node] = largest
print(*(score[i] for i in range(q)), sep='\n')


from collections import Counter, defaultdict
MOD = 10 ** 9 + 7

def read_row():
	return (int(x) for x in input().split())

def mul(x, y):
	return x * y % MOD

def add(*args):
	return sum(args) % MOD

def sub(x, y):
	return (x - y) % MOD
(n, q) = read_row()
adj_list = defaultdict(list)
for _ in range(n - 1):
	(u, v) = read_row()
	adj_list[u].append(v)
	adj_list[v].append(u)
elements = {v: set() for v in adj_list}
for set_no in range(q):
	read_row()
	for x in read_row():
		elements[x].add(set_no)
root = next(iter(adj_list))
current = [root]
current_depth = 0
order = []
parent = {root: None}
depth = {root: current_depth}
while current:
	current_depth += 1
	order.extend(current)
	nxt = []
	for node in current:
		for neighbor in adj_list[node]:
			if neighbor not in parent:
				parent[neighbor] = node
				depth[neighbor] = current_depth
				nxt.append(neighbor)
	current = nxt
score = Counter()
state = {}
for node in reversed(order):
	states = [state[neighbor] for neighbor in adj_list[node] if neighbor != parent[node]]
	largest = {s: [depth[node], node, 0] for s in elements[node]}
	if states:
		max_index = max(range(len(states)), key=lambda x: len(states[x]))
		if len(states[max_index]) > len(largest):
			(states[max_index], largest) = (largest, states[max_index])
	sets = defaultdict(list)
	for cur_state in states:
		for (set_no, v) in cur_state.items():
			sets[set_no].append(v)
	for (set_no, states) in sets.items():
		if len(states) == 1 and set_no not in largest:
			largest[set_no] = states[0]
			continue
		if set_no in largest:
			states.append(largest.pop(set_no))
		total_flow = 0
		total_node_sum = 0
		for (node_depth, node_sum, node_flow) in states:
			flow_delta = mul(node_depth - depth[node], node_sum)
			total_flow = add(total_flow, flow_delta, node_flow)
			total_node_sum += node_sum
		set_score = 0
		for (node_depth, node_sum, node_flow) in states:
			node_flow = add(mul(node_depth - depth[node], node_sum), node_flow)
			diff = mul(sub(total_flow, node_flow), node_sum)
			set_score = add(set_score, diff)
		score[set_no] = add(score[set_no], set_score)
		largest[set_no] = (depth[node], total_node_sum, total_flow)
	state[node] = largest
print(*(score[i] for i in range(q)), sep='\n')


def put(d, a, b):
	if a in d:
		d[a].append(b)
	else:
		d[a] = [b]

def main():
	for n in ns[::-1]:
		r = [tt[s] for s in tree[n] if s != f[n]]
		bst = {s: [gl[n], n, 0] for s in queries[n]}
		if r:
			o = max(range(len(r)), key=lambda a: len(r[a]))
			if len(r[o]) > len(bst):
				(r[o], bst) = (bst, r[o])
		ry = {}
		for ae in r:
			for (y, v) in ae.items():
				put(ry, y, v)
		for (y, r) in ry.items():
			(eq, z, t) = (0, 0, 0)
			if len(r) == 1 and y not in bst:
				bst[y] = r[0]
				continue
			if y in bst:
				r.append(bst.pop(y))
			for (d, v, c) in r:
				eq += (d - gl[n]) * v + c
				z += v
			for (d, v, c) in r:
				c += (d - gl[n]) * v
				diff = (eq - c) * v
				t += diff
			returns[y] += t
			bst[y] = (gl[n], z, eq)
		tt[n] = bst

def locate():
	q = [r]
	level = 0
	while q:
		level += 1
		tmp = []
		ns.extend(q)
		for n in q:
			for s in tree[n]:
				if s not in f:
					f[s] = n
					gl[s] = level
					tmp.append(s)
		q = tmp
tree = {}
tt = {}
(n, q) = map(int, input().split())
returns = [0] * q
for _ in range(n - 1):
	(a, b) = map(int, input().split())
	put(tree, a, b)
	put(tree, b, a)
queries = {a: set() for a in tree}
for y in range(q):
	input()
	for x in map(int, input().split()):
		queries[x].add(y)
r = next(iter(tree))
ns = []
f = {r: None}
gl = {r: 0}
locate()
main()
for s in returns:
	print(s % (10 ** 9 + 7))


from collections import Counter, defaultdict
MOD = 10 ** 9 + 7

def read_row():
	return (int(x) for x in input().split())

def mul(x, y):
	return x * y % MOD

def add(*args):
	return sum(args) % MOD

def sub(x, y):
	return (x - y) % MOD
(n, q) = read_row()
adj_list = defaultdict(list)
for _ in range(n - 1):
	(u, v) = read_row()
	adj_list[u].append(v)
	adj_list[v].append(u)
elements = {v: set() for v in adj_list}
for set_no in range(q):
	read_row()
	for x in read_row():
		elements[x].add(set_no)
root = next(iter(adj_list))
current = [root]
current_depth = 0
order = []
parent = {root: None}
depth = {root: current_depth}
while current:
	current_depth += 1
	order.extend(current)
	nxt = []
	for node in current:
		for neighbor in adj_list[node]:
			if neighbor not in parent:
				parent[neighbor] = node
				depth[neighbor] = current_depth
				nxt.append(neighbor)
	current = nxt
score = Counter()
state = {}
for node in reversed(order):
	states = [state[neighbor] for neighbor in adj_list[node] if neighbor != parent[node]]
	largest = {s: [depth[node], node, 0] for s in elements[node]}
	if states:
		max_index = max(range(len(states)), key=lambda x: len(states[x]))
		if len(states[max_index]) > len(largest):
			(states[max_index], largest) = (largest, states[max_index])
	sets = defaultdict(list)
	for cur_state in states:
		for (set_no, v) in cur_state.items():
			sets[set_no].append(v)
	for (set_no, states) in sets.items():
		if len(states) == 1 and set_no not in largest:
			largest[set_no] = states[0]
			continue
		if set_no in largest:
			states.append(largest.pop(set_no))
		total_flow = 0
		total_node_sum = 0
		for (node_depth, node_sum, node_flow) in states:
			flow_delta = mul(node_depth - depth[node], node_sum)
			total_flow = add(total_flow, flow_delta, node_flow)
			total_node_sum += node_sum
		set_score = 0
		for (node_depth, node_sum, node_flow) in states:
			node_flow = add(mul(node_depth - depth[node], node_sum), node_flow)
			diff = mul(sub(total_flow, node_flow), node_sum)
			set_score = add(set_score, diff)
		score[set_no] = add(score[set_no], set_score)
		largest[set_no] = (depth[node], total_node_sum, total_flow)
	state[node] = largest
print(*(score[i] for i in range(q)), sep='\n')


from collections import Counter, defaultdict
MOD = 10 ** 9 + 7

def read_row():
	return (int(x) for x in input().split())

def mul(x, y):
	return x * y % MOD

def add(*args):
	return sum(args) % MOD

def sub(x, y):
	return (x - y) % MOD
(n, q) = read_row()
adj_list = defaultdict(list)
for _ in range(n - 1):
	(u, v) = read_row()
	adj_list[u].append(v)
	adj_list[v].append(u)
elements = {v: set() for v in adj_list}
for set_no in range(q):
	read_row()
	for x in read_row():
		elements[x].add(set_no)
root = next(iter(adj_list))
current = [root]
current_depth = 0
order = []
parent = {root: None}
depth = {root: current_depth}
while current:
	current_depth += 1
	order.extend(current)
	nxt = []
	for node in current:
		for neighbor in adj_list[node]:
			if neighbor not in parent:
				parent[neighbor] = node
				depth[neighbor] = current_depth
				nxt.append(neighbor)
	current = nxt
score = Counter()
state = {}
for node in reversed(order):
	states = [state[neighbor] for neighbor in adj_list[node] if neighbor != parent[node]]
	largest = {s: [depth[node], node, 0] for s in elements[node]}
	if states:
		max_index = max(range(len(states)), key=lambda x: len(states[x]))
		if len(states[max_index]) > len(largest):
			(states[max_index], largest) = (largest, states[max_index])
	sets = defaultdict(list)
	for cur_state in states:
		for (set_no, v) in cur_state.items():
			sets[set_no].append(v)
	for (set_no, states) in sets.items():
		if len(states) == 1 and set_no not in largest:
			largest[set_no] = states[0]
			continue
		if set_no in largest:
			states.append(largest.pop(set_no))
		total_flow = 0
		total_node_sum = 0
		for (node_depth, node_sum, node_flow) in states:
			flow_delta = mul(node_depth - depth[node], node_sum)
			total_flow = add(total_flow, flow_delta, node_flow)
			total_node_sum += node_sum
		set_score = 0
		for (node_depth, node_sum, node_flow) in states:
			node_flow = add(mul(node_depth - depth[node], node_sum), node_flow)
			diff = mul(sub(total_flow, node_flow), node_sum)
			set_score = add(set_score, diff)
		score[set_no] = add(score[set_no], set_score)
		largest[set_no] = (depth[node], total_node_sum, total_flow)
	state[node] = largest
print(*(score[i] for i in range(q)), sep='\n')


from collections import Counter, defaultdict
MOD = 10 ** 9 + 7

def read_row():
	return (int(x) for x in input().split())

def mul(x, y):
	return x * y % MOD

def add(*args):
	return sum(args) % MOD

def sub(x, y):
	return (x - y) % MOD
(n, q) = read_row()
adj_list = defaultdict(list)
for _ in range(n - 1):
	(u, v) = read_row()
	adj_list[u].append(v)
	adj_list[v].append(u)
elements = {v: set() for v in adj_list}
for set_no in range(q):
	read_row()
	for x in read_row():
		elements[x].add(set_no)
root = next(iter(adj_list))
current = [root]
current_depth = 0
order = []
parent = {root: None}
depth = {root: current_depth}
while current:
	current_depth += 1
	order.extend(current)
	nxt = []
	for node in current:
		for neighbor in adj_list[node]:
			if neighbor not in parent:
				parent[neighbor] = node
				depth[neighbor] = current_depth
				nxt.append(neighbor)
	current = nxt
score = Counter()
state = {}
for node in reversed(order):
	states = [state[neighbor] for neighbor in adj_list[node] if neighbor != parent[node]]
	largest = {s: [depth[node], node, 0] for s in elements[node]}
	if states:
		max_index = max(range(len(states)), key=lambda x: len(states[x]))
		if len(states[max_index]) > len(largest):
			(states[max_index], largest) = (largest, states[max_index])
	sets = defaultdict(list)
	for cur_state in states:
		for (set_no, v) in cur_state.items():
			sets[set_no].append(v)
	for (set_no, states) in sets.items():
		if len(states) == 1 and set_no not in largest:
			largest[set_no] = states[0]
			continue
		if set_no in largest:
			states.append(largest.pop(set_no))
		total_flow = 0
		total_node_sum = 0
		for (node_depth, node_sum, node_flow) in states:
			flow_delta = mul(node_depth - depth[node], node_sum)
			total_flow = add(total_flow, flow_delta, node_flow)
			total_node_sum += node_sum
		set_score = 0
		for (node_depth, node_sum, node_flow) in states:
			node_flow = add(mul(node_depth - depth[node], node_sum), node_flow)
			diff = mul(sub(total_flow, node_flow), node_sum)
			set_score = add(set_score, diff)
		score[set_no] = add(score[set_no], set_score)
		largest[set_no] = (depth[node], total_node_sum, total_flow)
	state[node] = largest
print(*(score[i] for i in range(q)), sep='\n')


from collections import Counter, defaultdict
MOD = 10 ** 9 + 7

def read_row():
	return (int(x) for x in input().split())

def mul(x, y):
	return x * y % MOD

def add(*args):
	return sum(args) % MOD

def sub(x, y):
	return (x - y) % MOD
(n, q) = read_row()
adj_list = defaultdict(list)
for _ in range(n - 1):
	(u, v) = read_row()
	adj_list[u].append(v)
	adj_list[v].append(u)
elements = {v: set() for v in adj_list}
for set_no in range(q):
	read_row()
	for x in read_row():
		elements[x].add(set_no)
root = next(iter(adj_list))
current = [root]
current_depth = 0
order = []
parent = {root: None}
depth = {root: current_depth}
while current:
	current_depth += 1
	order.extend(current)
	nxt = []
	for node in current:
		for neighbor in adj_list[node]:
			if neighbor not in parent:
				parent[neighbor] = node
				depth[neighbor] = current_depth
				nxt.append(neighbor)
	current = nxt
score = Counter()
state = {}
for node in reversed(order):
	states = [state[neighbor] for neighbor in adj_list[node] if neighbor != parent[node]]
	largest = {s: [depth[node], node, 0] for s in elements[node]}
	if states:
		max_index = max(range(len(states)), key=lambda x: len(states[x]))
		if len(states[max_index]) > len(largest):
			(states[max_index], largest) = (largest, states[max_index])
	sets = defaultdict(list)
	for cur_state in states:
		for (set_no, v) in cur_state.items():
			sets[set_no].append(v)
	for (set_no, states) in sets.items():
		if len(states) == 1 and set_no not in largest:
			largest[set_no] = states[0]
			continue
		if set_no in largest:
			states.append(largest.pop(set_no))
		total_flow = 0
		total_node_sum = 0
		for (node_depth, node_sum, node_flow) in states:
			flow_delta = mul(node_depth - depth[node], node_sum)
			total_flow = add(total_flow, flow_delta, node_flow)
			total_node_sum += node_sum
		set_score = 0
		for (node_depth, node_sum, node_flow) in states:
			node_flow = add(mul(node_depth - depth[node], node_sum), node_flow)
			diff = mul(sub(total_flow, node_flow), node_sum)
			set_score = add(set_score, diff)
		score[set_no] = add(score[set_no], set_score)
		largest[set_no] = (depth[node], total_node_sum, total_flow)
	state[node] = largest
print(*(score[i] for i in range(q)), sep='\n')


from collections import Counter, defaultdict
MOD = 10 ** 9 + 7

def read_row():
	return (int(x) for x in input().split())

def mul(x, y):
	return x * y % MOD

def add(*args):
	return sum(args) % MOD

def sub(x, y):
	return (x - y) % MOD
(n, q) = read_row()
adj_list = defaultdict(list)
for _ in range(n - 1):
	(u, v) = read_row()
	adj_list[u].append(v)
	adj_list[v].append(u)
elements = {v: set() for v in adj_list}
for set_no in range(q):
	read_row()
	for x in read_row():
		elements[x].add(set_no)
root = next(iter(adj_list))
current = [root]
current_depth = 0
order = []
parent = {root: None}
depth = {root: current_depth}
while current:
	current_depth += 1
	order.extend(current)
	nxt = []
	for node in current:
		for neighbor in adj_list[node]:
			if neighbor not in parent:
				parent[neighbor] = node
				depth[neighbor] = current_depth
				nxt.append(neighbor)
	current = nxt
score = Counter()
state = {}
for node in reversed(order):
	states = [state[neighbor] for neighbor in adj_list[node] if neighbor != parent[node]]
	largest = {s: [depth[node], node, 0] for s in elements[node]}
	if states:
		max_index = max(range(len(states)), key=lambda x: len(states[x]))
		if len(states[max_index]) > len(largest):
			(states[max_index], largest) = (largest, states[max_index])
	sets = defaultdict(list)
	for cur_state in states:
		for (set_no, v) in cur_state.items():
			sets[set_no].append(v)
	for (set_no, states) in sets.items():
		if len(states) == 1 and set_no not in largest:
			largest[set_no] = states[0]
			continue
		if set_no in largest:
			states.append(largest.pop(set_no))
		total_flow = 0
		total_node_sum = 0
		for (node_depth, node_sum, node_flow) in states:
			flow_delta = mul(node_depth - depth[node], node_sum)
			total_flow = add(total_flow, flow_delta, node_flow)
			total_node_sum += node_sum
		set_score = 0
		for (node_depth, node_sum, node_flow) in states:
			node_flow = add(mul(node_depth - depth[node], node_sum), node_flow)
			diff = mul(sub(total_flow, node_flow), node_sum)
			set_score = add(set_score, diff)
		score[set_no] = add(score[set_no], set_score)
		largest[set_no] = (depth[node], total_node_sum, total_flow)
	state[node] = largest
print(*(score[i] for i in range(q)), sep='\n')


from collections import Counter, defaultdict
MOD = 10 ** 9 + 7

def read_row():
	return (int(x) for x in input().split())

def mul(x, y):
	return x * y % MOD

def add(*args):
	return sum(args) % MOD

def sub(x, y):
	return (x - y) % MOD
(n, q) = read_row()
adj_list = defaultdict(list)
for _ in range(n - 1):
	(u, v) = read_row()
	adj_list[u].append(v)
	adj_list[v].append(u)
elements = {v: set() for v in adj_list}
for set_no in range(q):
	read_row()
	for x in read_row():
		elements[x].add(set_no)
root = next(iter(adj_list))
current = [root]
current_depth = 0
order = []
parent = {root: None}
depth = {root: current_depth}
while current:
	current_depth += 1
	order.extend(current)
	nxt = []
	for node in current:
		for neighbor in adj_list[node]:
			if neighbor not in parent:
				parent[neighbor] = node
				depth[neighbor] = current_depth
				nxt.append(neighbor)
	current = nxt
score = Counter()
state = {}
for node in reversed(order):
	states = [state[neighbor] for neighbor in adj_list[node] if neighbor != parent[node]]
	largest = {s: [depth[node], node, 0] for s in elements[node]}
	if states:
		max_index = max(range(len(states)), key=lambda x: len(states[x]))
		if len(states[max_index]) > len(largest):
			(states[max_index], largest) = (largest, states[max_index])
	sets = defaultdict(list)
	for cur_state in states:
		for (set_no, v) in cur_state.items():
			sets[set_no].append(v)
	for (set_no, states) in sets.items():
		if len(states) == 1 and set_no not in largest:
			largest[set_no] = states[0]
			continue
		if set_no in largest:
			states.append(largest.pop(set_no))
		total_flow = 0
		total_node_sum = 0
		for (node_depth, node_sum, node_flow) in states:
			flow_delta = mul(node_depth - depth[node], node_sum)
			total_flow = add(total_flow, flow_delta, node_flow)
			total_node_sum += node_sum
		set_score = 0
		for (node_depth, node_sum, node_flow) in states:
			node_flow = add(mul(node_depth - depth[node], node_sum), node_flow)
			diff = mul(sub(total_flow, node_flow), node_sum)
			set_score = add(set_score, diff)
		score[set_no] = add(score[set_no], set_score)
		largest[set_no] = (depth[node], total_node_sum, total_flow)
	state[node] = largest
print(*(score[i] for i in range(q)), sep='\n')


from collections import Counter, defaultdict
MOD = 10 ** 9 + 7

def read_row():
	return (int(x) for x in input().split())

def mul(x, y):
	return x * y % MOD

def add(*args):
	return sum(args) % MOD

def sub(x, y):
	return (x - y) % MOD
(n, q) = read_row()
adj_list = defaultdict(list)
for _ in range(n - 1):
	(u, v) = read_row()
	adj_list[u].append(v)
	adj_list[v].append(u)
elements = {v: set() for v in adj_list}
for set_no in range(q):
	read_row()
	for x in read_row():
		elements[x].add(set_no)
root = next(iter(adj_list))
current = [root]
current_depth = 0
order = []
parent = {root: None}
depth = {root: current_depth}
while current:
	current_depth += 1
	order.extend(current)
	nxt = []
	for node in current:
		for neighbor in adj_list[node]:
			if neighbor not in parent:
				parent[neighbor] = node
				depth[neighbor] = current_depth
				nxt.append(neighbor)
	current = nxt
score = Counter()
state = {}
for node in reversed(order):
	states = [state[neighbor] for neighbor in adj_list[node] if neighbor != parent[node]]
	largest = {s: [depth[node], node, 0] for s in elements[node]}
	if states:
		max_index = max(range(len(states)), key=lambda x: len(states[x]))
		if len(states[max_index]) > len(largest):
			(states[max_index], largest) = (largest, states[max_index])
	sets = defaultdict(list)
	for cur_state in states:
		for (set_no, v) in cur_state.items():
			sets[set_no].append(v)
	for (set_no, states) in sets.items():
		if len(states) == 1 and set_no not in largest:
			largest[set_no] = states[0]
			continue
		if set_no in largest:
			states.append(largest.pop(set_no))
		total_flow = 0
		total_node_sum = 0
		for (node_depth, node_sum, node_flow) in states:
			flow_delta = mul(node_depth - depth[node], node_sum)
			total_flow = add(total_flow, flow_delta, node_flow)
			total_node_sum += node_sum
		set_score = 0
		for (node_depth, node_sum, node_flow) in states:
			node_flow = add(mul(node_depth - depth[node], node_sum), node_flow)
			diff = mul(sub(total_flow, node_flow), node_sum)
			set_score = add(set_score, diff)
		score[set_no] = add(score[set_no], set_score)
		largest[set_no] = (depth[node], total_node_sum, total_flow)
	state[node] = largest
print(*(score[i] for i in range(q)), sep='\n')


from collections import Counter, defaultdict
MOD = 10 ** 9 + 7

def read_row():
	return (int(x) for x in input().split())

def mul(x, y):
	return x * y % MOD

def add(*args):
	return sum(args) % MOD

def sub(x, y):
	return (x - y) % MOD
(n, q) = read_row()
adj_list = defaultdict(list)
for _ in range(n - 1):
	(u, v) = read_row()
	adj_list[u].append(v)
	adj_list[v].append(u)
elements = {v: set() for v in adj_list}
for set_no in range(q):
	read_row()
	for x in read_row():
		elements[x].add(set_no)
root = next(iter(adj_list))
current = [root]
current_depth = 0
order = []
parent = {root: None}
depth = {root: current_depth}
while current:
	current_depth += 1
	order.extend(current)
	nxt = []
	for node in current:
		for neighbor in adj_list[node]:
			if neighbor not in parent:
				parent[neighbor] = node
				depth[neighbor] = current_depth
				nxt.append(neighbor)
	current = nxt
score = Counter()
state = {}
for node in reversed(order):
	states = [state[neighbor] for neighbor in adj_list[node] if neighbor != parent[node]]
	largest = {s: [depth[node], node, 0] for s in elements[node]}
	if states:
		max_index = max(range(len(states)), key=lambda x: len(states[x]))
		if len(states[max_index]) > len(largest):
			(states[max_index], largest) = (largest, states[max_index])
	sets = defaultdict(list)
	for cur_state in states:
		for (set_no, v) in cur_state.items():
			sets[set_no].append(v)
	for (set_no, states) in sets.items():
		if len(states) == 1 and set_no not in largest:
			largest[set_no] = states[0]
			continue
		if set_no in largest:
			states.append(largest.pop(set_no))
		total_flow = 0
		total_node_sum = 0
		for (node_depth, node_sum, node_flow) in states:
			flow_delta = mul(node_depth - depth[node], node_sum)
			total_flow = add(total_flow, flow_delta, node_flow)
			total_node_sum += node_sum
		set_score = 0
		for (node_depth, node_sum, node_flow) in states:
			node_flow = add(mul(node_depth - depth[node], node_sum), node_flow)
			diff = mul(sub(total_flow, node_flow), node_sum)
			set_score = add(set_score, diff)
		score[set_no] = add(score[set_no], set_score)
		largest[set_no] = (depth[node], total_node_sum, total_flow)
	state[node] = largest
print(*(score[i] for i in range(q)), sep='\n')


from collections import Counter, defaultdict
MOD = 10 ** 9 + 7

def read_row():
	return (int(x) for x in input().split())

def mul(x, y):
	return x * y % MOD

def add(*args):
	return sum(args) % MOD

def sub(x, y):
	return (x - y) % MOD
(n, q) = read_row()
adj_list = defaultdict(list)
for _ in range(n - 1):
	(u, v) = read_row()
	adj_list[u].append(v)
	adj_list[v].append(u)
elements = {v: set() for v in adj_list}
for set_no in range(q):
	read_row()
	for x in read_row():
		elements[x].add(set_no)
root = next(iter(adj_list))
current = [root]
current_depth = 0
order = []
parent = {root: None}
depth = {root: current_depth}
while current:
	current_depth += 1
	order.extend(current)
	nxt = []
	for node in current:
		for neighbor in adj_list[node]:
			if neighbor not in parent:
				parent[neighbor] = node
				depth[neighbor] = current_depth
				nxt.append(neighbor)
	current = nxt
score = Counter()
state = {}
for node in reversed(order):
	states = [state[neighbor] for neighbor in adj_list[node] if neighbor != parent[node]]
	largest = {s: [depth[node], node, 0] for s in elements[node]}
	if states:
		max_index = max(range(len(states)), key=lambda x: len(states[x]))
		if len(states[max_index]) > len(largest):
			(states[max_index], largest) = (largest, states[max_index])
	sets = defaultdict(list)
	for cur_state in states:
		for (set_no, v) in cur_state.items():
			sets[set_no].append(v)
	for (set_no, states) in sets.items():
		if len(states) == 1 and set_no not in largest:
			largest[set_no] = states[0]
			continue
		if set_no in largest:
			states.append(largest.pop(set_no))
		total_flow = 0
		total_node_sum = 0
		for (node_depth, node_sum, node_flow) in states:
			flow_delta = mul(node_depth - depth[node], node_sum)
			total_flow = add(total_flow, flow_delta, node_flow)
			total_node_sum += node_sum
		set_score = 0
		for (node_depth, node_sum, node_flow) in states:
			node_flow = add(mul(node_depth - depth[node], node_sum), node_flow)
			diff = mul(sub(total_flow, node_flow), node_sum)
			set_score = add(set_score, diff)
		score[set_no] = add(score[set_no], set_score)
		largest[set_no] = (depth[node], total_node_sum, total_flow)
	state[node] = largest
print(*(score[i] for i in range(q)), sep='\n')


from collections import Counter, defaultdict
MOD = 10 ** 9 + 7

def read_row():
	return (int(x) for x in input().split())

def mul(x, y):
	return x * y % MOD

def add(*args):
	return sum(args) % MOD

def sub(x, y):
	return (x - y) % MOD
(n, q) = read_row()
adj_list = defaultdict(list)
for _ in range(n - 1):
	(u, v) = read_row()
	adj_list[u].append(v)
	adj_list[v].append(u)
elements = {v: set() for v in adj_list}
for set_no in range(q):
	read_row()
	for x in read_row():
		elements[x].add(set_no)
root = next(iter(adj_list))
current = [root]
current_depth = 0
order = []
parent = {root: None}
depth = {root: current_depth}
while current:
	current_depth += 1
	order.extend(current)
	nxt = []
	for node in current:
		for neighbor in adj_list[node]:
			if neighbor not in parent:
				parent[neighbor] = node
				depth[neighbor] = current_depth
				nxt.append(neighbor)
	current = nxt
score = Counter()
state = {}
for node in reversed(order):
	states = [state[neighbor] for neighbor in adj_list[node] if neighbor != parent[node]]
	largest = {s: [depth[node], node, 0] for s in elements[node]}
	if states:
		max_index = max(range(len(states)), key=lambda x: len(states[x]))
		if len(states[max_index]) > len(largest):
			(states[max_index], largest) = (largest, states[max_index])
	sets = defaultdict(list)
	for cur_state in states:
		for (set_no, v) in cur_state.items():
			sets[set_no].append(v)
	for (set_no, states) in sets.items():
		if len(states) == 1 and set_no not in largest:
			largest[set_no] = states[0]
			continue
		if set_no in largest:
			states.append(largest.pop(set_no))
		total_flow = 0
		total_node_sum = 0
		for (node_depth, node_sum, node_flow) in states:
			flow_delta = mul(node_depth - depth[node], node_sum)
			total_flow = add(total_flow, flow_delta, node_flow)
			total_node_sum += node_sum
		set_score = 0
		for (node_depth, node_sum, node_flow) in states:
			node_flow = add(mul(node_depth - depth[node], node_sum), node_flow)
			diff = mul(sub(total_flow, node_flow), node_sum)
			set_score = add(set_score, diff)
		score[set_no] = add(score[set_no], set_score)
		largest[set_no] = (depth[node], total_node_sum, total_flow)
	state[node] = largest
print(*(score[i] for i in range(q)), sep='\n')


from collections import Counter, defaultdict
MOD = 10 ** 9 + 7

def read_row():
	return (int(x) for x in input().split())

def mul(x, y):
	return x * y % MOD

def add(*args):
	return sum(args) % MOD

def sub(x, y):
	return (x - y) % MOD
(n, q) = read_row()
ad_list = defaultdict(list)
for _ in range(n - 1):
	(u, v) = read_row()
	ad_list[u].append(v)
	ad_list[v].append(u)
elements = {v: set() for v in ad_list}
for set_no in range(q):
	read_row()
	for x in read_row():
		elements[x].add(set_no)
root = next(iter(ad_list))
current = [root]
current_depth = 0
order = []
parent = {root: None}
depth = {root: current_depth}
while current:
	current_depth += 1
	order.extend(current)
	nxt = []
	for node in current:
		for neighbor in ad_list[node]:
			if neighbor not in parent:
				parent[neighbor] = node
				depth[neighbor] = current_depth
				nxt.append(neighbor)
	current = nxt
score = Counter()
state = {}
for node in reversed(order):
	states = [state[neighbor] for neighbor in ad_list[node] if neighbor != parent[node]]
	largest = {s: [depth[node], node, 0] for s in elements[node]}
	if states:
		max_index = max(range(len(states)), key=lambda x: len(states[x]))
		if len(states[max_index]) > len(largest):
			(states[max_index], largest) = (largest, states[max_index])
	sets = defaultdict(list)
	for cur_state in states:
		for (set_no, v) in cur_state.items():
			sets[set_no].append(v)
	for (set_no, states) in sets.items():
		if len(states) == 1 and set_no not in largest:
			largest[set_no] = states[0]
			continue
		if set_no in largest:
			states.append(largest.pop(set_no))
		total_flow = 0
		total_node_sum = 0
		for (node_depth, node_sum, node_flow) in states:
			flow_delta = mul(node_depth - depth[node], node_sum)
			total_flow = add(total_flow, flow_delta, node_flow)
			total_node_sum += node_sum
		set_score = 0
		for (node_depth, node_sum, node_flow) in states:
			node_flow = add(mul(node_depth - depth[node], node_sum), node_flow)
			diff = mul(sub(total_flow, node_flow), node_sum)
			set_score = add(set_score, diff)
		score[set_no] = add(score[set_no], set_score)
		largest[set_no] = (depth[node], total_node_sum, total_flow)
	state[node] = largest
print(*(score[i] for i in range(q)), sep='\n')


from collections import Counter, defaultdict
MOD = 10 ** 9 + 7

def read_row():
	return (int(x) for x in input().split())

def mul(x, y):
	return x * y % MOD

def add(*args):
	return sum(args) % MOD

def sub(x, y):
	return (x - y) % MOD
(n, q) = read_row()
adj_list = defaultdict(list)
for _ in range(n - 1):
	(u, v) = read_row()
	adj_list[u].append(v)
	adj_list[v].append(u)
elements = {v: set() for v in adj_list}
for set_no in range(q):
	read_row()
	for x in read_row():
		elements[x].add(set_no)
root = next(iter(adj_list))
current = [root]
current_depth = 0
order = []
parent = {root: None}
depth = {root: current_depth}
while current:
	current_depth += 1
	order.extend(current)
	nxt = []
	for node in current:
		for neighbor in adj_list[node]:
			if neighbor not in parent:
				parent[neighbor] = node
				depth[neighbor] = current_depth
				nxt.append(neighbor)
	current = nxt
score = Counter()
state = {}
for node in reversed(order):
	states = [state[neighbor] for neighbor in adj_list[node] if neighbor != parent[node]]
	largest = {s: [depth[node], node, 0] for s in elements[node]}
	if states:
		max_index = max(range(len(states)), key=lambda x: len(states[x]))
		if len(states[max_index]) > len(largest):
			(states[max_index], largest) = (largest, states[max_index])
	sets = defaultdict(list)
	for cur_state in states:
		for (set_no, v) in cur_state.items():
			sets[set_no].append(v)
	for (set_no, states) in sets.items():
		if len(states) == 1 and set_no not in largest:
			largest[set_no] = states[0]
			continue
		if set_no in largest:
			states.append(largest.pop(set_no))
		total_flow = 0
		total_node_sum = 0
		for (node_depth, node_sum, node_flow) in states:
			flow_delta = mul(node_depth - depth[node], node_sum)
			total_flow = add(total_flow, flow_delta, node_flow)
			total_node_sum += node_sum
		set_score = 0
		for (node_depth, node_sum, node_flow) in states:
			node_flow = add(mul(node_depth - depth[node], node_sum), node_flow)
			diff = mul(sub(total_flow, node_flow), node_sum)
			set_score = add(set_score, diff)
		score[set_no] = add(score[set_no], set_score)
		largest[set_no] = (depth[node], total_node_sum, total_flow)
	state[node] = largest
print(*(score[i] for i in range(q)), sep='\n')


from collections import Counter, defaultdict
MOD = 10 ** 9 + 7

def read_row():
	return (int(x) for x in input().split())

def mul(x, y):
	return x * y % MOD

def add(*args):
	return sum(args) % MOD

def sub(x, y):
	return (x - y) % MOD
(n, q) = read_row()
adj_list = defaultdict(list)
for _ in range(n - 1):
	(u, v) = read_row()
	adj_list[u].append(v)
	adj_list[v].append(u)
elements = {v: set() for v in adj_list}
for set_no in range(q):
	read_row()
	for x in read_row():
		elements[x].add(set_no)
root = next(iter(adj_list))
current = [root]
current_depth = 0
order = []
parent = {root: None}
depth = {root: current_depth}
while current:
	current_depth += 1
	order.extend(current)
	nxt = []
	for node in current:
		for neighbor in adj_list[node]:
			if neighbor not in parent:
				parent[neighbor] = node
				depth[neighbor] = current_depth
				nxt.append(neighbor)
	current = nxt
score = Counter()
state = {}
for node in reversed(order):
	states = [state[neighbor] for neighbor in adj_list[node] if neighbor != parent[node]]
	largest = {s: [depth[node], node, 0] for s in elements[node]}
	if states:
		max_index = max(range(len(states)), key=lambda x: len(states[x]))
		if len(states[max_index]) > len(largest):
			(states[max_index], largest) = (largest, states[max_index])
	sets = defaultdict(list)
	for cur_state in states:
		for (set_no, v) in cur_state.items():
			sets[set_no].append(v)
	for (set_no, states) in sets.items():
		if len(states) == 1 and set_no not in largest:
			largest[set_no] = states[0]
			continue
		if set_no in largest:
			states.append(largest.pop(set_no))
		total_flow = 0
		total_node_sum = 0
		for (node_depth, node_sum, node_flow) in states:
			flow_delta = mul(node_depth - depth[node], node_sum)
			total_flow = add(total_flow, flow_delta, node_flow)
			total_node_sum += node_sum
		set_score = 0
		for (node_depth, node_sum, node_flow) in states:
			node_flow = add(mul(node_depth - depth[node], node_sum), node_flow)
			diff = mul(sub(total_flow, node_flow), node_sum)
			set_score = add(set_score, diff)
		score[set_no] = add(score[set_no], set_score)
		largest[set_no] = (depth[node], total_node_sum, total_flow)
	state[node] = largest
print(*(score[i] for i in range(q)), sep='\n')


from collections import Counter, defaultdict
MOD = 10 ** 9 + 7

def read_row():
	return (int(x) for x in input().split())

def mul(x, y):
	return x * y % MOD

def add(*args):
	return sum(args) % MOD

def sub(x, y):
	return (x - y) % MOD
(n, q) = read_row()
adj_list = defaultdict(list)
for _ in range(n - 1):
	(u, v) = read_row()
	adj_list[u].append(v)
	adj_list[v].append(u)
elements = {v: set() for v in adj_list}
for set_no in range(q):
	read_row()
	for x in read_row():
		elements[x].add(set_no)
root = next(iter(adj_list))
current = [root]
current_depth = 0
order = []
parent = {root: None}
depth = {root: current_depth}
while current:
	current_depth += 1
	order.extend(current)
	nxt = []
	for node in current:
		for neighbor in adj_list[node]:
			if neighbor not in parent:
				parent[neighbor] = node
				depth[neighbor] = current_depth
				nxt.append(neighbor)
	current = nxt
score = Counter()
state = {}
for node in reversed(order):
	states = [state[neighbor] for neighbor in adj_list[node] if neighbor != parent[node]]
	largest = {s: [depth[node], node, 0] for s in elements[node]}
	if states:
		max_index = max(range(len(states)), key=lambda x: len(states[x]))
		if len(states[max_index]) > len(largest):
			(states[max_index], largest) = (largest, states[max_index])
	sets = defaultdict(list)
	for cur_state in states:
		for (set_no, v) in cur_state.items():
			sets[set_no].append(v)
	for (set_no, states) in sets.items():
		if len(states) == 1 and set_no not in largest:
			largest[set_no] = states[0]
			continue
		if set_no in largest:
			states.append(largest.pop(set_no))
		total_flow = 0
		total_node_sum = 0
		for (node_depth, node_sum, node_flow) in states:
			flow_delta = mul(node_depth - depth[node], node_sum)
			total_flow = add(total_flow, flow_delta, node_flow)
			total_node_sum += node_sum
		set_score = 0
		for (node_depth, node_sum, node_flow) in states:
			node_flow = add(mul(node_depth - depth[node], node_sum), node_flow)
			diff = mul(sub(total_flow, node_flow), node_sum)
			set_score = add(set_score, diff)
		score[set_no] = add(score[set_no], set_score)
		largest[set_no] = (depth[node], total_node_sum, total_flow)
	state[node] = largest
print(*(score[i] for i in range(q)), sep='\n')


from collections import Counter, defaultdict
MOD = 10 ** 9 + 7

def read_row():
	return (int(x) for x in input().split())

def mul(x, y):
	return x * y % MOD

def add(*args):
	return sum(args) % MOD

def sub(x, y):
	return (x - y) % MOD
(n, q) = read_row()
adj_list = defaultdict(list)
for _ in range(n - 1):
	(u, v) = read_row()
	adj_list[u].append(v)
	adj_list[v].append(u)
elements = {v: set() for v in adj_list}
for set_no in range(q):
	read_row()
	for x in read_row():
		elements[x].add(set_no)
root = next(iter(adj_list))
current = [root]
current_depth = 0
order = []
parent = {root: None}
depth = {root: current_depth}
while current:
	current_depth += 1
	order.extend(current)
	nxt = []
	for node in current:
		for neighbor in adj_list[node]:
			if neighbor not in parent:
				parent[neighbor] = node
				depth[neighbor] = current_depth
				nxt.append(neighbor)
	current = nxt
score = Counter()
state = {}
for node in reversed(order):
	states = [state[neighbor] for neighbor in adj_list[node] if neighbor != parent[node]]
	largest = {s: [depth[node], node, 0] for s in elements[node]}
	if states:
		max_index = max(range(len(states)), key=lambda x: len(states[x]))
		if len(states[max_index]) > len(largest):
			(states[max_index], largest) = (largest, states[max_index])
	sets = defaultdict(list)
	for cur_state in states:
		for (set_no, v) in cur_state.items():
			sets[set_no].append(v)
	for (set_no, states) in sets.items():
		if len(states) == 1 and set_no not in largest:
			largest[set_no] = states[0]
			continue
		if set_no in largest:
			states.append(largest.pop(set_no))
		total_flow = 0
		total_node_sum = 0
		for (node_depth, node_sum, node_flow) in states:
			flow_delta = mul(node_depth - depth[node], node_sum)
			total_flow = add(total_flow, flow_delta, node_flow)
			total_node_sum += node_sum
		set_score = 0
		for (node_depth, node_sum, node_flow) in states:
			node_flow = add(mul(node_depth - depth[node], node_sum), node_flow)
			diff = mul(sub(total_flow, node_flow), node_sum)
			set_score = add(set_score, diff)
		score[set_no] = add(score[set_no], set_score)
		largest[set_no] = (depth[node], total_node_sum, total_flow)
	state[node] = largest
print(*(score[i] for i in range(q)), sep='\n')


from collections import Counter, defaultdict
MOD = 10 ** 9 + 7

def read_row():
	return (int(x) for x in input().split())

def mul(x, y):
	return x * y % MOD

def add(*args):
	return sum(args) % MOD

def sub(x, y):
	return (x - y) % MOD
(n, q) = read_row()
adj_list = defaultdict(list)
for _ in range(n - 1):
	(u, v) = read_row()
	adj_list[u].append(v)
	adj_list[v].append(u)
elements = {v: set() for v in adj_list}
for set_no in range(q):
	read_row()
	for x in read_row():
		elements[x].add(set_no)
root = next(iter(adj_list))
current = [root]
current_depth = 0
order = []
parent = {root: None}
depth = {root: current_depth}
while current:
	current_depth += 1
	order.extend(current)
	nxt = []
	for node in current:
		for neighbor in adj_list[node]:
			if neighbor not in parent:
				parent[neighbor] = node
				depth[neighbor] = current_depth
				nxt.append(neighbor)
	current = nxt
score = Counter()
state = {}
for node in reversed(order):
	states = [state[neighbor] for neighbor in adj_list[node] if neighbor != parent[node]]
	largest = {s: [depth[node], node, 0] for s in elements[node]}
	if states:
		max_index = max(range(len(states)), key=lambda x: len(states[x]))
		if len(states[max_index]) > len(largest):
			(states[max_index], largest) = (largest, states[max_index])
	sets = defaultdict(list)
	for cur_state in states:
		for (set_no, v) in cur_state.items():
			sets[set_no].append(v)
	for (set_no, states) in sets.items():
		if len(states) == 1 and set_no not in largest:
			largest[set_no] = states[0]
			continue
		if set_no in largest:
			states.append(largest.pop(set_no))
		total_flow = 0
		total_node_sum = 0
		for (node_depth, node_sum, node_flow) in states:
			flow_delta = mul(node_depth - depth[node], node_sum)
			total_flow = add(total_flow, flow_delta, node_flow)
			total_node_sum += node_sum
		set_score = 0
		for (node_depth, node_sum, node_flow) in states:
			node_flow = add(mul(node_depth - depth[node], node_sum), node_flow)
			diff = mul(sub(total_flow, node_flow), node_sum)
			set_score = add(set_score, diff)
		score[set_no] = add(score[set_no], set_score)
		largest[set_no] = (depth[node], total_node_sum, total_flow)
	state[node] = largest
print(*(score[i] for i in range(q)), sep='\n')


from collections import Counter, defaultdict
MOD = 10 ** 9 + 7

def read_row():
	return (int(x) for x in input().split())

def mul(x, y):
	return x * y % MOD

def add(*args):
	return sum(args) % MOD

def sub(x, y):
	return (x - y) % MOD
(n, q) = read_row()
adj_list = defaultdict(list)
for _ in range(n - 1):
	(u, v) = read_row()
	adj_list[u].append(v)
	adj_list[v].append(u)
elements = {v: set() for v in adj_list}
for set_no in range(q):
	read_row()
	for x in read_row():
		elements[x].add(set_no)
root = next(iter(adj_list))
current = [root]
current_depth = 0
order = []
parent = {root: None}
depth = {root: current_depth}
while current:
	current_depth += 1
	order.extend(current)
	nxt = []
	for node in current:
		for neighbor in adj_list[node]:
			if neighbor not in parent:
				parent[neighbor] = node
				depth[neighbor] = current_depth
				nxt.append(neighbor)
	current = nxt
score = Counter()
state = {}
for node in reversed(order):
	states = [state[neighbor] for neighbor in adj_list[node] if neighbor != parent[node]]
	largest = {s: [depth[node], node, 0] for s in elements[node]}
	if states:
		max_index = max(range(len(states)), key=lambda x: len(states[x]))
		if len(states[max_index]) > len(largest):
			(states[max_index], largest) = (largest, states[max_index])
	sets = defaultdict(list)
	for cur_state in states:
		for (set_no, v) in cur_state.items():
			sets[set_no].append(v)
	for (set_no, states) in sets.items():
		if len(states) == 1 and set_no not in largest:
			largest[set_no] = states[0]
			continue
		if set_no in largest:
			states.append(largest.pop(set_no))
		total_flow = 0
		total_node_sum = 0
		for (node_depth, node_sum, node_flow) in states:
			flow_delta = mul(node_depth - depth[node], node_sum)
			total_flow = add(total_flow, flow_delta, node_flow)
			total_node_sum += node_sum
		set_score = 0
		for (node_depth, node_sum, node_flow) in states:
			node_flow = add(mul(node_depth - depth[node], node_sum), node_flow)
			diff = mul(sub(total_flow, node_flow), node_sum)
			set_score = add(set_score, diff)
		score[set_no] = add(score[set_no], set_score)
		largest[set_no] = (depth[node], total_node_sum, total_flow)
	state[node] = largest
print(*(score[i] for i in range(q)), sep='\n')


from collections import Counter, defaultdict
Mod = 10 ** 9 + 7

def read_row():
	return (int(x) for x in input().split())

def mul(x, y):
	return x * y % Mod

def add(*args):
	return sum(args) % Mod

def sub(x, y):
	return (x - y) % Mod
(n, q) = read_row()
adj_list = defaultdict(list)
for _ in range(n - 1):
	(u, v) = read_row()
	adj_list[u].append(v)
	adj_list[v].append(u)
elements = {v: set() for v in adj_list}
for set_no in range(q):
	read_row()
	for x in read_row():
		elements[x].add(set_no)
root = next(iter(adj_list))
current = [root]
current_depth = 0
order = []
parent = {root: None}
depth = {root: current_depth}
while current:
	current_depth += 1
	order.extend(current)
	nxt = []
	for node in current:
		for neighbour in adj_list[node]:
			if neighbour not in parent:
				parent[neighbour] = node
				depth[neighbour] = current_depth
				nxt.append(neighbour)
	current = nxt
score = Counter()
state = {}
for node in reversed(order):
	states = [state[neighbour] for neighbour in adj_list[node] if neighbour != parent[node]]
	largest = {s: [depth[node], node, 0] for s in elements[node]}
	if states:
		max_index = max(range(len(states)), key=lambda x: len(states[x]))
		if len(states[max_index]) > len(largest):
			(states[max_index], largest) = (largest, states[max_index])
	sets = defaultdict(list)
	for cur_state in states:
		for (set_no, v) in cur_state.items():
			sets[set_no].append(v)
	for (set_no, states) in sets.items():
		if len(states) == 1 and set_no not in largest:
			largest[set_no] = states[0]
			continue
		if set_no in largest:
			states.append(largest.pop(set_no))
		total_flow = 0
		total_node_sum = 0
		for (node_depth, node_sum, node_flow) in states:
			flow_delta = mul(node_depth - depth[node], node_sum)
			total_flow = add(total_flow, flow_delta, node_flow)
			total_node_sum += node_sum
		set_score = 0
		for (node_depth, node_sum, node_flow) in states:
			node_flow = add(mul(node_depth - depth[node], node_sum), node_flow)
			diff = mul(sub(total_flow, node_flow), node_sum)
			set_score = add(set_score, diff)
		score[set_no] = add(score[set_no], set_score)
		largest[set_no] = (depth[node], total_node_sum, total_flow)
	state[node] = largest
print(*(score[i] for i in range(q)), sep='\n')


from collections import Counter, defaultdict
MOD = 10 ** 9 + 7

def read_row():
	return (int(x) for x in input().split())

def mul(x, y):
	return x * y % MOD

def add(*args):
	return sum(args) % MOD

def sub(x, y):
	return (x - y) % MOD
(n, q) = read_row()
adj_list = defaultdict(list)
for _ in range(n - 1):
	(u, v) = read_row()
	adj_list[u].append(v)
	adj_list[v].append(u)
elements = {v: set() for v in adj_list}
for set_no in range(q):
	read_row()
	for x in read_row():
		elements[x].add(set_no)
root = next(iter(adj_list))
current = [root]
current_depth = 0
order = []
parent = {root: None}
depth = {root: current_depth}
while current:
	current_depth += 1
	order.extend(current)
	nxt = []
	for node in current:
		for neighbor in adj_list[node]:
			if neighbor not in parent:
				parent[neighbor] = node
				depth[neighbor] = current_depth
				nxt.append(neighbor)
	current = nxt
score = Counter()
state = {}
for node in reversed(order):
	states = [state[neighbor] for neighbor in adj_list[node] if neighbor != parent[node]]
	largest = {s: [depth[node], node, 0] for s in elements[node]}
	if states:
		max_index = max(range(len(states)), key=lambda x: len(states[x]))
		if len(states[max_index]) > len(largest):
			(states[max_index], largest) = (largest, states[max_index])
	sets = defaultdict(list)
	for cur_state in states:
		for (set_no, v) in cur_state.items():
			sets[set_no].append(v)
	for (set_no, states) in sets.items():
		if len(states) == 1 and set_no not in largest:
			largest[set_no] = states[0]
			continue
		if set_no in largest:
			states.append(largest.pop(set_no))
		total_flow = 0
		total_node_sum = 0
		for (node_depth, node_sum, node_flow) in states:
			flow_delta = mul(node_depth - depth[node], node_sum)
			total_flow = add(total_flow, flow_delta, node_flow)
			total_node_sum += node_sum
		set_score = 0
		for (node_depth, node_sum, node_flow) in states:
			node_flow = add(mul(node_depth - depth[node], node_sum), node_flow)
			diff = mul(sub(total_flow, node_flow), node_sum)
			set_score = add(set_score, diff)
		score[set_no] = add(score[set_no], set_score)
		largest[set_no] = (depth[node], total_node_sum, total_flow)
	state[node] = largest
print(*(score[i] for i in range(q)), sep='\n')


from collections import Counter, defaultdict
MOD = 10 ** 9 + 7

def read_row():
	return (int(x) for x in input().split())

def mul(x, y):
	return x * y % MOD

def add(*args):
	return sum(args) % MOD

def sub(x, y):
	return (x - y) % MOD
(n, q) = read_row()
adj_list = defaultdict(list)
for _ in range(n - 1):
	(u, v) = read_row()
	adj_list[u].append(v)
	adj_list[v].append(u)
elements = {v: set() for v in adj_list}
for set_no in range(q):
	read_row()
	for x in read_row():
		elements[x].add(set_no)
root = next(iter(adj_list))
current = [root]
current_depth = 0
order = []
parent = {root: None}
depth = {root: current_depth}
while current:
	current_depth += 1
	order.extend(current)
	nxt = []
	for node in current:
		for neighbor in adj_list[node]:
			if neighbor not in parent:
				parent[neighbor] = node
				depth[neighbor] = current_depth
				nxt.append(neighbor)
	current = nxt
score = Counter()
state = {}
for node in reversed(order):
	states = [state[neighbor] for neighbor in adj_list[node] if neighbor != parent[node]]
	largest = {s: [depth[node], node, 0] for s in elements[node]}
	if states:
		max_index = max(range(len(states)), key=lambda x: len(states[x]))
		if len(states[max_index]) > len(largest):
			(states[max_index], largest) = (largest, states[max_index])
	sets = defaultdict(list)
	for cur_state in states:
		for (set_no, v) in cur_state.items():
			sets[set_no].append(v)
	for (set_no, states) in sets.items():
		if len(states) == 1 and set_no not in largest:
			largest[set_no] = states[0]
			continue
		if set_no in largest:
			states.append(largest.pop(set_no))
		total_flow = 0
		total_node_sum = 0
		for (node_depth, node_sum, node_flow) in states:
			flow_delta = mul(node_depth - depth[node], node_sum)
			total_flow = add(total_flow, flow_delta, node_flow)
			total_node_sum += node_sum
		set_score = 0
		for (node_depth, node_sum, node_flow) in states:
			node_flow = add(mul(node_depth - depth[node], node_sum), node_flow)
			diff = mul(sub(total_flow, node_flow), node_sum)
			set_score = add(set_score, diff)
		score[set_no] = add(score[set_no], set_score)
		largest[set_no] = (depth[node], total_node_sum, total_flow)
	state[node] = largest
print(*(score[i] for i in range(q)), sep='\n')


from collections import Counter, defaultdict
MOD = 10 ** 9 + 7

def read_row():
	return (int(x) for x in input().split())

def mul(x, y):
	return x * y % MOD

def add(*args):
	return sum(args) % MOD

def sub(x, y):
	return (x - y) % MOD
(n, q) = read_row()
adj_list = defaultdict(list)
for _ in range(n - 1):
	(u, v) = read_row()
	adj_list[u].append(v)
	adj_list[v].append(u)
elements = {v: set() for v in adj_list}
for set_no in range(q):
	read_row()
	for x in read_row():
		elements[x].add(set_no)
root = next(iter(adj_list))
current = [root]
current_depth = 0
order = []
parent = {root: None}
depth = {root: current_depth}
while current:
	current_depth += 1
	order.extend(current)
	nxt = []
	for node in current:
		for neighbor in adj_list[node]:
			if neighbor not in parent:
				parent[neighbor] = node
				depth[neighbor] = current_depth
				nxt.append(neighbor)
	current = nxt
score = Counter()
state = {}
for node in reversed(order):
	states = [state[neighbor] for neighbor in adj_list[node] if neighbor != parent[node]]
	largest = {s: [depth[node], node, 0] for s in elements[node]}
	if states:
		max_index = max(range(len(states)), key=lambda x: len(states[x]))
		if len(states[max_index]) > len(largest):
			(states[max_index], largest) = (largest, states[max_index])
	sets = defaultdict(list)
	for cur_state in states:
		for (set_no, v) in cur_state.items():
			sets[set_no].append(v)
	for (set_no, states) in sets.items():
		if len(states) == 1 and set_no not in largest:
			largest[set_no] = states[0]
			continue
		if set_no in largest:
			states.append(largest.pop(set_no))
		total_flow = 0
		total_node_sum = 0
		for (node_depth, node_sum, node_flow) in states:
			flow_delta = mul(node_depth - depth[node], node_sum)
			total_flow = add(total_flow, flow_delta, node_flow)
			total_node_sum += node_sum
		set_score = 0
		for (node_depth, node_sum, node_flow) in states:
			node_flow = add(mul(node_depth - depth[node], node_sum), node_flow)
			diff = mul(sub(total_flow, node_flow), node_sum)
			set_score = add(set_score, diff)
		score[set_no] = add(score[set_no], set_score)
		largest[set_no] = (depth[node], total_node_sum, total_flow)
	state[node] = largest
print(*(score[i] for i in range(q)), sep='\n')


from collections import Counter, defaultdict
MOD = 10 ** 9 + 7

def read_row():
	return (int(x) for x in input().split())

def mul(x, y):
	return x * y % MOD

def add(*args):
	return sum(args) % MOD

def sub(x, y):
	return (x - y) % MOD
(n, q) = read_row()
adj_list = defaultdict(list)
for _ in range(n - 1):
	(u, v) = read_row()
	adj_list[u].append(v)
	adj_list[v].append(u)
elements = {v: set() for v in adj_list}
for set_no in range(q):
	read_row()
	for x in read_row():
		elements[x].add(set_no)
root = next(iter(adj_list))
current = [root]
current_depth = 0
order = []
parent = {root: None}
depth = {root: current_depth}
while current:
	current_depth += 1
	order.extend(current)
	nxt = []
	for node in current:
		for neighbor in adj_list[node]:
			if neighbor not in parent:
				parent[neighbor] = node
				depth[neighbor] = current_depth
				nxt.append(neighbor)
	current = nxt
score = Counter()
state = {}
for node in reversed(order):
	states = [state[neighbor] for neighbor in adj_list[node] if neighbor != parent[node]]
	largest = {s: [depth[node], node, 0] for s in elements[node]}
	if states:
		max_index = max(range(len(states)), key=lambda x: len(states[x]))
		if len(states[max_index]) > len(largest):
			(states[max_index], largest) = (largest, states[max_index])
	sets = defaultdict(list)
	for cur_state in states:
		for (set_no, v) in cur_state.items():
			sets[set_no].append(v)
	for (set_no, states) in sets.items():
		if len(states) == 1 and set_no not in largest:
			largest[set_no] = states[0]
			continue
		if set_no in largest:
			states.append(largest.pop(set_no))
		total_flow = 0
		total_node_sum = 0
		for (node_depth, node_sum, node_flow) in states:
			flow_delta = mul(node_depth - depth[node], node_sum)
			total_flow = add(total_flow, flow_delta, node_flow)
			total_node_sum += node_sum
		set_score = 0
		for (node_depth, node_sum, node_flow) in states:
			node_flow = add(mul(node_depth - depth[node], node_sum), node_flow)
			diff = mul(sub(total_flow, node_flow), node_sum)
			set_score = add(set_score, diff)
		score[set_no] = add(score[set_no], set_score)
		largest[set_no] = (depth[node], total_node_sum, total_flow)
	state[node] = largest
print(*(score[i] for i in range(q)), sep='\n')


from collections import Counter, defaultdict
MOD = 10 ** 9 + 7

def read_row():
	return (int(x) for x in input().split())

def mul(x, y):
	return x * y % MOD

def add(*args):
	return sum(args) % MOD

def sub(x, y):
	return (x - y) % MOD
(n, q) = read_row()
adj_list = defaultdict(list)
for _ in range(n - 1):
	(u, v) = read_row()
	adj_list[u].append(v)
	adj_list[v].append(u)
elements = {v: set() for v in adj_list}
for set_no in range(q):
	read_row()
	for x in read_row():
		elements[x].add(set_no)
root = next(iter(adj_list))
current = [root]
current_depth = 0
order = []
parent = {root: None}
depth = {root: current_depth}
while current:
	current_depth += 1
	order.extend(current)
	nxt = []
	for node in current:
		for neighbor in adj_list[node]:
			if neighbor not in parent:
				parent[neighbor] = node
				depth[neighbor] = current_depth
				nxt.append(neighbor)
	current = nxt
score = Counter()
state = {}
for node in reversed(order):
	states = [state[neighbor] for neighbor in adj_list[node] if neighbor != parent[node]]
	largest = {s: [depth[node], node, 0] for s in elements[node]}
	if states:
		max_index = max(range(len(states)), key=lambda x: len(states[x]))
		if len(states[max_index]) > len(largest):
			(states[max_index], largest) = (largest, states[max_index])
	sets = defaultdict(list)
	for cur_state in states:
		for (set_no, v) in cur_state.items():
			sets[set_no].append(v)
	for (set_no, states) in sets.items():
		if len(states) == 1 and set_no not in largest:
			largest[set_no] = states[0]
			continue
		if set_no in largest:
			states.append(largest.pop(set_no))
		total_flow = 0
		total_node_sum = 0
		for (node_depth, node_sum, node_flow) in states:
			flow_delta = mul(node_depth - depth[node], node_sum)
			total_flow = add(total_flow, flow_delta, node_flow)
			total_node_sum += node_sum
		set_score = 0
		for (node_depth, node_sum, node_flow) in states:
			node_flow = add(mul(node_depth - depth[node], node_sum), node_flow)
			diff = mul(sub(total_flow, node_flow), node_sum)
			set_score = add(set_score, diff)
		score[set_no] = add(score[set_no], set_score)
		largest[set_no] = (depth[node], total_node_sum, total_flow)
	state[node] = largest
print(*(score[i] for i in range(q)), sep='\n')


from collections import Counter, defaultdict
MOD = 10 ** 9 + 7

def read_row():
	return (int(x) for x in input().split())

def mul(x, y):
	return x * y % MOD

def add(*args):
	return sum(args) % MOD

def sub(x, y):
	return (x - y) % MOD
(n, q) = read_row()
adj_list = defaultdict(list)
for _ in range(n - 1):
	(u, v) = read_row()
	adj_list[u].append(v)
	adj_list[v].append(u)
elements = {v: set() for v in adj_list}
for set_no in range(q):
	read_row()
	for x in read_row():
		elements[x].add(set_no)
root = next(iter(adj_list))
current = [root]
current_depth = 0
order = []
parent = {root: None}
depth = {root: current_depth}
while current:
	current_depth += 1
	order.extend(current)
	nxt = []
	for node in current:
		for neighbor in adj_list[node]:
			if neighbor not in parent:
				parent[neighbor] = node
				depth[neighbor] = current_depth
				nxt.append(neighbor)
	current = nxt
score = Counter()
state = {}
for node in reversed(order):
	states = [state[neighbor] for neighbor in adj_list[node] if neighbor != parent[node]]
	largest = {s: [depth[node], node, 0] for s in elements[node]}
	if states:
		max_index = max(range(len(states)), key=lambda x: len(states[x]))
		if len(states[max_index]) > len(largest):
			(states[max_index], largest) = (largest, states[max_index])
	sets = defaultdict(list)
	for cur_state in states:
		for (set_no, v) in cur_state.items():
			sets[set_no].append(v)
	for (set_no, states) in sets.items():
		if len(states) == 1 and set_no not in largest:
			largest[set_no] = states[0]
			continue
		if set_no in largest:
			states.append(largest.pop(set_no))
		total_flow = 0
		total_node_sum = 0
		for (node_depth, node_sum, node_flow) in states:
			flow_delta = mul(node_depth - depth[node], node_sum)
			total_flow = add(total_flow, flow_delta, node_flow)
			total_node_sum += node_sum
		set_score = 0
		for (node_depth, node_sum, node_flow) in states:
			node_flow = add(mul(node_depth - depth[node], node_sum), node_flow)
			diff = mul(sub(total_flow, node_flow), node_sum)
			set_score = add(set_score, diff)
		score[set_no] = add(score[set_no], set_score)
		largest[set_no] = (depth[node], total_node_sum, total_flow)
	state[node] = largest
print(*(score[i] for i in range(q)), sep='\n')


from collections import Counter, defaultdict
MOD = 10 ** 9 + 7

def read_row():
	return (int(x) for x in input().split())

def mul(x, y):
	return x * y % MOD

def add(*args):
	return sum(args) % MOD

def sub(x, y):
	return (x - y) % MOD
(n, q) = read_row()
adj_list = defaultdict(list)
for _ in range(n - 1):
	(u, v) = read_row()
	adj_list[u].append(v)
	adj_list[v].append(u)
elements = {v: set() for v in adj_list}
for set_no in range(q):
	read_row()
	for x in read_row():
		elements[x].add(set_no)
root = next(iter(adj_list))
current = [root]
current_depth = 0
order = []
parent = {root: None}
depth = {root: current_depth}
while current:
	current_depth += 1
	order.extend(current)
	nxt = []
	for node in current:
		for neighbor in adj_list[node]:
			if neighbor not in parent:
				parent[neighbor] = node
				depth[neighbor] = current_depth
				nxt.append(neighbor)
	current = nxt
score = Counter()
state = {}
for node in reversed(order):
	states = [state[neighbor] for neighbor in adj_list[node] if neighbor != parent[node]]
	largest = {s: [depth[node], node, 0] for s in elements[node]}
	if states:
		max_index = max(range(len(states)), key=lambda x: len(states[x]))
		if len(states[max_index]) > len(largest):
			(states[max_index], largest) = (largest, states[max_index])
	sets = defaultdict(list)
	for cur_state in states:
		for (set_no, v) in cur_state.items():
			sets[set_no].append(v)
	for (set_no, states) in sets.items():
		if len(states) == 1 and set_no not in largest:
			largest[set_no] = states[0]
			continue
		if set_no in largest:
			states.append(largest.pop(set_no))
		total_flow = 0
		total_node_sum = 0
		for (node_depth, node_sum, node_flow) in states:
			flow_delta = mul(node_depth - depth[node], node_sum)
			total_flow = add(total_flow, flow_delta, node_flow)
			total_node_sum += node_sum
		set_score = 0
		for (node_depth, node_sum, node_flow) in states:
			node_flow = add(mul(node_depth - depth[node], node_sum), node_flow)
			diff = mul(sub(total_flow, node_flow), node_sum)
			set_score = add(set_score, diff)
		score[set_no] = add(score[set_no], set_score)
		largest[set_no] = (depth[node], total_node_sum, total_flow)
	state[node] = largest
print(*(score[i] for i in range(q)), sep='\n')


from collections import Counter, defaultdict
MOD = 10 ** 9 + 7

def read_row():
	return (int(x) for x in input().split())

def mul(x, y):
	return x * y % MOD

def add(*args):
	return sum(args) % MOD

def sub(x, y):
	return (x - y) % MOD
(n, q) = read_row()
adj_list = defaultdict(list)
for _ in range(n - 1):
	(u, v) = read_row()
	adj_list[u].append(v)
	adj_list[v].append(u)
elements = {v: set() for v in adj_list}
for set_no in range(q):
	read_row()
	for x in read_row():
		elements[x].add(set_no)
root = next(iter(adj_list))
current = [root]
current_depth = 0
order = []
parent = {root: None}
depth = {root: current_depth}
while current:
	current_depth += 1
	order.extend(current)
	nxt = []
	for node in current:
		for neighbor in adj_list[node]:
			if neighbor not in parent:
				parent[neighbor] = node
				depth[neighbor] = current_depth
				nxt.append(neighbor)
	current = nxt
score = Counter()
state = {}
for node in reversed(order):
	states = [state[neighbor] for neighbor in adj_list[node] if neighbor != parent[node]]
	largest = {s: [depth[node], node, 0] for s in elements[node]}
	if states:
		max_index = max(range(len(states)), key=lambda x: len(states[x]))
		if len(states[max_index]) > len(largest):
			(states[max_index], largest) = (largest, states[max_index])
	sets = defaultdict(list)
	for cur_state in states:
		for (set_no, v) in cur_state.items():
			sets[set_no].append(v)
	for (set_no, states) in sets.items():
		if len(states) == 1 and set_no not in largest:
			largest[set_no] = states[0]
			continue
		if set_no in largest:
			states.append(largest.pop(set_no))
		total_flow = 0
		total_node_sum = 0
		for (node_depth, node_sum, node_flow) in states:
			flow_delta = mul(node_depth - depth[node], node_sum)
			total_flow = add(total_flow, flow_delta, node_flow)
			total_node_sum += node_sum
		set_score = 0
		for (node_depth, node_sum, node_flow) in states:
			node_flow = add(mul(node_depth - depth[node], node_sum), node_flow)
			diff = mul(sub(total_flow, node_flow), node_sum)
			set_score = add(set_score, diff)
		score[set_no] = add(score[set_no], set_score)
		largest[set_no] = (depth[node], total_node_sum, total_flow)
	state[node] = largest
print(*(score[i] for i in range(q)), sep='\n')


from collections import Counter, defaultdict
MOD = 10 ** 9 + 7

def read_row():
	return (int(x) for x in input().split())

def mul(x, y):
	return x * y % MOD

def add(*args):
	return sum(args) % MOD

def sub(x, y):
	return (x - y) % MOD
(n, q) = read_row()
adj_list = defaultdict(list)
for _ in range(n - 1):
	(u, v) = read_row()
	adj_list[u].append(v)
	adj_list[v].append(u)
elements = {v: set() for v in adj_list}
for set_no in range(q):
	read_row()
	for x in read_row():
		elements[x].add(set_no)
root = next(iter(adj_list))
current = [root]
current_depth = 0
order = []
parent = {root: None}
depth = {root: current_depth}
while current:
	current_depth += 1
	order.extend(current)
	nxt = []
	for node in current:
		for neighbor in adj_list[node]:
			if neighbor not in parent:
				parent[neighbor] = node
				depth[neighbor] = current_depth
				nxt.append(neighbor)
	current = nxt
score = Counter()
state = {}
for node in reversed(order):
	states = [state[neighbor] for neighbor in adj_list[node] if neighbor != parent[node]]
	largest = {s: [depth[node], node, 0] for s in elements[node]}
	if states:
		max_index = max(range(len(states)), key=lambda x: len(states[x]))
		if len(states[max_index]) > len(largest):
			(states[max_index], largest) = (largest, states[max_index])
	sets = defaultdict(list)
	for cur_state in states:
		for (set_no, v) in cur_state.items():
			sets[set_no].append(v)
	for (set_no, states) in sets.items():
		if len(states) == 1 and set_no not in largest:
			largest[set_no] = states[0]
			continue
		if set_no in largest:
			states.append(largest.pop(set_no))
		total_flow = 0
		total_node_sum = 0
		for (node_depth, node_sum, node_flow) in states:
			flow_delta = mul(node_depth - depth[node], node_sum)
			total_flow = add(total_flow, flow_delta, node_flow)
			total_node_sum += node_sum
		set_score = 0
		for (node_depth, node_sum, node_flow) in states:
			node_flow = add(mul(node_depth - depth[node], node_sum), node_flow)
			diff = mul(sub(total_flow, node_flow), node_sum)
			set_score = add(set_score, diff)
		score[set_no] = add(score[set_no], set_score)
		largest[set_no] = (depth[node], total_node_sum, total_flow)
	state[node] = largest
print(*(score[i] for i in range(q)), sep='\n')


from collections import Counter, defaultdict
MOD = 10 ** 9 + 7

def read_row():
	return (int(x) for x in input().split())

def mul(x, y):
	return x * y % MOD

def add(*args):
	return sum(args) % MOD

def sub(x, y):
	return (x - y) % MOD
(n, q) = read_row()
adj_list = defaultdict(list)
for _ in range(n - 1):
	(u, v) = read_row()
	adj_list[u].append(v)
	adj_list[v].append(u)
elements = {v: set() for v in adj_list}
for set_no in range(q):
	read_row()
	for x in read_row():
		elements[x].add(set_no)
root = next(iter(adj_list))
current = [root]
current_depth = 0
order = []
parent = {root: None}
depth = {root: current_depth}
while current:
	current_depth += 1
	order.extend(current)
	nxt = []
	for node in current:
		for neighbor in adj_list[node]:
			if neighbor not in parent:
				parent[neighbor] = node
				depth[neighbor] = current_depth
				nxt.append(neighbor)
	current = nxt
score = Counter()
state = {}
for node in reversed(order):
	states = [state[neighbor] for neighbor in adj_list[node] if neighbor != parent[node]]
	largest = {s: [depth[node], node, 0] for s in elements[node]}
	if states:
		max_index = max(range(len(states)), key=lambda x: len(states[x]))
		if len(states[max_index]) > len(largest):
			(states[max_index], largest) = (largest, states[max_index])
	sets = defaultdict(list)
	for cur_state in states:
		for (set_no, v) in cur_state.items():
			sets[set_no].append(v)
	for (set_no, states) in sets.items():
		if len(states) == 1 and set_no not in largest:
			largest[set_no] = states[0]
			continue
		if set_no in largest:
			states.append(largest.pop(set_no))
		total_flow = 0
		total_node_sum = 0
		for (node_depth, node_sum, node_flow) in states:
			flow_delta = mul(node_depth - depth[node], node_sum)
			total_flow = add(total_flow, flow_delta, node_flow)
			total_node_sum += node_sum
		set_score = 0
		for (node_depth, node_sum, node_flow) in states:
			node_flow = add(mul(node_depth - depth[node], node_sum), node_flow)
			diff = mul(sub(total_flow, node_flow), node_sum)
			set_score = add(set_score, diff)
		score[set_no] = add(score[set_no], set_score)
		largest[set_no] = (depth[node], total_node_sum, total_flow)
	state[node] = largest
print(*(score[i] for i in range(q)), sep='\n')


from collections import Counter, defaultdict
MOD = 10 ** 9 + 7

def read_row():
	return (int(x) for x in input().split())

def mul(x, y):
	return x * y % MOD

def add(*args):
	return sum(args) % MOD

def sub(x, y):
	return (x - y) % MOD
(n, q) = read_row()
adj_list = defaultdict(list)
for _ in range(n - 1):
	(u, v) = read_row()
	adj_list[u].append(v)
	adj_list[v].append(u)
elements = {v: set() for v in adj_list}
for set_no in range(q):
	read_row()
	for x in read_row():
		elements[x].add(set_no)
root = next(iter(adj_list))
current = [root]
current_depth = 0
order = []
parent = {root: None}
depth = {root: current_depth}
while current:
	current_depth += 1
	order.extend(current)
	nxt = []
	for node in current:
		for neighbor in adj_list[node]:
			if neighbor not in parent:
				parent[neighbor] = node
				depth[neighbor] = current_depth
				nxt.append(neighbor)
	current = nxt
score = Counter()
state = {}
for node in reversed(order):
	states = [state[neighbor] for neighbor in adj_list[node] if neighbor != parent[node]]
	largest = {s: [depth[node], node, 0] for s in elements[node]}
	if states:
		max_index = max(range(len(states)), key=lambda x: len(states[x]))
		if len(states[max_index]) > len(largest):
			(states[max_index], largest) = (largest, states[max_index])
	sets = defaultdict(list)
	for cur_state in states:
		for (set_no, v) in cur_state.items():
			sets[set_no].append(v)
	for (set_no, states) in sets.items():
		if len(states) == 1 and set_no not in largest:
			largest[set_no] = states[0]
			continue
		if set_no in largest:
			states.append(largest.pop(set_no))
		total_flow = 0
		total_node_sum = 0
		for (node_depth, node_sum, node_flow) in states:
			flow_delta = mul(node_depth - depth[node], node_sum)
			total_flow = add(total_flow, flow_delta, node_flow)
			total_node_sum += node_sum
		set_score = 0
		for (node_depth, node_sum, node_flow) in states:
			node_flow = add(mul(node_depth - depth[node], node_sum), node_flow)
			diff = mul(sub(total_flow, node_flow), node_sum)
			set_score = add(set_score, diff)
		score[set_no] = add(score[set_no], set_score)
		largest[set_no] = (depth[node], total_node_sum, total_flow)
	state[node] = largest
print(*(score[i] for i in range(q)), sep='\n')


from collections import Counter, defaultdict
MOD = 10 ** 9 + 7

def read_row():
	return (int(x) for x in input().split())

def mul(x, y):
	return x * y % MOD

def add(*args):
	return sum(args) % MOD

def sub(x, y):
	return (x - y) % MOD
(n, q) = read_row()
adj_list = defaultdict(list)
for _ in range(n - 1):
	(u, v) = read_row()
	adj_list[u].append(v)
	adj_list[v].append(u)
elements = {v: set() for v in adj_list}
for set_no in range(q):
	read_row()
	for x in read_row():
		elements[x].add(set_no)
root = next(iter(adj_list))
current = [root]
current_depth = 0
order = []
parent = {root: None}
depth = {root: current_depth}
while current:
	current_depth += 1
	order.extend(current)
	nxt = []
	for node in current:
		for neighbor in adj_list[node]:
			if neighbor not in parent:
				parent[neighbor] = node
				depth[neighbor] = current_depth
				nxt.append(neighbor)
	current = nxt
score = Counter()
state = {}
for node in reversed(order):
	states = [state[neighbor] for neighbor in adj_list[node] if neighbor != parent[node]]
	largest = {s: [depth[node], node, 0] for s in elements[node]}
	if states:
		max_index = max(range(len(states)), key=lambda x: len(states[x]))
		if len(states[max_index]) > len(largest):
			(states[max_index], largest) = (largest, states[max_index])
	sets = defaultdict(list)
	for cur_state in states:
		for (set_no, v) in cur_state.items():
			sets[set_no].append(v)
	for (set_no, states) in sets.items():
		if len(states) == 1 and set_no not in largest:
			largest[set_no] = states[0]
			continue
		if set_no in largest:
			states.append(largest.pop(set_no))
		total_flow = 0
		total_node_sum = 0
		for (node_depth, node_sum, node_flow) in states:
			flow_delta = mul(node_depth - depth[node], node_sum)
			total_flow = add(total_flow, flow_delta, node_flow)
			total_node_sum += node_sum
		set_score = 0
		for (node_depth, node_sum, node_flow) in states:
			node_flow = add(mul(node_depth - depth[node], node_sum), node_flow)
			diff = mul(sub(total_flow, node_flow), node_sum)
			set_score = add(set_score, diff)
		score[set_no] = add(score[set_no], set_score)
		largest[set_no] = (depth[node], total_node_sum, total_flow)
	state[node] = largest
print(*(score[i] for i in range(q)), sep='\n')


def put(d, a, b):
	if a in d:
		d[a].append(b)
	else:
		d[a] = [b]

def main():
	for n in ns[::-1]:
		r = [tt[s] for s in tree[n] if s != f[n]]
		bst = {s: [gl[n], n, 0] for s in queries[n]}
		if r:
			o = max(range(len(r)), key=lambda a: len(r[a]))
			if len(r[o]) > len(bst):
				(r[o], bst) = (bst, r[o])
		ry = {}
		for ae in r:
			for (y, v) in ae.items():
				put(ry, y, v)
		for (y, r) in ry.items():
			(eq, z, t) = (0, 0, 0)
			if len(r) == 1 and y not in bst:
				bst[y] = r[0]
				continue
			if y in bst:
				r.append(bst.pop(y))
			for (d, v, c) in r:
				eq += (d - gl[n]) * v + c
				z += v
			for (d, v, c) in r:
				c += (d - gl[n]) * v
				diff = (eq - c) * v
				t += diff
			returns[y] += t
			bst[y] = (gl[n], z, eq)
		tt[n] = bst

def locate():
	q = [r]
	level = 0
	while q:
		level += 1
		tmp = []
		ns.extend(q)
		for n in q:
			for s in tree[n]:
				if s not in f:
					f[s] = n
					gl[s] = level
					tmp.append(s)
		q = tmp
tree = {}
tt = {}
(n, q) = map(int, input().split())
returns = [0] * q
for _ in range(n - 1):
	(a, b) = map(int, input().split())
	put(tree, a, b)
	put(tree, b, a)
queries = {a: set() for a in tree}
for y in range(q):
	input()
	for x in map(int, input().split()):
		queries[x].add(y)
r = next(iter(tree))
ns = []
f = {r: None}
gl = {r: 0}
locate()
main()
for s in returns:
	print(s % (10 ** 9 + 7))


from collections import Counter, defaultdict
MOD = 10 ** 9 + 7

def read_row():
	return (int(x) for x in input().split())

def mul(x, y):
	return x * y % MOD

def add(*args):
	return sum(args) % MOD

def sub(x, y):
	return (x - y) % MOD
(n, q) = read_row()
adj_list = defaultdict(list)
for _ in range(n - 1):
	(u, v) = read_row()
	adj_list[u].append(v)
	adj_list[v].append(u)
elements = {v: set() for v in adj_list}
for set_no in range(q):
	read_row()
	for x in read_row():
		elements[x].add(set_no)
root = next(iter(adj_list))
current = [root]
current_depth = 0
order = []
parent = {root: None}
depth = {root: current_depth}
while current:
	current_depth += 1
	order.extend(current)
	nxt = []
	for node in current:
		for neighbor in adj_list[node]:
			if neighbor not in parent:
				parent[neighbor] = node
				depth[neighbor] = current_depth
				nxt.append(neighbor)
	current = nxt
score = Counter()
state = {}
for node in reversed(order):
	states = [state[neighbor] for neighbor in adj_list[node] if neighbor != parent[node]]
	largest = {s: [depth[node], node, 0] for s in elements[node]}
	if states:
		max_index = max(range(len(states)), key=lambda x: len(states[x]))
		if len(states[max_index]) > len(largest):
			(states[max_index], largest) = (largest, states[max_index])
	sets = defaultdict(list)
	for cur_state in states:
		for (set_no, v) in cur_state.items():
			sets[set_no].append(v)
	for (set_no, states) in sets.items():
		if len(states) == 1 and set_no not in largest:
			largest[set_no] = states[0]
			continue
		if set_no in largest:
			states.append(largest.pop(set_no))
		total_flow = 0
		total_node_sum = 0
		for (node_depth, node_sum, node_flow) in states:
			flow_delta = mul(node_depth - depth[node], node_sum)
			total_flow = add(total_flow, flow_delta, node_flow)
			total_node_sum += node_sum
		set_score = 0
		for (node_depth, node_sum, node_flow) in states:
			node_flow = add(mul(node_depth - depth[node], node_sum), node_flow)
			diff = mul(sub(total_flow, node_flow), node_sum)
			set_score = add(set_score, diff)
		score[set_no] = add(score[set_no], set_score)
		largest[set_no] = (depth[node], total_node_sum, total_flow)
	state[node] = largest
print(*(score[i] for i in range(q)), sep='\n')


from collections import Counter, defaultdict
MOD = 10 ** 9 + 7

def read_row():
	return (int(x) for x in input().split())

def mul(x, y):
	return x * y % MOD

def add(*args):
	return sum(args) % MOD

def sub(x, y):
	return (x - y) % MOD
(n, q) = read_row()
adj_list = defaultdict(list)
for _ in range(n - 1):
	(u, v) = read_row()
	adj_list[u].append(v)
	adj_list[v].append(u)
elements = {v: set() for v in adj_list}
for set_no in range(q):
	read_row()
	for x in read_row():
		elements[x].add(set_no)
root = next(iter(adj_list))
current = [root]
current_depth = 0
order = []
parent = {root: None}
depth = {root: current_depth}
while current:
	current_depth += 1
	order.extend(current)
	nxt = []
	for node in current:
		for neighbor in adj_list[node]:
			if neighbor not in parent:
				parent[neighbor] = node
				depth[neighbor] = current_depth
				nxt.append(neighbor)
	current = nxt
score = Counter()
state = {}
for node in reversed(order):
	states = [state[neighbor] for neighbor in adj_list[node] if neighbor != parent[node]]
	largest = {s: [depth[node], node, 0] for s in elements[node]}
	if states:
		max_index = max(range(len(states)), key=lambda x: len(states[x]))
		if len(states[max_index]) > len(largest):
			(states[max_index], largest) = (largest, states[max_index])
	sets = defaultdict(list)
	for cur_state in states:
		for (set_no, v) in cur_state.items():
			sets[set_no].append(v)
	for (set_no, states) in sets.items():
		if len(states) == 1 and set_no not in largest:
			largest[set_no] = states[0]
			continue
		if set_no in largest:
			states.append(largest.pop(set_no))
		total_flow = 0
		total_node_sum = 0
		for (node_depth, node_sum, node_flow) in states:
			flow_delta = mul(node_depth - depth[node], node_sum)
			total_flow = add(total_flow, flow_delta, node_flow)
			total_node_sum += node_sum
		set_score = 0
		for (node_depth, node_sum, node_flow) in states:
			node_flow = add(mul(node_depth - depth[node], node_sum), node_flow)
			diff = mul(sub(total_flow, node_flow), node_sum)
			set_score = add(set_score, diff)
		score[set_no] = add(score[set_no], set_score)
		largest[set_no] = (depth[node], total_node_sum, total_flow)
	state[node] = largest
print(*(score[i] for i in range(q)), sep='\n')


from collections import Counter, defaultdict
MOD = 10 ** 9 + 7

def read_row():
	return (int(x) for x in input().split())

def mul(x, y):
	return x * y % MOD

def add(*args):
	return sum(args) % MOD

def sub(x, y):
	return (x - y) % MOD
(n, q) = read_row()
adj_list = defaultdict(list)
for _ in range(n - 1):
	(u, v) = read_row()
	adj_list[u].append(v)
	adj_list[v].append(u)
elements = {v: set() for v in adj_list}
for set_no in range(q):
	read_row()
	for x in read_row():
		elements[x].add(set_no)
root = next(iter(adj_list))
current = [root]
current_depth = 0
order = []
parent = {root: None}
depth = {root: current_depth}
while current:
	current_depth += 1
	order.extend(current)
	nxt = []
	for node in current:
		for neighbor in adj_list[node]:
			if neighbor not in parent:
				parent[neighbor] = node
				depth[neighbor] = current_depth
				nxt.append(neighbor)
	current = nxt
score = Counter()
state = {}
for node in reversed(order):
	states = [state[neighbor] for neighbor in adj_list[node] if neighbor != parent[node]]
	largest = {s: [depth[node], node, 0] for s in elements[node]}
	if states:
		max_index = max(range(len(states)), key=lambda x: len(states[x]))
		if len(states[max_index]) > len(largest):
			(states[max_index], largest) = (largest, states[max_index])
	sets = defaultdict(list)
	for cur_state in states:
		for (set_no, v) in cur_state.items():
			sets[set_no].append(v)
	for (set_no, states) in sets.items():
		if len(states) == 1 and set_no not in largest:
			largest[set_no] = states[0]
			continue
		if set_no in largest:
			states.append(largest.pop(set_no))
		total_flow = 0
		total_node_sum = 0
		for (node_depth, node_sum, node_flow) in states:
			flow_delta = mul(node_depth - depth[node], node_sum)
			total_flow = add(total_flow, flow_delta, node_flow)
			total_node_sum += node_sum
		set_score = 0
		for (node_depth, node_sum, node_flow) in states:
			node_flow = add(mul(node_depth - depth[node], node_sum), node_flow)
			diff = mul(sub(total_flow, node_flow), node_sum)
			set_score = add(set_score, diff)
		score[set_no] = add(score[set_no], set_score)
		largest[set_no] = (depth[node], total_node_sum, total_flow)
	state[node] = largest
print(*(score[i] for i in range(q)), sep='\n')


from collections import Counter, defaultdict
MOD = 10 ** 9 + 7

def read_row():
	return (int(x) for x in input().split())

def mul(x, y):
	return x * y % MOD

def add(*args):
	return sum(args) % MOD

def sub(x, y):
	return (x - y) % MOD
(n, q) = read_row()
adj_list = defaultdict(list)
for _ in range(n - 1):
	(u, v) = read_row()
	adj_list[u].append(v)
	adj_list[v].append(u)
elements = {v: set() for v in adj_list}
for set_no in range(q):
	read_row()
	for x in read_row():
		elements[x].add(set_no)
root = next(iter(adj_list))
current = [root]
current_depth = 0
order = []
parent = {root: None}
depth = {root: current_depth}
while current:
	current_depth += 1
	order.extend(current)
	nxt = []
	for node in current:
		for neighbor in adj_list[node]:
			if neighbor not in parent:
				parent[neighbor] = node
				depth[neighbor] = current_depth
				nxt.append(neighbor)
	current = nxt
score = Counter()
state = {}
for node in reversed(order):
	states = [state[neighbor] for neighbor in adj_list[node] if neighbor != parent[node]]
	largest = {s: [depth[node], node, 0] for s in elements[node]}
	if states:
		max_index = max(range(len(states)), key=lambda x: len(states[x]))
		if len(states[max_index]) > len(largest):
			(states[max_index], largest) = (largest, states[max_index])
	sets = defaultdict(list)
	for cur_state in states:
		for (set_no, v) in cur_state.items():
			sets[set_no].append(v)
	for (set_no, states) in sets.items():
		if len(states) == 1 and set_no not in largest:
			largest[set_no] = states[0]
			continue
		if set_no in largest:
			states.append(largest.pop(set_no))
		total_flow = 0
		total_node_sum = 0
		for (node_depth, node_sum, node_flow) in states:
			flow_delta = mul(node_depth - depth[node], node_sum)
			total_flow = add(total_flow, flow_delta, node_flow)
			total_node_sum += node_sum
		set_score = 0
		for (node_depth, node_sum, node_flow) in states:
			node_flow = add(mul(node_depth - depth[node], node_sum), node_flow)
			diff = mul(sub(total_flow, node_flow), node_sum)
			set_score = add(set_score, diff)
		score[set_no] = add(score[set_no], set_score)
		largest[set_no] = (depth[node], total_node_sum, total_flow)
	state[node] = largest
print(*(score[i] for i in range(q)), sep='\n')


from collections import Counter, defaultdict
MOD = 10 ** 9 + 7

def read_row():
	return (int(x) for x in input().split())

def mul(x, y):
	return x * y % MOD

def add(*args):
	return sum(args) % MOD

def sub(x, y):
	return (x - y) % MOD
(n, q) = read_row()
adj_list = defaultdict(list)
for _ in range(n - 1):
	(u, v) = read_row()
	adj_list[u].append(v)
	adj_list[v].append(u)
elements = {v: set() for v in adj_list}
for set_no in range(q):
	read_row()
	for x in read_row():
		elements[x].add(set_no)
root = next(iter(adj_list))
current = [root]
current_depth = 0
order = []
parent = {root: None}
depth = {root: current_depth}
while current:
	current_depth += 1
	order.extend(current)
	nxt = []
	for node in current:
		for neighbor in adj_list[node]:
			if neighbor not in parent:
				parent[neighbor] = node
				depth[neighbor] = current_depth
				nxt.append(neighbor)
	current = nxt
score = Counter()
state = {}
for node in reversed(order):
	states = [state[neighbor] for neighbor in adj_list[node] if neighbor != parent[node]]
	largest = {s: [depth[node], node, 0] for s in elements[node]}
	if states:
		max_index = max(range(len(states)), key=lambda x: len(states[x]))
		if len(states[max_index]) > len(largest):
			(states[max_index], largest) = (largest, states[max_index])
	sets = defaultdict(list)
	for cur_state in states:
		for (set_no, v) in cur_state.items():
			sets[set_no].append(v)
	for (set_no, states) in sets.items():
		if len(states) == 1 and set_no not in largest:
			largest[set_no] = states[0]
			continue
		if set_no in largest:
			states.append(largest.pop(set_no))
		total_flow = 0
		total_node_sum = 0
		for (node_depth, node_sum, node_flow) in states:
			flow_delta = mul(node_depth - depth[node], node_sum)
			total_flow = add(total_flow, flow_delta, node_flow)
			total_node_sum += node_sum
		set_score = 0
		for (node_depth, node_sum, node_flow) in states:
			node_flow = add(mul(node_depth - depth[node], node_sum), node_flow)
			diff = mul(sub(total_flow, node_flow), node_sum)
			set_score = add(set_score, diff)
		score[set_no] = add(score[set_no], set_score)
		largest[set_no] = (depth[node], total_node_sum, total_flow)
	state[node] = largest
print(*(score[i] for i in range(q)), sep='\n')


from collections import Counter, defaultdict
MOD = 10 ** 9 + 7

def read_row():
	return (int(x) for x in input().split())

def mul(x, y):
	return x * y % MOD

def add(*args):
	return sum(args) % MOD

def sub(x, y):
	return (x - y) % MOD
(n, q) = read_row()
adj_list = defaultdict(list)
for _ in range(n - 1):
	(u, v) = read_row()
	adj_list[u].append(v)
	adj_list[v].append(u)
elements = {v: set() for v in adj_list}
for set_no in range(q):
	read_row()
	for x in read_row():
		elements[x].add(set_no)
root = next(iter(adj_list))
current = [root]
current_depth = 0
order = []
parent = {root: None}
depth = {root: current_depth}
while current:
	current_depth += 1
	order.extend(current)
	nxt = []
	for node in current:
		for neighbor in adj_list[node]:
			if neighbor not in parent:
				parent[neighbor] = node
				depth[neighbor] = current_depth
				nxt.append(neighbor)
	current = nxt
score = Counter()
state = {}
for node in reversed(order):
	states = [state[neighbor] for neighbor in adj_list[node] if neighbor != parent[node]]
	largest = {s: [depth[node], node, 0] for s in elements[node]}
	if states:
		max_index = max(range(len(states)), key=lambda x: len(states[x]))
		if len(states[max_index]) > len(largest):
			(states[max_index], largest) = (largest, states[max_index])
	sets = defaultdict(list)
	for cur_state in states:
		for (set_no, v) in cur_state.items():
			sets[set_no].append(v)
	for (set_no, states) in sets.items():
		if len(states) == 1 and set_no not in largest:
			largest[set_no] = states[0]
			continue
		if set_no in largest:
			states.append(largest.pop(set_no))
		total_flow = 0
		total_node_sum = 0
		for (node_depth, node_sum, node_flow) in states:
			flow_delta = mul(node_depth - depth[node], node_sum)
			total_flow = add(total_flow, flow_delta, node_flow)
			total_node_sum += node_sum
		set_score = 0
		for (node_depth, node_sum, node_flow) in states:
			node_flow = add(mul(node_depth - depth[node], node_sum), node_flow)
			diff = mul(sub(total_flow, node_flow), node_sum)
			set_score = add(set_score, diff)
		score[set_no] = add(score[set_no], set_score)
		largest[set_no] = (depth[node], total_node_sum, total_flow)
	state[node] = largest
print(*(score[i] for i in range(q)), sep='\n')


from collections import Counter, defaultdict
MOD = 10 ** 9 + 7

def read_row():
	return (int(x) for x in input().split())

def mul(x, y):
	return x * y % MOD

def add(*args):
	return sum(args) % MOD

def sub(x, y):
	return (x - y) % MOD
(n, q) = read_row()
adj_list = defaultdict(list)
for _ in range(n - 1):
	(u, v) = read_row()
	adj_list[u].append(v)
	adj_list[v].append(u)
elements = {v: set() for v in adj_list}
for set_no in range(q):
	read_row()
	for x in read_row():
		elements[x].add(set_no)
root = next(iter(adj_list))
current = [root]
current_depth = 0
order = []
parent = {root: None}
depth = {root: current_depth}
while current:
	current_depth += 1
	order.extend(current)
	nxt = []
	for node in current:
		for neighbor in adj_list[node]:
			if neighbor not in parent:
				parent[neighbor] = node
				depth[neighbor] = current_depth
				nxt.append(neighbor)
	current = nxt
score = Counter()
state = {}
for node in reversed(order):
	states = [state[neighbor] for neighbor in adj_list[node] if neighbor != parent[node]]
	largest = {s: [depth[node], node, 0] for s in elements[node]}
	if states:
		max_index = max(range(len(states)), key=lambda x: len(states[x]))
		if len(states[max_index]) > len(largest):
			(states[max_index], largest) = (largest, states[max_index])
	sets = defaultdict(list)
	for cur_state in states:
		for (set_no, v) in cur_state.items():
			sets[set_no].append(v)
	for (set_no, states) in sets.items():
		if len(states) == 1 and set_no not in largest:
			largest[set_no] = states[0]
			continue
		if set_no in largest:
			states.append(largest.pop(set_no))
		total_flow = 0
		total_node_sum = 0
		for (node_depth, node_sum, node_flow) in states:
			flow_delta = mul(node_depth - depth[node], node_sum)
			total_flow = add(total_flow, flow_delta, node_flow)
			total_node_sum += node_sum
		set_score = 0
		for (node_depth, node_sum, node_flow) in states:
			node_flow = add(mul(node_depth - depth[node], node_sum), node_flow)
			diff = mul(sub(total_flow, node_flow), node_sum)
			set_score = add(set_score, diff)
		score[set_no] = add(score[set_no], set_score)
		largest[set_no] = (depth[node], total_node_sum, total_flow)
	state[node] = largest
print(*(score[i] for i in range(q)), sep='\n')


from collections import Counter, defaultdict
MOD = 10 ** 9 + 7

def read_row():
	return (int(x) for x in input().split())

def mul(x, y):
	return x * y % MOD

def add(*args):
	return sum(args) % MOD

def sub(x, y):
	return (x - y) % MOD
(n, q) = read_row()
adj_list = defaultdict(list)
for _ in range(n - 1):
	(u, v) = read_row()
	adj_list[u].append(v)
	adj_list[v].append(u)
elements = {v: set() for v in adj_list}
for set_no in range(q):
	read_row()
	for x in read_row():
		elements[x].add(set_no)
root = next(iter(adj_list))
current = [root]
current_depth = 0
order = []
parent = {root: None}
depth = {root: current_depth}
while current:
	current_depth += 1
	order.extend(current)
	nxt = []
	for node in current:
		for neighbor in adj_list[node]:
			if neighbor not in parent:
				parent[neighbor] = node
				depth[neighbor] = current_depth
				nxt.append(neighbor)
	current = nxt
score = Counter()
state = {}
for node in reversed(order):
	states = [state[neighbor] for neighbor in adj_list[node] if neighbor != parent[node]]
	largest = {s: [depth[node], node, 0] for s in elements[node]}
	if states:
		max_index = max(range(len(states)), key=lambda x: len(states[x]))
		if len(states[max_index]) > len(largest):
			(states[max_index], largest) = (largest, states[max_index])
	sets = defaultdict(list)
	for cur_state in states:
		for (set_no, v) in cur_state.items():
			sets[set_no].append(v)
	for (set_no, states) in sets.items():
		if len(states) == 1 and set_no not in largest:
			largest[set_no] = states[0]
			continue
		if set_no in largest:
			states.append(largest.pop(set_no))
		total_flow = 0
		total_node_sum = 0
		for (node_depth, node_sum, node_flow) in states:
			flow_delta = mul(node_depth - depth[node], node_sum)
			total_flow = add(total_flow, flow_delta, node_flow)
			total_node_sum += node_sum
		set_score = 0
		for (node_depth, node_sum, node_flow) in states:
			node_flow = add(mul(node_depth - depth[node], node_sum), node_flow)
			diff = mul(sub(total_flow, node_flow), node_sum)
			set_score = add(set_score, diff)
		score[set_no] = add(score[set_no], set_score)
		largest[set_no] = (depth[node], total_node_sum, total_flow)
	state[node] = largest
print(*(score[i] for i in range(q)), sep='\n')


from collections import Counter, defaultdict
MOD = 10 ** 9 + 7

def read_row():
	return (int(x) for x in input().split())

def mul(x, y):
	return x * y % MOD

def add(*args):
	return sum(args) % MOD

def sub(x, y):
	return (x - y) % MOD
(n, q) = read_row()
adj_list = defaultdict(list)
for _ in range(n - 1):
	(u, v) = read_row()
	adj_list[u].append(v)
	adj_list[v].append(u)
elements = {v: set() for v in adj_list}
for set_no in range(q):
	read_row()
	for x in read_row():
		elements[x].add(set_no)
root = next(iter(adj_list))
current = [root]
current_depth = 0
order = []
parent = {root: None}
depth = {root: current_depth}
while current:
	current_depth += 1
	order.extend(current)
	nxt = []
	for node in current:
		for neighbor in adj_list[node]:
			if neighbor not in parent:
				parent[neighbor] = node
				depth[neighbor] = current_depth
				nxt.append(neighbor)
	current = nxt
score = Counter()
state = {}
for node in reversed(order):
	states = [state[neighbor] for neighbor in adj_list[node] if neighbor != parent[node]]
	largest = {s: [depth[node], node, 0] for s in elements[node]}
	if states:
		max_index = max(range(len(states)), key=lambda x: len(states[x]))
		if len(states[max_index]) > len(largest):
			(states[max_index], largest) = (largest, states[max_index])
	sets = defaultdict(list)
	for cur_state in states:
		for (set_no, v) in cur_state.items():
			sets[set_no].append(v)
	for (set_no, states) in sets.items():
		if len(states) == 1 and set_no not in largest:
			largest[set_no] = states[0]
			continue
		if set_no in largest:
			states.append(largest.pop(set_no))
		total_flow = 0
		total_node_sum = 0
		for (node_depth, node_sum, node_flow) in states:
			flow_delta = mul(node_depth - depth[node], node_sum)
			total_flow = add(total_flow, flow_delta, node_flow)
			total_node_sum += node_sum
		set_score = 0
		for (node_depth, node_sum, node_flow) in states:
			node_flow = add(mul(node_depth - depth[node], node_sum), node_flow)
			diff = mul(sub(total_flow, node_flow), node_sum)
			set_score = add(set_score, diff)
		score[set_no] = add(score[set_no], set_score)
		largest[set_no] = (depth[node], total_node_sum, total_flow)
	state[node] = largest
print(*(score[i] for i in range(q)), sep='\n')


from collections import Counter, defaultdict
MOD = 10 ** 9 + 7

def read_row():
	return (int(x) for x in input().split())

def mul(x, y):
	return x * y % MOD

def add(*args):
	return sum(args) % MOD

def sub(x, y):
	return (x - y) % MOD
(n, q) = read_row()
adj_list = defaultdict(list)
for _ in range(n - 1):
	(u, v) = read_row()
	adj_list[u].append(v)
	adj_list[v].append(u)
elements = {v: set() for v in adj_list}
for set_no in range(q):
	read_row()
	for x in read_row():
		elements[x].add(set_no)
root = next(iter(adj_list))
current = [root]
current_depth = 0
order = []
parent = {root: None}
depth = {root: current_depth}
while current:
	current_depth += 1
	order.extend(current)
	nxt = []
	for node in current:
		for neighbor in adj_list[node]:
			if neighbor not in parent:
				parent[neighbor] = node
				depth[neighbor] = current_depth
				nxt.append(neighbor)
	current = nxt
score = Counter()
state = {}
for node in reversed(order):
	states = [state[neighbor] for neighbor in adj_list[node] if neighbor != parent[node]]
	largest = {s: [depth[node], node, 0] for s in elements[node]}
	if states:
		max_index = max(range(len(states)), key=lambda x: len(states[x]))
		if len(states[max_index]) > len(largest):
			(states[max_index], largest) = (largest, states[max_index])
	sets = defaultdict(list)
	for cur_state in states:
		for (set_no, v) in cur_state.items():
			sets[set_no].append(v)
	for (set_no, states) in sets.items():
		if len(states) == 1 and set_no not in largest:
			largest[set_no] = states[0]
			continue
		if set_no in largest:
			states.append(largest.pop(set_no))
		total_flow = 0
		total_node_sum = 0
		for (node_depth, node_sum, node_flow) in states:
			flow_delta = mul(node_depth - depth[node], node_sum)
			total_flow = add(total_flow, flow_delta, node_flow)
			total_node_sum += node_sum
		set_score = 0
		for (node_depth, node_sum, node_flow) in states:
			node_flow = add(mul(node_depth - depth[node], node_sum), node_flow)
			diff = mul(sub(total_flow, node_flow), node_sum)
			set_score = add(set_score, diff)
		score[set_no] = add(score[set_no], set_score)
		largest[set_no] = (depth[node], total_node_sum, total_flow)
	state[node] = largest
print(*(score[i] for i in range(q)), sep='\n')


from collections import Counter, defaultdict
MOD = 10 ** 9 + 7

def read_row():
	return (int(x) for x in input().split())

def mul(x, y):
	return x * y % MOD

def add(*args):
	return sum(args) % MOD

def sub(x, y):
	return (x - y) % MOD
(n, q) = read_row()
adj_list = defaultdict(list)
for _ in range(n - 1):
	(u, v) = read_row()
	adj_list[u].append(v)
	adj_list[v].append(u)
elements = {v: set() for v in adj_list}
for set_no in range(q):
	read_row()
	for x in read_row():
		elements[x].add(set_no)
root = next(iter(adj_list))
current = [root]
current_depth = 0
order = []
parent = {root: None}
depth = {root: current_depth}
while current:
	current_depth += 1
	order.extend(current)
	nxt = []
	for node in current:
		for neighbor in adj_list[node]:
			if neighbor not in parent:
				parent[neighbor] = node
				depth[neighbor] = current_depth
				nxt.append(neighbor)
	current = nxt
score = Counter()
state = {}
for node in reversed(order):
	states = [state[neighbor] for neighbor in adj_list[node] if neighbor != parent[node]]
	largest = {s: [depth[node], node, 0] for s in elements[node]}
	if states:
		max_index = max(range(len(states)), key=lambda x: len(states[x]))
		if len(states[max_index]) > len(largest):
			(states[max_index], largest) = (largest, states[max_index])
	sets = defaultdict(list)
	for cur_state in states:
		for (set_no, v) in cur_state.items():
			sets[set_no].append(v)
	for (set_no, states) in sets.items():
		if len(states) == 1 and set_no not in largest:
			largest[set_no] = states[0]
			continue
		if set_no in largest:
			states.append(largest.pop(set_no))
		total_flow = 0
		total_node_sum = 0
		for (node_depth, node_sum, node_flow) in states:
			flow_delta = mul(node_depth - depth[node], node_sum)
			total_flow = add(total_flow, flow_delta, node_flow)
			total_node_sum += node_sum
		set_score = 0
		for (node_depth, node_sum, node_flow) in states:
			node_flow = add(mul(node_depth - depth[node], node_sum), node_flow)
			diff = mul(sub(total_flow, node_flow), node_sum)
			set_score = add(set_score, diff)
		score[set_no] = add(score[set_no], set_score)
		largest[set_no] = (depth[node], total_node_sum, total_flow)
	state[node] = largest
print(*(score[i] for i in range(q)), sep='\n')


import itertools
import sys
from collections import defaultdict
m = 10 ** 9 + 7
sys.setrecursionlimit(300000)

class Node:

	def __init__(self, Id):
		self.links = set()
		self.parent = []
		self.summ = 0

class Tree:

	def __init__(self, n, edges):
		self.nodes = [Node(0)] + [Node(x + 1) for x in range(n)]
		self.nodes[0].layer = -1
		for edge in edges:
			self.nodes[edge[0]].links.add(edge[1])
			self.nodes[edge[1]].links.add(edge[0])
		self.l = [0] * (len(self.nodes) - 1)
		self.build_tree()
		self.layers = [[] for x in range(self.max_layer + 1)]
		for i in range(1, len(self.nodes)):
			self.layers[self.nodes[i].layer].append(i)
		for layer in range(len(self.layers) - 1, -1, -1):
			for index in self.layers[layer]:
				for link in self.nodes[index].links:
					self.nodes[index].summ += self.nodes[link].summ
				self.nodes[index].summ += 1
		self.build_tree1()

	def build_tree(self):
		queue = [None] * (len(self.nodes) + 1)
		next_lev = [None] * (len(self.nodes) + 1)
		queue[0] = 1
		c_q = 1
		c_n = 0
		layer = 0
		self.nodes[1].layer = layer
		self.nodes[1].weight = 0
		while c_q > 0:
			layer += 1
			for i in range(c_q):
				for link in self.nodes[queue[i]].links:
					next_lev[c_n] = link
					c_n += 1
					self.nodes[link].layer = layer
					self.nodes[link].weight = layer * link
					self.nodes[link].parent.append(queue[i])
					self.nodes[link].links.remove(queue[i])
					co = 1
					while True:
						try:
							self.nodes[link].parent.append(self.nodes[self.nodes[link].parent[co - 1]].parent[co - 1])
							co += 1
						except:
							break
			c_q = c_n
			t = queue
			queue = next_lev
			next_lev = t
			c_n = 0
		self.max_layer = layer

	def build_tree1(self):
		queue = [None] * (len(self.nodes) + 1)
		next_lev = [None] * (len(self.nodes) + 1)
		queue[0] = 1
		c_q = 1
		c_n = 0
		self.nodes[1].bucket = self.nodes[1].summ - 1
		while c_q > 0:
			for i in range(c_q):
				bucket = self.nodes[queue[i]].bucket
				for link in self.nodes[queue[i]].links:
					next_lev[c_n] = link
					c_n += 1
					self.nodes[link].bucket = bucket - 1
					bucket -= self.nodes[link].summ
			c_q = c_n
			t = queue
			queue = next_lev
			next_lev = t
			c_n = 0

	def LCA(self, n, m):
		f = n
		s = m
		h = self.nodes[f].layer - self.nodes[s].layer
		c = 0
		while h > 0:
			if h % 2 == 1:
				f = self.nodes[f].parent[c]
			c += 1
			h >>= 1
		h = -h
		while h > 0:
			if h % 2 == 1:
				s = self.nodes[s].parent[c]
			c += 1
			h >>= 1
		if s == f:
			return s
		c = len(self.nodes[s].parent) - 1
		while True:
			while c >= 0 and self.nodes[s].parent[c] == self.nodes[f].parent[c]:
				c -= 1
			if c == -1:
				return self.nodes[s].parent[0]
			s = self.nodes[s].parent[c]
			f = self.nodes[f].parent[c]
			c -= 1
			if len(self.nodes[s].parent) - 1 < c:
				c = len(self.nodes[s].parent) - 1

	def build_subtree(self, S):
		for el in S:
			self.nodes[el].sub_l = set()
		vertices = sorted(list(S), key=lambda x: self.nodes[x].bucket)
		stack = []
		for v in range(len(vertices)):
			if not stack:
				stack.append(vertices[v])
			else:
				tmp = self.LCA(vertices[v - 1], vertices[v])
				while stack and self.nodes[stack[-1]].layer > self.nodes[tmp].layer:
					el = stack.pop()
				if not stack or stack[-1] != tmp:
					self.nodes[tmp].sub_l = set()
					self.nodes[el].sub_p = tmp
					self.nodes[tmp].sub_l.add(el)
					if stack:
						self.nodes[tmp].sub_p = stack[-1]
						self.nodes[stack[-1]].sub_l.add(tmp)
						self.nodes[stack[-1]].sub_l.remove(el)
					stack.append(tmp)
				if tmp != vertices[v]:
					self.nodes[tmp].sub_l.add(vertices[v])
					self.nodes[vertices[v]].sub_p = tmp
					stack.append(vertices[v])
		return stack[0]

	def trav_sub_tree(self, node, S):
		Sum = 0
		Weight = 0
		Res = 0
		tmp = [0] * len(self.nodes[node].sub_l)
		for (c, link) in enumerate(self.nodes[node].sub_l):
			tmp[c] = self.trav_sub_tree(link, S)
		for my_tup in tmp:
			Sum += my_tup[0]
			Weight += my_tup[1]
			Res += my_tup[2]
		if node in S:
			Res += (Weight - Sum * self.nodes[node].layer) * node
		for i in range(len(tmp)):
			tmp[i] = (tmp[i][0], tmp[i][1] - tmp[i][0] * self.nodes[node].layer)
		for i in range(len(tmp)):
			Res += tmp[i][1] * (Sum - tmp[i][0])
		if node in S:
			Sum += node
			Weight += self.nodes[node].weight
		if len(self.nodes[node].sub_l) == 0:
			self.nodes[node].sub_l = None
			return (node, self.nodes[node].weight, 0)
		else:
			self.nodes[node].sub_l = None
			return (Sum, Weight, Res % m)
(N, q) = map(int, input().split())
edges = []
for _ in range(N - 1):
	edges.append(list(map(int, input().split())))
tree = Tree(N, edges)
for _ in range(q):
	k = int(input())
	S = set(map(int, input().split()))
	print(tree.trav_sub_tree(tree.build_subtree(S), S)[2])


from collections import Counter, defaultdict
MOD = 10 ** 9 + 7

def read_row():
	return (int(x) for x in input().split())

def mul(x, y):
	return x * y % MOD

def add(*args):
	return sum(args) % MOD

def sub(x, y):
	return (x - y) % MOD
(n, q) = read_row()
adj_list = defaultdict(list)
for _ in range(n - 1):
	(u, v) = read_row()
	adj_list[u].append(v)
	adj_list[v].append(u)
elements = {v: set() for v in adj_list}
for set_no in range(q):
	read_row()
	for x in read_row():
		elements[x].add(set_no)
root = next(iter(adj_list))
current = [root]
current_depth = 0
order = []
parent = {root: None}
depth = {root: current_depth}
while current:
	current_depth += 1
	order.extend(current)
	nxt = []
	for node in current:
		for neighbor in adj_list[node]:
			if neighbor not in parent:
				parent[neighbor] = node
				depth[neighbor] = current_depth
				nxt.append(neighbor)
	current = nxt
score = Counter()
state = {}
for node in reversed(order):
	states = [state[neighbor] for neighbor in adj_list[node] if neighbor != parent[node]]
	largest = {s: [depth[node], node, 0] for s in elements[node]}
	if states:
		max_index = max(range(len(states)), key=lambda x: len(states[x]))
		if len(states[max_index]) > len(largest):
			(states[max_index], largest) = (largest, states[max_index])
	sets = defaultdict(list)
	for cur_state in states:
		for (set_no, v) in cur_state.items():
			sets[set_no].append(v)
	for (set_no, states) in sets.items():
		if len(states) == 1 and set_no not in largest:
			largest[set_no] = states[0]
			continue
		if set_no in largest:
			states.append(largest.pop(set_no))
		total_flow = 0
		total_node_sum = 0
		for (node_depth, node_sum, node_flow) in states:
			flow_delta = mul(node_depth - depth[node], node_sum)
			total_flow = add(total_flow, flow_delta, node_flow)
			total_node_sum += node_sum
		set_score = 0
		for (node_depth, node_sum, node_flow) in states:
			node_flow = add(mul(node_depth - depth[node], node_sum), node_flow)
			diff = mul(sub(total_flow, node_flow), node_sum)
			set_score = add(set_score, diff)
		score[set_no] = add(score[set_no], set_score)
		largest[set_no] = (depth[node], total_node_sum, total_flow)
	state[node] = largest
print(*(score[i] for i in range(q)), sep='\n')


from collections import Counter, defaultdict
MOD = 10 ** 9 + 7

def read_row():
	return (int(x) for x in input().split())

def mul(x, y):
	return x * y % MOD

def add(*args):
	return sum(args) % MOD

def sub(x, y):
	return (x - y) % MOD
(n, q) = read_row()
adj_list = defaultdict(list)
for _ in range(n - 1):
	(u, v) = read_row()
	adj_list[u].append(v)
	adj_list[v].append(u)
elements = {v: set() for v in adj_list}
for set_no in range(q):
	read_row()
	for x in read_row():
		elements[x].add(set_no)
root = next(iter(adj_list))
current = [root]
current_depth = 0
order = []
parent = {root: None}
depth = {root: current_depth}
while current:
	current_depth += 1
	order.extend(current)
	nxt = []
	for node in current:
		for neighbor in adj_list[node]:
			if neighbor not in parent:
				parent[neighbor] = node
				depth[neighbor] = current_depth
				nxt.append(neighbor)
	current = nxt
score = Counter()
state = {}
for node in reversed(order):
	states = [state[neighbor] for neighbor in adj_list[node] if neighbor != parent[node]]
	largest = {s: [depth[node], node, 0] for s in elements[node]}
	if states:
		max_index = max(range(len(states)), key=lambda x: len(states[x]))
		if len(states[max_index]) > len(largest):
			(states[max_index], largest) = (largest, states[max_index])
	sets = defaultdict(list)
	for cur_state in states:
		for (set_no, v) in cur_state.items():
			sets[set_no].append(v)
	for (set_no, states) in sets.items():
		if len(states) == 1 and set_no not in largest:
			largest[set_no] = states[0]
			continue
		if set_no in largest:
			states.append(largest.pop(set_no))
		total_flow = 0
		total_node_sum = 0
		for (node_depth, node_sum, node_flow) in states:
			flow_delta = mul(node_depth - depth[node], node_sum)
			total_flow = add(total_flow, flow_delta, node_flow)
			total_node_sum += node_sum
		set_score = 0
		for (node_depth, node_sum, node_flow) in states:
			node_flow = add(mul(node_depth - depth[node], node_sum), node_flow)
			diff = mul(sub(total_flow, node_flow), node_sum)
			set_score = add(set_score, diff)
		score[set_no] = add(score[set_no], set_score)
		largest[set_no] = (depth[node], total_node_sum, total_flow)
	state[node] = largest
print(*(score[i] for i in range(q)), sep='\n')


from collections import Counter, defaultdict
MOD = 10 ** 9 + 7

def read_row():
	return (int(x) for x in input().split())

def mul(x, y):
	return x * y % MOD

def add(*args):
	return sum(args) % MOD

def sub(x, y):
	return (x - y) % MOD
(n, q) = read_row()
adj_list = defaultdict(list)
for _ in range(n - 1):
	(u, v) = read_row()
	adj_list[u].append(v)
	adj_list[v].append(u)
elements = {v: set() for v in adj_list}
for set_no in range(q):
	read_row()
	for x in read_row():
		elements[x].add(set_no)
root = next(iter(adj_list))
current = [root]
current_depth = 0
order = []
parent = {root: None}
depth = {root: current_depth}
while current:
	current_depth += 1
	order.extend(current)
	nxt = []
	for node in current:
		for neighbor in adj_list[node]:
			if neighbor not in parent:
				parent[neighbor] = node
				depth[neighbor] = current_depth
				nxt.append(neighbor)
	current = nxt
score = Counter()
state = {}
for node in reversed(order):
	states = [state[neighbor] for neighbor in adj_list[node] if neighbor != parent[node]]
	largest = {s: [depth[node], node, 0] for s in elements[node]}
	if states:
		max_index = max(range(len(states)), key=lambda x: len(states[x]))
		if len(states[max_index]) > len(largest):
			(states[max_index], largest) = (largest, states[max_index])
	sets = defaultdict(list)
	for cur_state in states:
		for (set_no, v) in cur_state.items():
			sets[set_no].append(v)
	for (set_no, states) in sets.items():
		if len(states) == 1 and set_no not in largest:
			largest[set_no] = states[0]
			continue
		if set_no in largest:
			states.append(largest.pop(set_no))
		total_flow = 0
		total_node_sum = 0
		for (node_depth, node_sum, node_flow) in states:
			flow_delta = mul(node_depth - depth[node], node_sum)
			total_flow = add(total_flow, flow_delta, node_flow)
			total_node_sum += node_sum
		set_score = 0
		for (node_depth, node_sum, node_flow) in states:
			node_flow = add(mul(node_depth - depth[node], node_sum), node_flow)
			diff = mul(sub(total_flow, node_flow), node_sum)
			set_score = add(set_score, diff)
		score[set_no] = add(score[set_no], set_score)
		largest[set_no] = (depth[node], total_node_sum, total_flow)
	state[node] = largest
print(*(score[i] for i in range(q)), sep='\n')


def put(d, a, b):
	if a in d:
		d[a].append(b)
	else:
		d[a] = [b]

def main():
	for n in ns[::-1]:
		r = [tt[s] for s in tree[n] if s != f[n]]
		bst = {s: [gl[n], n, 0] for s in queries[n]}
		if r:
			o = max(range(len(r)), key=lambda a: len(r[a]))
			if len(r[o]) > len(bst):
				(r[o], bst) = (bst, r[o])
		ry = {}
		for ae in r:
			for (y, v) in ae.items():
				put(ry, y, v)
		for (y, r) in ry.items():
			(eq, z, t) = (0, 0, 0)
			if len(r) == 1 and y not in bst:
				bst[y] = r[0]
				continue
			if y in bst:
				r.append(bst.pop(y))
			for (d, v, c) in r:
				eq += (d - gl[n]) * v + c
				z += v
			for (d, v, c) in r:
				c += (d - gl[n]) * v
				diff = (eq - c) * v
				t += diff
			returns[y] += t
			bst[y] = (gl[n], z, eq)
		tt[n] = bst

def locate():
	q = [r]
	level = 0
	while q:
		level += 1
		tmp = []
		ns.extend(q)
		for n in q:
			for s in tree[n]:
				if s not in f:
					f[s] = n
					gl[s] = level
					tmp.append(s)
		q = tmp
tree = {}
tt = {}
(n, q) = map(int, input().split())
returns = [0] * q
for _ in range(n - 1):
	(a, b) = map(int, input().split())
	put(tree, a, b)
	put(tree, b, a)
queries = {a: set() for a in tree}
for y in range(q):
	input()
	for x in map(int, input().split()):
		queries[x].add(y)
r = next(iter(tree))
ns = []
f = {r: None}
gl = {r: 0}
locate()
main()
for s in returns:
	print(s % (10 ** 9 + 7))


from collections import Counter, defaultdict
MOD = 10 ** 9 + 7

def read_row():
	return (int(x) for x in input().split())

def mul(x, y):
	return x * y % MOD

def add(*args):
	return sum(args) % MOD

def sub(x, y):
	return (x - y) % MOD
(n, q) = read_row()
adj_list = defaultdict(list)
for _ in range(n - 1):
	(u, v) = read_row()
	adj_list[u].append(v)
	adj_list[v].append(u)
elements = {v: set() for v in adj_list}
for set_no in range(q):
	read_row()
	for x in read_row():
		elements[x].add(set_no)
root = next(iter(adj_list))
current = [root]
current_depth = 0
order = []
parent = {root: None}
depth = {root: current_depth}
while current:
	current_depth += 1
	order.extend(current)
	nxt = []
	for node in current:
		for neighbor in adj_list[node]:
			if neighbor not in parent:
				parent[neighbor] = node
				depth[neighbor] = current_depth
				nxt.append(neighbor)
	current = nxt
score = Counter()
state = {}
for node in reversed(order):
	states = [state[neighbor] for neighbor in adj_list[node] if neighbor != parent[node]]
	largest = {s: [depth[node], node, 0] for s in elements[node]}
	if states:
		max_index = max(range(len(states)), key=lambda x: len(states[x]))
		if len(states[max_index]) > len(largest):
			(states[max_index], largest) = (largest, states[max_index])
	sets = defaultdict(list)
	for cur_state in states:
		for (set_no, v) in cur_state.items():
			sets[set_no].append(v)
	for (set_no, states) in sets.items():
		if len(states) == 1 and set_no not in largest:
			largest[set_no] = states[0]
			continue
		if set_no in largest:
			states.append(largest.pop(set_no))
		total_flow = 0
		total_node_sum = 0
		for (node_depth, node_sum, node_flow) in states:
			flow_delta = mul(node_depth - depth[node], node_sum)
			total_flow = add(total_flow, flow_delta, node_flow)
			total_node_sum += node_sum
		set_score = 0
		for (node_depth, node_sum, node_flow) in states:
			node_flow = add(mul(node_depth - depth[node], node_sum), node_flow)
			diff = mul(sub(total_flow, node_flow), node_sum)
			set_score = add(set_score, diff)
		score[set_no] = add(score[set_no], set_score)
		largest[set_no] = (depth[node], total_node_sum, total_flow)
	state[node] = largest
print(*(score[i] for i in range(q)), sep='\n')


from collections import Counter, defaultdict
MOD = 10 ** 9 + 7

def read_row():
	return (int(x) for x in input().split())

def mul(x, y):
	return x * y % MOD

def add(*args):
	return sum(args) % MOD

def sub(x, y):
	return (x - y) % MOD
(n, q) = read_row()
adj_list = defaultdict(list)
for _ in range(n - 1):
	(u, v) = read_row()
	adj_list[u].append(v)
	adj_list[v].append(u)
elements = {v: set() for v in adj_list}
for set_no in range(q):
	read_row()
	for x in read_row():
		elements[x].add(set_no)
root = next(iter(adj_list))
current = [root]
current_depth = 0
order = []
parent = {root: None}
depth = {root: current_depth}
while current:
	current_depth += 1
	order.extend(current)
	nxt = []
	for node in current:
		for neighbor in adj_list[node]:
			if neighbor not in parent:
				parent[neighbor] = node
				depth[neighbor] = current_depth
				nxt.append(neighbor)
	current = nxt
score = Counter()
state = {}
for node in reversed(order):
	states = [state[neighbor] for neighbor in adj_list[node] if neighbor != parent[node]]
	largest = {s: [depth[node], node, 0] for s in elements[node]}
	if states:
		max_index = max(range(len(states)), key=lambda x: len(states[x]))
		if len(states[max_index]) > len(largest):
			(states[max_index], largest) = (largest, states[max_index])
	sets = defaultdict(list)
	for cur_state in states:
		for (set_no, v) in cur_state.items():
			sets[set_no].append(v)
	for (set_no, states) in sets.items():
		if len(states) == 1 and set_no not in largest:
			largest[set_no] = states[0]
			continue
		if set_no in largest:
			states.append(largest.pop(set_no))
		total_flow = 0
		total_node_sum = 0
		for (node_depth, node_sum, node_flow) in states:
			flow_delta = mul(node_depth - depth[node], node_sum)
			total_flow = add(total_flow, flow_delta, node_flow)
			total_node_sum += node_sum
		set_score = 0
		for (node_depth, node_sum, node_flow) in states:
			node_flow = add(mul(node_depth - depth[node], node_sum), node_flow)
			diff = mul(sub(total_flow, node_flow), node_sum)
			set_score = add(set_score, diff)
		score[set_no] = add(score[set_no], set_score)
		largest[set_no] = (depth[node], total_node_sum, total_flow)
	state[node] = largest
print(*(score[i] for i in range(q)), sep='\n')


from collections import Counter, defaultdict
MOD = 10 ** 9 + 7

def read_row():
	return (int(x) for x in input().split())

def mul(x, y):
	return x * y % MOD

def add(*args):
	return sum(args) % MOD

def sub(x, y):
	return (x - y) % MOD
(n, q) = read_row()
adj_list = defaultdict(list)
for _ in range(n - 1):
	(u, v) = read_row()
	adj_list[u].append(v)
	adj_list[v].append(u)
elements = {v: set() for v in adj_list}
for set_no in range(q):
	read_row()
	for x in read_row():
		elements[x].add(set_no)
root = next(iter(adj_list))
current = [root]
current_depth = 0
order = []
parent = {root: None}
depth = {root: current_depth}
while current:
	current_depth += 1
	order.extend(current)
	nxt = []
	for node in current:
		for neighbor in adj_list[node]:
			if neighbor not in parent:
				parent[neighbor] = node
				depth[neighbor] = current_depth
				nxt.append(neighbor)
	current = nxt
score = Counter()
state = {}
for node in reversed(order):
	states = [state[neighbor] for neighbor in adj_list[node] if neighbor != parent[node]]
	largest = {s: [depth[node], node, 0] for s in elements[node]}
	if states:
		max_index = max(range(len(states)), key=lambda x: len(states[x]))
		if len(states[max_index]) > len(largest):
			(states[max_index], largest) = (largest, states[max_index])
	sets = defaultdict(list)
	for cur_state in states:
		for (set_no, v) in cur_state.items():
			sets[set_no].append(v)
	for (set_no, states) in sets.items():
		if len(states) == 1 and set_no not in largest:
			largest[set_no] = states[0]
			continue
		if set_no in largest:
			states.append(largest.pop(set_no))
		total_flow = 0
		total_node_sum = 0
		for (node_depth, node_sum, node_flow) in states:
			flow_delta = mul(node_depth - depth[node], node_sum)
			total_flow = add(total_flow, flow_delta, node_flow)
			total_node_sum += node_sum
		set_score = 0
		for (node_depth, node_sum, node_flow) in states:
			node_flow = add(mul(node_depth - depth[node], node_sum), node_flow)
			diff = mul(sub(total_flow, node_flow), node_sum)
			set_score = add(set_score, diff)
		score[set_no] = add(score[set_no], set_score)
		largest[set_no] = (depth[node], total_node_sum, total_flow)
	state[node] = largest
print(*(score[i] for i in range(q)), sep='\n')


from collections import Counter, defaultdict
MOD = 10 ** 9 + 7

def read_row():
	return (int(x) for x in input().split())

def mul(x, y):
	return x * y % MOD

def add(*args):
	return sum(args) % MOD

def sub(x, y):
	return (x - y) % MOD
(n, q) = read_row()
adj_list = defaultdict(list)
for _ in range(n - 1):
	(u, v) = read_row()
	adj_list[u].append(v)
	adj_list[v].append(u)
elements = {v: set() for v in adj_list}
for set_no in range(q):
	read_row()
	for x in read_row():
		elements[x].add(set_no)
root = next(iter(adj_list))
current = [root]
current_depth = 0
order = []
parent = {root: None}
depth = {root: current_depth}
while current:
	current_depth += 1
	order.extend(current)
	nxt = []
	for node in current:
		for neighbor in adj_list[node]:
			if neighbor not in parent:
				parent[neighbor] = node
				depth[neighbor] = current_depth
				nxt.append(neighbor)
	current = nxt
score = Counter()
state = {}
for node in reversed(order):
	states = [state[neighbor] for neighbor in adj_list[node] if neighbor != parent[node]]
	largest = {s: [depth[node], node, 0] for s in elements[node]}
	if states:
		max_index = max(range(len(states)), key=lambda x: len(states[x]))
		if len(states[max_index]) > len(largest):
			(states[max_index], largest) = (largest, states[max_index])
	sets = defaultdict(list)
	for cur_state in states:
		for (set_no, v) in cur_state.items():
			sets[set_no].append(v)
	for (set_no, states) in sets.items():
		if len(states) == 1 and set_no not in largest:
			largest[set_no] = states[0]
			continue
		if set_no in largest:
			states.append(largest.pop(set_no))
		total_flow = 0
		total_node_sum = 0
		for (node_depth, node_sum, node_flow) in states:
			flow_delta = mul(node_depth - depth[node], node_sum)
			total_flow = add(total_flow, flow_delta, node_flow)
			total_node_sum += node_sum
		set_score = 0
		for (node_depth, node_sum, node_flow) in states:
			node_flow = add(mul(node_depth - depth[node], node_sum), node_flow)
			diff = mul(sub(total_flow, node_flow), node_sum)
			set_score = add(set_score, diff)
		score[set_no] = add(score[set_no], set_score)
		largest[set_no] = (depth[node], total_node_sum, total_flow)
	state[node] = largest
print(*(score[i] for i in range(q)), sep='\n')


from collections import Counter, defaultdict
MOD = 10 ** 9 + 7

def read_row():
	return (int(x) for x in input().split())

def mul(x, y):
	return x * y % MOD

def add(*args):
	return sum(args) % MOD

def sub(x, y):
	return (x - y) % MOD
(n, q) = read_row()
adj_list = defaultdict(list)
for _ in range(n - 1):
	(u, v) = read_row()
	adj_list[u].append(v)
	adj_list[v].append(u)
elements = {v: set() for v in adj_list}
for set_no in range(q):
	read_row()
	for x in read_row():
		elements[x].add(set_no)
root = next(iter(adj_list))
current = [root]
current_depth = 0
order = []
parent = {root: None}
depth = {root: current_depth}
while current:
	current_depth += 1
	order.extend(current)
	nxt = []
	for node in current:
		for neighbor in adj_list[node]:
			if neighbor not in parent:
				parent[neighbor] = node
				depth[neighbor] = current_depth
				nxt.append(neighbor)
	current = nxt
score = Counter()
state = {}
for node in reversed(order):
	states = [state[neighbor] for neighbor in adj_list[node] if neighbor != parent[node]]
	largest = {s: [depth[node], node, 0] for s in elements[node]}
	if states:
		max_index = max(range(len(states)), key=lambda x: len(states[x]))
		if len(states[max_index]) > len(largest):
			(states[max_index], largest) = (largest, states[max_index])
	sets = defaultdict(list)
	for cur_state in states:
		for (set_no, v) in cur_state.items():
			sets[set_no].append(v)
	for (set_no, states) in sets.items():
		if len(states) == 1 and set_no not in largest:
			largest[set_no] = states[0]
			continue
		if set_no in largest:
			states.append(largest.pop(set_no))
		total_flow = 0
		total_node_sum = 0
		for (node_depth, node_sum, node_flow) in states:
			flow_delta = mul(node_depth - depth[node], node_sum)
			total_flow = add(total_flow, flow_delta, node_flow)
			total_node_sum += node_sum
		set_score = 0
		for (node_depth, node_sum, node_flow) in states:
			node_flow = add(mul(node_depth - depth[node], node_sum), node_flow)
			diff = mul(sub(total_flow, node_flow), node_sum)
			set_score = add(set_score, diff)
		score[set_no] = add(score[set_no], set_score)
		largest[set_no] = (depth[node], total_node_sum, total_flow)
	state[node] = largest
print(*(score[i] for i in range(q)), sep='\n')


from collections import Counter, defaultdict
MOD = 10 ** 9 + 7

def read_row():
	return (int(x) for x in input().split())

def mul(x, y):
	return x * y % MOD

def add(*args):
	return sum(args) % MOD

def sub(x, y):
	return (x - y) % MOD
(n, q) = read_row()
adj_list = defaultdict(list)
for _ in range(n - 1):
	(u, v) = read_row()
	adj_list[u].append(v)
	adj_list[v].append(u)
elements = {v: set() for v in adj_list}
for set_no in range(q):
	read_row()
	for x in read_row():
		elements[x].add(set_no)
root = next(iter(adj_list))
current = [root]
current_depth = 0
order = []
parent = {root: None}
depth = {root: current_depth}
while current:
	current_depth += 1
	order.extend(current)
	nxt = []
	for node in current:
		for neighbor in adj_list[node]:
			if neighbor not in parent:
				parent[neighbor] = node
				depth[neighbor] = current_depth
				nxt.append(neighbor)
	current = nxt
score = Counter()
state = {}
for node in reversed(order):
	states = [state[neighbor] for neighbor in adj_list[node] if neighbor != parent[node]]
	largest = {s: [depth[node], node, 0] for s in elements[node]}
	if states:
		max_index = max(range(len(states)), key=lambda x: len(states[x]))
		if len(states[max_index]) > len(largest):
			(states[max_index], largest) = (largest, states[max_index])
	sets = defaultdict(list)
	for cur_state in states:
		for (set_no, v) in cur_state.items():
			sets[set_no].append(v)
	for (set_no, states) in sets.items():
		if len(states) == 1 and set_no not in largest:
			largest[set_no] = states[0]
			continue
		if set_no in largest:
			states.append(largest.pop(set_no))
		total_flow = 0
		total_node_sum = 0
		for (node_depth, node_sum, node_flow) in states:
			flow_delta = mul(node_depth - depth[node], node_sum)
			total_flow = add(total_flow, flow_delta, node_flow)
			total_node_sum += node_sum
		set_score = 0
		for (node_depth, node_sum, node_flow) in states:
			node_flow = add(mul(node_depth - depth[node], node_sum), node_flow)
			diff = mul(sub(total_flow, node_flow), node_sum)
			set_score = add(set_score, diff)
		score[set_no] = add(score[set_no], set_score)
		largest[set_no] = (depth[node], total_node_sum, total_flow)
	state[node] = largest
print(*(score[i] for i in range(q)), sep='\n')


from collections import Counter, defaultdict
MOD = 10 ** 9 + 7

def read_row():
	return (int(x) for x in input().split())

def mul(x, y):
	return x * y % MOD

def add(*args):
	return sum(args) % MOD

def sub(x, y):
	return (x - y) % MOD
(n, q) = read_row()
adj_list = defaultdict(list)
for _ in range(n - 1):
	(u, v) = read_row()
	adj_list[u].append(v)
	adj_list[v].append(u)
elements = {v: set() for v in adj_list}
for set_no in range(q):
	read_row()
	for x in read_row():
		elements[x].add(set_no)
root = next(iter(adj_list))
current = [root]
current_depth = 0
order = []
parent = {root: None}
depth = {root: current_depth}
while current:
	current_depth += 1
	order.extend(current)
	nxt = []
	for node in current:
		for neighbor in adj_list[node]:
			if neighbor not in parent:
				parent[neighbor] = node
				depth[neighbor] = current_depth
				nxt.append(neighbor)
	current = nxt
score = Counter()
state = {}
for node in reversed(order):
	states = [state[neighbor] for neighbor in adj_list[node] if neighbor != parent[node]]
	largest = {s: [depth[node], node, 0] for s in elements[node]}
	if states:
		max_index = max(range(len(states)), key=lambda x: len(states[x]))
		if len(states[max_index]) > len(largest):
			(states[max_index], largest) = (largest, states[max_index])
	sets = defaultdict(list)
	for cur_state in states:
		for (set_no, v) in cur_state.items():
			sets[set_no].append(v)
	for (set_no, states) in sets.items():
		if len(states) == 1 and set_no not in largest:
			largest[set_no] = states[0]
			continue
		if set_no in largest:
			states.append(largest.pop(set_no))
		total_flow = 0
		total_node_sum = 0
		for (node_depth, node_sum, node_flow) in states:
			flow_delta = mul(node_depth - depth[node], node_sum)
			total_flow = add(total_flow, flow_delta, node_flow)
			total_node_sum += node_sum
		set_score = 0
		for (node_depth, node_sum, node_flow) in states:
			node_flow = add(mul(node_depth - depth[node], node_sum), node_flow)
			diff = mul(sub(total_flow, node_flow), node_sum)
			set_score = add(set_score, diff)
		score[set_no] = add(score[set_no], set_score)
		largest[set_no] = (depth[node], total_node_sum, total_flow)
	state[node] = largest
print(*(score[i] for i in range(q)), sep='\n')


from collections import Counter, defaultdict
MOD = 10 ** 9 + 7

def read_row():
	return (int(x) for x in input().split())

def mul(x, y):
	return x * y % MOD

def add(*args):
	return sum(args) % MOD

def sub(x, y):
	return (x - y) % MOD
(n, q) = read_row()
adj_list = defaultdict(list)
for _ in range(n - 1):
	(u, v) = read_row()
	adj_list[u].append(v)
	adj_list[v].append(u)
elements = {v: set() for v in adj_list}
for set_no in range(q):
	read_row()
	for x in read_row():
		elements[x].add(set_no)
root = next(iter(adj_list))
current = [root]
current_depth = 0
order = []
parent = {root: None}
depth = {root: current_depth}
while current:
	current_depth += 1
	order.extend(current)
	nxt = []
	for node in current:
		for neighbor in adj_list[node]:
			if neighbor not in parent:
				parent[neighbor] = node
				depth[neighbor] = current_depth
				nxt.append(neighbor)
	current = nxt
score = Counter()
state = {}
for node in reversed(order):
	states = [state[neighbor] for neighbor in adj_list[node] if neighbor != parent[node]]
	largest = {s: [depth[node], node, 0] for s in elements[node]}
	if states:
		max_index = max(range(len(states)), key=lambda x: len(states[x]))
		if len(states[max_index]) > len(largest):
			(states[max_index], largest) = (largest, states[max_index])
	sets = defaultdict(list)
	for cur_state in states:
		for (set_no, v) in cur_state.items():
			sets[set_no].append(v)
	for (set_no, states) in sets.items():
		if len(states) == 1 and set_no not in largest:
			largest[set_no] = states[0]
			continue
		if set_no in largest:
			states.append(largest.pop(set_no))
		total_flow = 0
		total_node_sum = 0
		for (node_depth, node_sum, node_flow) in states:
			flow_delta = mul(node_depth - depth[node], node_sum)
			total_flow = add(total_flow, flow_delta, node_flow)
			total_node_sum += node_sum
		set_score = 0
		for (node_depth, node_sum, node_flow) in states:
			node_flow = add(mul(node_depth - depth[node], node_sum), node_flow)
			diff = mul(sub(total_flow, node_flow), node_sum)
			set_score = add(set_score, diff)
		score[set_no] = add(score[set_no], set_score)
		largest[set_no] = (depth[node], total_node_sum, total_flow)
	state[node] = largest
print(*(score[i] for i in range(q)), sep='\n')


from collections import Counter, defaultdict
MOD = 10 ** 9 + 7

def read_row():
	return (int(x) for x in input().split())

def mul(x, y):
	return x * y % MOD

def add(*args):
	return sum(args) % MOD

def sub(x, y):
	return (x - y) % MOD
(n, q) = read_row()
adj_list = defaultdict(list)
for _ in range(n - 1):
	(u, v) = read_row()
	adj_list[u].append(v)
	adj_list[v].append(u)
elements = {v: set() for v in adj_list}
for set_no in range(q):
	read_row()
	for x in read_row():
		elements[x].add(set_no)
root = next(iter(adj_list))
current = [root]
current_depth = 0
order = []
parent = {root: None}
depth = {root: current_depth}
while current:
	current_depth += 1
	order.extend(current)
	nxt = []
	for node in current:
		for neighbor in adj_list[node]:
			if neighbor not in parent:
				parent[neighbor] = node
				depth[neighbor] = current_depth
				nxt.append(neighbor)
	current = nxt
score = Counter()
state = {}
for node in reversed(order):
	states = [state[neighbor] for neighbor in adj_list[node] if neighbor != parent[node]]
	largest = {s: [depth[node], node, 0] for s in elements[node]}
	if states:
		max_index = max(range(len(states)), key=lambda x: len(states[x]))
		if len(states[max_index]) > len(largest):
			(states[max_index], largest) = (largest, states[max_index])
	sets = defaultdict(list)
	for cur_state in states:
		for (set_no, v) in cur_state.items():
			sets[set_no].append(v)
	for (set_no, states) in sets.items():
		if len(states) == 1 and set_no not in largest:
			largest[set_no] = states[0]
			continue
		if set_no in largest:
			states.append(largest.pop(set_no))
		total_flow = 0
		total_node_sum = 0
		for (node_depth, node_sum, node_flow) in states:
			flow_delta = mul(node_depth - depth[node], node_sum)
			total_flow = add(total_flow, flow_delta, node_flow)
			total_node_sum += node_sum
		set_score = 0
		for (node_depth, node_sum, node_flow) in states:
			node_flow = add(mul(node_depth - depth[node], node_sum), node_flow)
			diff = mul(sub(total_flow, node_flow), node_sum)
			set_score = add(set_score, diff)
		score[set_no] = add(score[set_no], set_score)
		largest[set_no] = (depth[node], total_node_sum, total_flow)
	state[node] = largest
print(*(score[i] for i in range(q)), sep='\n')


from collections import Counter, defaultdict
MOD = 10 ** 9 + 7

def read_row():
	return (int(x) for x in input().split())

def mul(x, y):
	return x * y % MOD

def add(*args):
	return sum(args) % MOD

def sub(x, y):
	return (x - y) % MOD
(n, q) = read_row()
adj_list = defaultdict(list)
for _ in range(n - 1):
	(u, v) = read_row()
	adj_list[u].append(v)
	adj_list[v].append(u)
elements = {v: set() for v in adj_list}
for set_no in range(q):
	read_row()
	for x in read_row():
		elements[x].add(set_no)
root = next(iter(adj_list))
current = [root]
current_depth = 0
order = []
parent = {root: None}
depth = {root: current_depth}
while current:
	current_depth += 1
	order.extend(current)
	nxt = []
	for node in current:
		for neighbor in adj_list[node]:
			if neighbor not in parent:
				parent[neighbor] = node
				depth[neighbor] = current_depth
				nxt.append(neighbor)
	current = nxt
score = Counter()
state = {}
for node in reversed(order):
	states = [state[neighbor] for neighbor in adj_list[node] if neighbor != parent[node]]
	largest = {s: [depth[node], node, 0] for s in elements[node]}
	if states:
		max_index = max(range(len(states)), key=lambda x: len(states[x]))
		if len(states[max_index]) > len(largest):
			(states[max_index], largest) = (largest, states[max_index])
	sets = defaultdict(list)
	for cur_state in states:
		for (set_no, v) in cur_state.items():
			sets[set_no].append(v)
	for (set_no, states) in sets.items():
		if len(states) == 1 and set_no not in largest:
			largest[set_no] = states[0]
			continue
		if set_no in largest:
			states.append(largest.pop(set_no))
		total_flow = 0
		total_node_sum = 0
		for (node_depth, node_sum, node_flow) in states:
			flow_delta = mul(node_depth - depth[node], node_sum)
			total_flow = add(total_flow, flow_delta, node_flow)
			total_node_sum += node_sum
		set_score = 0
		for (node_depth, node_sum, node_flow) in states:
			node_flow = add(mul(node_depth - depth[node], node_sum), node_flow)
			diff = mul(sub(total_flow, node_flow), node_sum)
			set_score = add(set_score, diff)
		score[set_no] = add(score[set_no], set_score)
		largest[set_no] = (depth[node], total_node_sum, total_flow)
	state[node] = largest
print(*(score[i] for i in range(q)), sep='\n')


from collections import Counter, defaultdict
MOD = 10 ** 9 + 7

def read_row():
	return (int(x) for x in input().split())

def mul(x, y):
	return x * y % MOD

def add(*args):
	return sum(args) % MOD

def sub(x, y):
	return (x - y) % MOD
(n, q) = read_row()
adj_list = defaultdict(list)
for _ in range(n - 1):
	(u, v) = read_row()
	adj_list[u].append(v)
	adj_list[v].append(u)
elements = {v: set() for v in adj_list}
for set_no in range(q):
	read_row()
	for x in read_row():
		elements[x].add(set_no)
root = next(iter(adj_list))
current = [root]
current_depth = 0
order = []
parent = {root: None}
depth = {root: current_depth}
while current:
	current_depth += 1
	order.extend(current)
	nxt = []
	for node in current:
		for neighbor in adj_list[node]:
			if neighbor not in parent:
				parent[neighbor] = node
				depth[neighbor] = current_depth
				nxt.append(neighbor)
	current = nxt
score = Counter()
state = {}
for node in reversed(order):
	states = [state[neighbor] for neighbor in adj_list[node] if neighbor != parent[node]]
	largest = {s: [depth[node], node, 0] for s in elements[node]}
	if states:
		max_index = max(range(len(states)), key=lambda x: len(states[x]))
		if len(states[max_index]) > len(largest):
			(states[max_index], largest) = (largest, states[max_index])
	sets = defaultdict(list)
	for cur_state in states:
		for (set_no, v) in cur_state.items():
			sets[set_no].append(v)
	for (set_no, states) in sets.items():
		if len(states) == 1 and set_no not in largest:
			largest[set_no] = states[0]
			continue
		if set_no in largest:
			states.append(largest.pop(set_no))
		total_flow = 0
		total_node_sum = 0
		for (node_depth, node_sum, node_flow) in states:
			flow_delta = mul(node_depth - depth[node], node_sum)
			total_flow = add(total_flow, flow_delta, node_flow)
			total_node_sum += node_sum
		set_score = 0
		for (node_depth, node_sum, node_flow) in states:
			node_flow = add(mul(node_depth - depth[node], node_sum), node_flow)
			diff = mul(sub(total_flow, node_flow), node_sum)
			set_score = add(set_score, diff)
		score[set_no] = add(score[set_no], set_score)
		largest[set_no] = (depth[node], total_node_sum, total_flow)
	state[node] = largest
print(*(score[i] for i in range(q)), sep='\n')


from collections import Counter, defaultdict
MOD = 10 ** 9 + 7

def read_row():
	return (int(x) for x in input().split())

def mul(x, y):
	return x * y % MOD

def add(*args):
	return sum(args) % MOD

def sub(x, y):
	return (x - y) % MOD
(n, q) = read_row()
adj_list = defaultdict(list)
for _ in range(n - 1):
	(u, v) = read_row()
	adj_list[u].append(v)
	adj_list[v].append(u)
elements = {v: set() for v in adj_list}
for set_no in range(q):
	read_row()
	for x in read_row():
		elements[x].add(set_no)
root = next(iter(adj_list))
current = [root]
current_depth = 0
order = []
parent = {root: None}
depth = {root: current_depth}
while current:
	current_depth += 1
	order.extend(current)
	nxt = []
	for node in current:
		for neighbor in adj_list[node]:
			if neighbor not in parent:
				parent[neighbor] = node
				depth[neighbor] = current_depth
				nxt.append(neighbor)
	current = nxt
score = Counter()
state = {}
for node in reversed(order):
	states = [state[neighbor] for neighbor in adj_list[node] if neighbor != parent[node]]
	largest = {s: [depth[node], node, 0] for s in elements[node]}
	if states:
		max_index = max(range(len(states)), key=lambda x: len(states[x]))
		if len(states[max_index]) > len(largest):
			(states[max_index], largest) = (largest, states[max_index])
	sets = defaultdict(list)
	for cur_state in states:
		for (set_no, v) in cur_state.items():
			sets[set_no].append(v)
	for (set_no, states) in sets.items():
		if len(states) == 1 and set_no not in largest:
			largest[set_no] = states[0]
			continue
		if set_no in largest:
			states.append(largest.pop(set_no))
		total_flow = 0
		total_node_sum = 0
		for (node_depth, node_sum, node_flow) in states:
			flow_delta = mul(node_depth - depth[node], node_sum)
			total_flow = add(total_flow, flow_delta, node_flow)
			total_node_sum += node_sum
		set_score = 0
		for (node_depth, node_sum, node_flow) in states:
			node_flow = add(mul(node_depth - depth[node], node_sum), node_flow)
			diff = mul(sub(total_flow, node_flow), node_sum)
			set_score = add(set_score, diff)
		score[set_no] = add(score[set_no], set_score)
		largest[set_no] = (depth[node], total_node_sum, total_flow)
	state[node] = largest
print(*(score[i] for i in range(q)), sep='\n')


from collections import Counter, defaultdict
MOD = 10 ** 9 + 7

def read_row():
	return (int(x) for x in input().split())

def mul(x, y):
	return x * y % MOD

def add(*args):
	return sum(args) % MOD

def sub(x, y):
	return (x - y) % MOD
(n, q) = read_row()
adj_list = defaultdict(list)
for _ in range(n - 1):
	(u, v) = read_row()
	adj_list[u].append(v)
	adj_list[v].append(u)
elements = {v: set() for v in adj_list}
for set_no in range(q):
	read_row()
	for x in read_row():
		elements[x].add(set_no)
root = next(iter(adj_list))
current = [root]
current_depth = 0
order = []
parent = {root: None}
depth = {root: current_depth}
while current:
	current_depth += 1
	order.extend(current)
	nxt = []
	for node in current:
		for neighbor in adj_list[node]:
			if neighbor not in parent:
				parent[neighbor] = node
				depth[neighbor] = current_depth
				nxt.append(neighbor)
	current = nxt
score = Counter()
state = {}
for node in reversed(order):
	states = [state[neighbor] for neighbor in adj_list[node] if neighbor != parent[node]]
	largest = {s: [depth[node], node, 0] for s in elements[node]}
	if states:
		max_index = max(range(len(states)), key=lambda x: len(states[x]))
		if len(states[max_index]) > len(largest):
			(states[max_index], largest) = (largest, states[max_index])
	sets = defaultdict(list)
	for cur_state in states:
		for (set_no, v) in cur_state.items():
			sets[set_no].append(v)
	for (set_no, states) in sets.items():
		if len(states) == 1 and set_no not in largest:
			largest[set_no] = states[0]
			continue
		if set_no in largest:
			states.append(largest.pop(set_no))
		total_flow = 0
		total_node_sum = 0
		for (node_depth, node_sum, node_flow) in states:
			flow_delta = mul(node_depth - depth[node], node_sum)
			total_flow = add(total_flow, flow_delta, node_flow)
			total_node_sum += node_sum
		set_score = 0
		for (node_depth, node_sum, node_flow) in states:
			node_flow = add(mul(node_depth - depth[node], node_sum), node_flow)
			diff = mul(sub(total_flow, node_flow), node_sum)
			set_score = add(set_score, diff)
		score[set_no] = add(score[set_no], set_score)
		largest[set_no] = (depth[node], total_node_sum, total_flow)
	state[node] = largest
print(*(score[i] for i in range(q)), sep='\n')


from collections import Counter, defaultdict
MOD = 10 ** 9 + 7

def read_row():
	return (int(x) for x in input().split())

def mul(x, y):
	return x * y % MOD

def add(*args):
	return sum(args) % MOD

def sub(x, y):
	return (x - y) % MOD
(n, q) = read_row()
adj_list = defaultdict(list)
for _ in range(n - 1):
	(u, v) = read_row()
	adj_list[u].append(v)
	adj_list[v].append(u)
elements = {v: set() for v in adj_list}
for set_no in range(q):
	read_row()
	for x in read_row():
		elements[x].add(set_no)
root = next(iter(adj_list))
current = [root]
current_depth = 0
order = []
parent = {root: None}
depth = {root: current_depth}
while current:
	current_depth += 1
	order.extend(current)
	nxt = []
	for node in current:
		for neighbor in adj_list[node]:
			if neighbor not in parent:
				parent[neighbor] = node
				depth[neighbor] = current_depth
				nxt.append(neighbor)
	current = nxt
score = Counter()
state = {}
for node in reversed(order):
	states = [state[neighbor] for neighbor in adj_list[node] if neighbor != parent[node]]
	largest = {s: [depth[node], node, 0] for s in elements[node]}
	if states:
		max_index = max(range(len(states)), key=lambda x: len(states[x]))
		if len(states[max_index]) > len(largest):
			(states[max_index], largest) = (largest, states[max_index])
	sets = defaultdict(list)
	for cur_state in states:
		for (set_no, v) in cur_state.items():
			sets[set_no].append(v)
	for (set_no, states) in sets.items():
		if len(states) == 1 and set_no not in largest:
			largest[set_no] = states[0]
			continue
		if set_no in largest:
			states.append(largest.pop(set_no))
		total_flow = 0
		total_node_sum = 0
		for (node_depth, node_sum, node_flow) in states:
			flow_delta = mul(node_depth - depth[node], node_sum)
			total_flow = add(total_flow, flow_delta, node_flow)
			total_node_sum += node_sum
		set_score = 0
		for (node_depth, node_sum, node_flow) in states:
			node_flow = add(mul(node_depth - depth[node], node_sum), node_flow)
			diff = mul(sub(total_flow, node_flow), node_sum)
			set_score = add(set_score, diff)
		score[set_no] = add(score[set_no], set_score)
		largest[set_no] = (depth[node], total_node_sum, total_flow)
	state[node] = largest
print(*(score[i] for i in range(q)), sep='\n')


from collections import Counter, defaultdict
MOD = 10 ** 9 + 7

def read_row():
	return (int(x) for x in input().split())

def mul(x, y):
	return x * y % MOD

def add(*args):
	return sum(args) % MOD

def sub(x, y):
	return (x - y) % MOD
(n, q) = read_row()
adj_list = defaultdict(list)
for _ in range(n - 1):
	(u, v) = read_row()
	adj_list[u].append(v)
	adj_list[v].append(u)
elements = {v: set() for v in adj_list}
for set_no in range(q):
	read_row()
	for x in read_row():
		elements[x].add(set_no)
root = next(iter(adj_list))
current = [root]
current_depth = 0
order = []
parent = {root: None}
depth = {root: current_depth}
while current:
	current_depth += 1
	order.extend(current)
	nxt = []
	for node in current:
		for neighbor in adj_list[node]:
			if neighbor not in parent:
				parent[neighbor] = node
				depth[neighbor] = current_depth
				nxt.append(neighbor)
	current = nxt
score = Counter()
state = {}
for node in reversed(order):
	states = [state[neighbor] for neighbor in adj_list[node] if neighbor != parent[node]]
	largest = {s: [depth[node], node, 0] for s in elements[node]}
	if states:
		max_index = max(range(len(states)), key=lambda x: len(states[x]))
		if len(states[max_index]) > len(largest):
			(states[max_index], largest) = (largest, states[max_index])
	sets = defaultdict(list)
	for cur_state in states:
		for (set_no, v) in cur_state.items():
			sets[set_no].append(v)
	for (set_no, states) in sets.items():
		if len(states) == 1 and set_no not in largest:
			largest[set_no] = states[0]
			continue
		if set_no in largest:
			states.append(largest.pop(set_no))
		total_flow = 0
		total_node_sum = 0
		for (node_depth, node_sum, node_flow) in states:
			flow_delta = mul(node_depth - depth[node], node_sum)
			total_flow = add(total_flow, flow_delta, node_flow)
			total_node_sum += node_sum
		set_score = 0
		for (node_depth, node_sum, node_flow) in states:
			node_flow = add(mul(node_depth - depth[node], node_sum), node_flow)
			diff = mul(sub(total_flow, node_flow), node_sum)
			set_score = add(set_score, diff)
		score[set_no] = add(score[set_no], set_score)
		largest[set_no] = (depth[node], total_node_sum, total_flow)
	state[node] = largest
print(*(score[i] for i in range(q)), sep='\n')


from collections import Counter, defaultdict
MOD = 10 ** 9 + 7

def read_row():
	return (int(x) for x in input().split())

def mul(x, y):
	return x * y % MOD

def add(*args):
	return sum(args) % MOD

def sub(x, y):
	return (x - y) % MOD
(n, q) = read_row()
adj_list = defaultdict(list)
for _ in range(n - 1):
	(u, v) = read_row()
	adj_list[u].append(v)
	adj_list[v].append(u)
elements = {v: set() for v in adj_list}
for set_no in range(q):
	read_row()
	for x in read_row():
		elements[x].add(set_no)
root = next(iter(adj_list))
current = [root]
current_depth = 0
order = []
parent = {root: None}
depth = {root: current_depth}
while current:
	current_depth += 1
	order.extend(current)
	nxt = []
	for node in current:
		for neighbor in adj_list[node]:
			if neighbor not in parent:
				parent[neighbor] = node
				depth[neighbor] = current_depth
				nxt.append(neighbor)
	current = nxt
score = Counter()
state = {}
for node in reversed(order):
	states = [state[neighbor] for neighbor in adj_list[node] if neighbor != parent[node]]
	largest = {s: [depth[node], node, 0] for s in elements[node]}
	if states:
		max_index = max(range(len(states)), key=lambda x: len(states[x]))
		if len(states[max_index]) > len(largest):
			(states[max_index], largest) = (largest, states[max_index])
	sets = defaultdict(list)
	for cur_state in states:
		for (set_no, v) in cur_state.items():
			sets[set_no].append(v)
	for (set_no, states) in sets.items():
		if len(states) == 1 and set_no not in largest:
			largest[set_no] = states[0]
			continue
		if set_no in largest:
			states.append(largest.pop(set_no))
		total_flow = 0
		total_node_sum = 0
		for (node_depth, node_sum, node_flow) in states:
			flow_delta = mul(node_depth - depth[node], node_sum)
			total_flow = add(total_flow, flow_delta, node_flow)
			total_node_sum += node_sum
		set_score = 0
		for (node_depth, node_sum, node_flow) in states:
			node_flow = add(mul(node_depth - depth[node], node_sum), node_flow)
			diff = mul(sub(total_flow, node_flow), node_sum)
			set_score = add(set_score, diff)
		score[set_no] = add(score[set_no], set_score)
		largest[set_no] = (depth[node], total_node_sum, total_flow)
	state[node] = largest
print(*(score[i] for i in range(q)), sep='\n')


from collections import Counter, defaultdict
MOD = 10 ** 9 + 7

def read_row():
	return (int(x) for x in input().split())

def mul(x, y):
	return x * y % MOD

def add(*args):
	return sum(args) % MOD

def sub(x, y):
	return (x - y) % MOD
(n, q) = read_row()
adj_list = defaultdict(list)
for _ in range(n - 1):
	(u, v) = read_row()
	adj_list[u].append(v)
	adj_list[v].append(u)
elements = {v: set() for v in adj_list}
for set_no in range(q):
	read_row()
	for x in read_row():
		elements[x].add(set_no)
root = next(iter(adj_list))
current = [root]
current_depth = 0
order = []
parent = {root: None}
depth = {root: current_depth}
while current:
	current_depth += 1
	order.extend(current)
	nxt = []
	for node in current:
		for neighbor in adj_list[node]:
			if neighbor not in parent:
				parent[neighbor] = node
				depth[neighbor] = current_depth
				nxt.append(neighbor)
	current = nxt
score = Counter()
state = {}
for node in reversed(order):
	states = [state[neighbor] for neighbor in adj_list[node] if neighbor != parent[node]]
	largest = {s: [depth[node], node, 0] for s in elements[node]}
	if states:
		max_index = max(range(len(states)), key=lambda x: len(states[x]))
		if len(states[max_index]) > len(largest):
			(states[max_index], largest) = (largest, states[max_index])
	sets = defaultdict(list)
	for cur_state in states:
		for (set_no, v) in cur_state.items():
			sets[set_no].append(v)
	for (set_no, states) in sets.items():
		if len(states) == 1 and set_no not in largest:
			largest[set_no] = states[0]
			continue
		if set_no in largest:
			states.append(largest.pop(set_no))
		total_flow = 0
		total_node_sum = 0
		for (node_depth, node_sum, node_flow) in states:
			flow_delta = mul(node_depth - depth[node], node_sum)
			total_flow = add(total_flow, flow_delta, node_flow)
			total_node_sum += node_sum
		set_score = 0
		for (node_depth, node_sum, node_flow) in states:
			node_flow = add(mul(node_depth - depth[node], node_sum), node_flow)
			diff = mul(sub(total_flow, node_flow), node_sum)
			set_score = add(set_score, diff)
		score[set_no] = add(score[set_no], set_score)
		largest[set_no] = (depth[node], total_node_sum, total_flow)
	state[node] = largest
print(*(score[i] for i in range(q)), sep='\n')


def put(d, a, b):
	if a in d:
		d[a].append(b)
	else:
		d[a] = [b]

def main():
	for n in ns[::-1]:
		r = [tt[s] for s in tree[n] if s != f[n]]
		bst = {s: [gl[n], n, 0] for s in queries[n]}
		if r:
			o = max(range(len(r)), key=lambda a: len(r[a]))
			if len(r[o]) > len(bst):
				(r[o], bst) = (bst, r[o])
		ry = {}
		for ae in r:
			for (y, v) in ae.items():
				put(ry, y, v)
		for (y, r) in ry.items():
			(eq, z, t) = (0, 0, 0)
			if len(r) == 1 and y not in bst:
				bst[y] = r[0]
				continue
			if y in bst:
				r.append(bst.pop(y))
			for (d, v, c) in r:
				eq += (d - gl[n]) * v + c
				z += v
			for (d, v, c) in r:
				c += (d - gl[n]) * v
				diff = (eq - c) * v
				t += diff
			returns[y] += t
			bst[y] = (gl[n], z, eq)
		tt[n] = bst

def locate():
	q = [r]
	level = 0
	while q:
		level += 1
		tmp = []
		ns.extend(q)
		for n in q:
			for s in tree[n]:
				if s not in f:
					f[s] = n
					gl[s] = level
					tmp.append(s)
		q = tmp
tree = {}
tt = {}
(n, q) = map(int, input().split())
returns = [0] * q
for _ in range(n - 1):
	(a, b) = map(int, input().split())
	put(tree, a, b)
	put(tree, b, a)
queries = {a: set() for a in tree}
for y in range(q):
	input()
	for x in map(int, input().split()):
		queries[x].add(y)
r = next(iter(tree))
ns = []
f = {r: None}
gl = {r: 0}
locate()
main()
for s in returns:
	print(s % (10 ** 9 + 7))


from collections import Counter, defaultdict
MOD = 10 ** 9 + 7

def read_row():
	return (int(x) for x in input().split())

def mul(x, y):
	return x * y % MOD

def add(*args):
	return sum(args) % MOD

def sub(x, y):
	return (x - y) % MOD
(n, q) = read_row()
adj_list = defaultdict(list)
for _ in range(n - 1):
	(u, v) = read_row()
	adj_list[u].append(v)
	adj_list[v].append(u)
elements = {v: set() for v in adj_list}
for set_no in range(q):
	read_row()
	for x in read_row():
		elements[x].add(set_no)
root = next(iter(adj_list))
current = [root]
current_depth = 0
order = []
parent = {root: None}
depth = {root: current_depth}
while current:
	current_depth += 1
	order.extend(current)
	nxt = []
	for node in current:
		for neighbor in adj_list[node]:
			if neighbor not in parent:
				parent[neighbor] = node
				depth[neighbor] = current_depth
				nxt.append(neighbor)
	current = nxt
score = Counter()
state = {}
for node in reversed(order):
	states = [state[neighbor] for neighbor in adj_list[node] if neighbor != parent[node]]
	largest = {s: [depth[node], node, 0] for s in elements[node]}
	if states:
		max_index = max(range(len(states)), key=lambda x: len(states[x]))
		if len(states[max_index]) > len(largest):
			(states[max_index], largest) = (largest, states[max_index])
	sets = defaultdict(list)
	for cur_state in states:
		for (set_no, v) in cur_state.items():
			sets[set_no].append(v)
	for (set_no, states) in sets.items():
		if len(states) == 1 and set_no not in largest:
			largest[set_no] = states[0]
			continue
		if set_no in largest:
			states.append(largest.pop(set_no))
		total_flow = 0
		total_node_sum = 0
		for (node_depth, node_sum, node_flow) in states:
			flow_delta = mul(node_depth - depth[node], node_sum)
			total_flow = add(total_flow, flow_delta, node_flow)
			total_node_sum += node_sum
		set_score = 0
		for (node_depth, node_sum, node_flow) in states:
			node_flow = add(mul(node_depth - depth[node], node_sum), node_flow)
			diff = mul(sub(total_flow, node_flow), node_sum)
			set_score = add(set_score, diff)
		score[set_no] = add(score[set_no], set_score)
		largest[set_no] = (depth[node], total_node_sum, total_flow)
	state[node] = largest
print(*(score[i] for i in range(q)), sep='\n')


from collections import Counter, defaultdict
MOD = 10 ** 9 + 7

def read_row():
	return (int(x) for x in input().split())

def mul(x, y):
	return x * y % MOD

def add(*args):
	return sum(args) % MOD

def sub(x, y):
	return (x - y) % MOD
(n, q) = read_row()
adj_list = defaultdict(list)
for _ in range(n - 1):
	(u, v) = read_row()
	adj_list[u].append(v)
	adj_list[v].append(u)
elements = {v: set() for v in adj_list}
for set_no in range(q):
	read_row()
	for x in read_row():
		elements[x].add(set_no)
root = next(iter(adj_list))
current = [root]
current_depth = 0
order = []
parent = {root: None}
depth = {root: current_depth}
while current:
	current_depth += 1
	order.extend(current)
	nxt = []
	for node in current:
		for neighbor in adj_list[node]:
			if neighbor not in parent:
				parent[neighbor] = node
				depth[neighbor] = current_depth
				nxt.append(neighbor)
	current = nxt
score = Counter()
state = {}
for node in reversed(order):
	states = [state[neighbor] for neighbor in adj_list[node] if neighbor != parent[node]]
	largest = {s: [depth[node], node, 0] for s in elements[node]}
	if states:
		max_index = max(range(len(states)), key=lambda x: len(states[x]))
		if len(states[max_index]) > len(largest):
			(states[max_index], largest) = (largest, states[max_index])
	sets = defaultdict(list)
	for cur_state in states:
		for (set_no, v) in cur_state.items():
			sets[set_no].append(v)
	for (set_no, states) in sets.items():
		if len(states) == 1 and set_no not in largest:
			largest[set_no] = states[0]
			continue
		if set_no in largest:
			states.append(largest.pop(set_no))
		total_flow = 0
		total_node_sum = 0
		for (node_depth, node_sum, node_flow) in states:
			flow_delta = mul(node_depth - depth[node], node_sum)
			total_flow = add(total_flow, flow_delta, node_flow)
			total_node_sum += node_sum
		set_score = 0
		for (node_depth, node_sum, node_flow) in states:
			node_flow = add(mul(node_depth - depth[node], node_sum), node_flow)
			diff = mul(sub(total_flow, node_flow), node_sum)
			set_score = add(set_score, diff)
		score[set_no] = add(score[set_no], set_score)
		largest[set_no] = (depth[node], total_node_sum, total_flow)
	state[node] = largest
print(*(score[i] for i in range(q)), sep='\n')


from collections import Counter, defaultdict
MOD = 10 ** 9 + 7

def read_row():
	return (int(x) for x in input().split())

def mul(x, y):
	return x * y % MOD

def add(*args):
	return sum(args) % MOD

def sub(x, y):
	return (x - y) % MOD
(n, q) = read_row()
adj_list = defaultdict(list)
for _ in range(n - 1):
	(u, v) = read_row()
	adj_list[u].append(v)
	adj_list[v].append(u)
elements = {v: set() for v in adj_list}
for set_no in range(q):
	read_row()
	for x in read_row():
		elements[x].add(set_no)
root = next(iter(adj_list))
current = [root]
current_depth = 0
order = []
parent = {root: None}
depth = {root: current_depth}
while current:
	current_depth += 1
	order.extend(current)
	nxt = []
	for node in current:
		for neighbor in adj_list[node]:
			if neighbor not in parent:
				parent[neighbor] = node
				depth[neighbor] = current_depth
				nxt.append(neighbor)
	current = nxt
score = Counter()
state = {}
for node in reversed(order):
	states = [state[neighbor] for neighbor in adj_list[node] if neighbor != parent[node]]
	largest = {s: [depth[node], node, 0] for s in elements[node]}
	if states:
		max_index = max(range(len(states)), key=lambda x: len(states[x]))
		if len(states[max_index]) > len(largest):
			(states[max_index], largest) = (largest, states[max_index])
	sets = defaultdict(list)
	for cur_state in states:
		for (set_no, v) in cur_state.items():
			sets[set_no].append(v)
	for (set_no, states) in sets.items():
		if len(states) == 1 and set_no not in largest:
			largest[set_no] = states[0]
			continue
		if set_no in largest:
			states.append(largest.pop(set_no))
		total_flow = 0
		total_node_sum = 0
		for (node_depth, node_sum, node_flow) in states:
			flow_delta = mul(node_depth - depth[node], node_sum)
			total_flow = add(total_flow, flow_delta, node_flow)
			total_node_sum += node_sum
		set_score = 0
		for (node_depth, node_sum, node_flow) in states:
			node_flow = add(mul(node_depth - depth[node], node_sum), node_flow)
			diff = mul(sub(total_flow, node_flow), node_sum)
			set_score = add(set_score, diff)
		score[set_no] = add(score[set_no], set_score)
		largest[set_no] = (depth[node], total_node_sum, total_flow)
	state[node] = largest
print(*(score[i] for i in range(q)), sep='\n')


from collections import Counter, defaultdict
MOD = 10 ** 9 + 7

def read_row():
	return (int(x) for x in input().split())

def mul(x, y):
	return x * y % MOD

def add(*args):
	return sum(args) % MOD

def sub(x, y):
	return (x - y) % MOD
(n, q) = read_row()
adj_list = defaultdict(list)
for _ in range(n - 1):
	(u, v) = read_row()
	adj_list[u].append(v)
	adj_list[v].append(u)
elements = {v: set() for v in adj_list}
for set_no in range(q):
	read_row()
	for x in read_row():
		elements[x].add(set_no)
root = next(iter(adj_list))
current = [root]
current_depth = 0
order = []
parent = {root: None}
depth = {root: current_depth}
while current:
	current_depth += 1
	order.extend(current)
	nxt = []
	for node in current:
		for neighbor in adj_list[node]:
			if neighbor not in parent:
				parent[neighbor] = node
				depth[neighbor] = current_depth
				nxt.append(neighbor)
	current = nxt
score = Counter()
state = {}
for node in reversed(order):
	states = [state[neighbor] for neighbor in adj_list[node] if neighbor != parent[node]]
	largest = {s: [depth[node], node, 0] for s in elements[node]}
	if states:
		max_index = max(range(len(states)), key=lambda x: len(states[x]))
		if len(states[max_index]) > len(largest):
			(states[max_index], largest) = (largest, states[max_index])
	sets = defaultdict(list)
	for cur_state in states:
		for (set_no, v) in cur_state.items():
			sets[set_no].append(v)
	for (set_no, states) in sets.items():
		if len(states) == 1 and set_no not in largest:
			largest[set_no] = states[0]
			continue
		if set_no in largest:
			states.append(largest.pop(set_no))
		total_flow = 0
		total_node_sum = 0
		for (node_depth, node_sum, node_flow) in states:
			flow_delta = mul(node_depth - depth[node], node_sum)
			total_flow = add(total_flow, flow_delta, node_flow)
			total_node_sum += node_sum
		set_score = 0
		for (node_depth, node_sum, node_flow) in states:
			node_flow = add(mul(node_depth - depth[node], node_sum), node_flow)
			diff = mul(sub(total_flow, node_flow), node_sum)
			set_score = add(set_score, diff)
		score[set_no] = add(score[set_no], set_score)
		largest[set_no] = (depth[node], total_node_sum, total_flow)
	state[node] = largest
print(*(score[i] for i in range(q)), sep='\n')


from collections import Counter, defaultdict
MOD = 10 ** 9 + 7

def read_row():
	return (int(x) for x in input().split())

def mul(x, y):
	return x * y % MOD

def add(*args):
	return sum(args) % MOD

def sub(x, y):
	return (x - y) % MOD
(n, q) = read_row()
adj_list = defaultdict(list)
for _ in range(n - 1):
	(u, v) = read_row()
	adj_list[u].append(v)
	adj_list[v].append(u)
elements = {v: set() for v in adj_list}
for set_no in range(q):
	read_row()
	for x in read_row():
		elements[x].add(set_no)
root = next(iter(adj_list))
current = [root]
current_depth = 0
order = []
parent = {root: None}
depth = {root: current_depth}
while current:
	current_depth += 1
	order.extend(current)
	nxt = []
	for node in current:
		for neighbor in adj_list[node]:
			if neighbor not in parent:
				parent[neighbor] = node
				depth[neighbor] = current_depth
				nxt.append(neighbor)
	current = nxt
score = Counter()
state = {}
for node in reversed(order):
	states = [state[neighbor] for neighbor in adj_list[node] if neighbor != parent[node]]
	largest = {s: [depth[node], node, 0] for s in elements[node]}
	if states:
		max_index = max(range(len(states)), key=lambda x: len(states[x]))
		if len(states[max_index]) > len(largest):
			(states[max_index], largest) = (largest, states[max_index])
	sets = defaultdict(list)
	for cur_state in states:
		for (set_no, v) in cur_state.items():
			sets[set_no].append(v)
	for (set_no, states) in sets.items():
		if len(states) == 1 and set_no not in largest:
			largest[set_no] = states[0]
			continue
		if set_no in largest:
			states.append(largest.pop(set_no))
		total_flow = 0
		total_node_sum = 0
		for (node_depth, node_sum, node_flow) in states:
			flow_delta = mul(node_depth - depth[node], node_sum)
			total_flow = add(total_flow, flow_delta, node_flow)
			total_node_sum += node_sum
		set_score = 0
		for (node_depth, node_sum, node_flow) in states:
			node_flow = add(mul(node_depth - depth[node], node_sum), node_flow)
			diff = mul(sub(total_flow, node_flow), node_sum)
			set_score = add(set_score, diff)
		score[set_no] = add(score[set_no], set_score)
		largest[set_no] = (depth[node], total_node_sum, total_flow)
	state[node] = largest
print(*(score[i] for i in range(q)), sep='\n')


from collections import Counter, defaultdict
MOD = 10 ** 9 + 7

def read_row():
	return (int(x) for x in input().split())

def mul(x, y):
	return x * y % MOD

def add(*args):
	return sum(args) % MOD

def sub(x, y):
	return (x - y) % MOD
(n, q) = read_row()
adj_list = defaultdict(list)
for _ in range(n - 1):
	(u, v) = read_row()
	adj_list[u].append(v)
	adj_list[v].append(u)
elements = {v: set() for v in adj_list}
for set_no in range(q):
	read_row()
	for x in read_row():
		elements[x].add(set_no)
root = next(iter(adj_list))
current = [root]
current_depth = 0
order = []
parent = {root: None}
depth = {root: current_depth}
while current:
	current_depth += 1
	order.extend(current)
	nxt = []
	for node in current:
		for neighbor in adj_list[node]:
			if neighbor not in parent:
				parent[neighbor] = node
				depth[neighbor] = current_depth
				nxt.append(neighbor)
	current = nxt
score = Counter()
state = {}
for node in reversed(order):
	states = [state[neighbor] for neighbor in adj_list[node] if neighbor != parent[node]]
	largest = {s: [depth[node], node, 0] for s in elements[node]}
	if states:
		max_index = max(range(len(states)), key=lambda x: len(states[x]))
		if len(states[max_index]) > len(largest):
			(states[max_index], largest) = (largest, states[max_index])
	sets = defaultdict(list)
	for cur_state in states:
		for (set_no, v) in cur_state.items():
			sets[set_no].append(v)
	for (set_no, states) in sets.items():
		if len(states) == 1 and set_no not in largest:
			largest[set_no] = states[0]
			continue
		if set_no in largest:
			states.append(largest.pop(set_no))
		total_flow = 0
		total_node_sum = 0
		for (node_depth, node_sum, node_flow) in states:
			flow_delta = mul(node_depth - depth[node], node_sum)
			total_flow = add(total_flow, flow_delta, node_flow)
			total_node_sum += node_sum
		set_score = 0
		for (node_depth, node_sum, node_flow) in states:
			node_flow = add(mul(node_depth - depth[node], node_sum), node_flow)
			diff = mul(sub(total_flow, node_flow), node_sum)
			set_score = add(set_score, diff)
		score[set_no] = add(score[set_no], set_score)
		largest[set_no] = (depth[node], total_node_sum, total_flow)
	state[node] = largest
print(*(score[i] for i in range(q)), sep='\n')


from collections import Counter, defaultdict
MOD = 10 ** 9 + 7

def read_row():
	return (int(x) for x in input().split())

def mul(x, y):
	return x * y % MOD

def add(*args):
	return sum(args) % MOD

def sub(x, y):
	return (x - y) % MOD
(n, q) = read_row()
adj_list = defaultdict(list)
for _ in range(n - 1):
	(u, v) = read_row()
	adj_list[u].append(v)
	adj_list[v].append(u)
elements = {v: set() for v in adj_list}
for set_no in range(q):
	read_row()
	for x in read_row():
		elements[x].add(set_no)
root = next(iter(adj_list))
current = [root]
current_depth = 0
order = []
parent = {root: None}
depth = {root: current_depth}
while current:
	current_depth += 1
	order.extend(current)
	nxt = []
	for node in current:
		for neighbor in adj_list[node]:
			if neighbor not in parent:
				parent[neighbor] = node
				depth[neighbor] = current_depth
				nxt.append(neighbor)
	current = nxt
score = Counter()
state = {}
for node in reversed(order):
	states = [state[neighbor] for neighbor in adj_list[node] if neighbor != parent[node]]
	largest = {s: [depth[node], node, 0] for s in elements[node]}
	if states:
		max_index = max(range(len(states)), key=lambda x: len(states[x]))
		if len(states[max_index]) > len(largest):
			(states[max_index], largest) = (largest, states[max_index])
	sets = defaultdict(list)
	for cur_state in states:
		for (set_no, v) in cur_state.items():
			sets[set_no].append(v)
	for (set_no, states) in sets.items():
		if len(states) == 1 and set_no not in largest:
			largest[set_no] = states[0]
			continue
		if set_no in largest:
			states.append(largest.pop(set_no))
		total_flow = 0
		total_node_sum = 0
		for (node_depth, node_sum, node_flow) in states:
			flow_delta = mul(node_depth - depth[node], node_sum)
			total_flow = add(total_flow, flow_delta, node_flow)
			total_node_sum += node_sum
		set_score = 0
		for (node_depth, node_sum, node_flow) in states:
			node_flow = add(mul(node_depth - depth[node], node_sum), node_flow)
			diff = mul(sub(total_flow, node_flow), node_sum)
			set_score = add(set_score, diff)
		score[set_no] = add(score[set_no], set_score)
		largest[set_no] = (depth[node], total_node_sum, total_flow)
	state[node] = largest
print(*(score[i] for i in range(q)), sep='\n')


from collections import Counter, defaultdict
MOD = 10 ** 9 + 7

def read_row():
	return (int(x) for x in input().split())

def mul(x, y):
	return x * y % MOD

def add(*args):
	return sum(args) % MOD

def sub(x, y):
	return (x - y) % MOD
(n, q) = read_row()
adj_list = defaultdict(list)
for _ in range(n - 1):
	(u, v) = read_row()
	adj_list[u].append(v)
	adj_list[v].append(u)
elements = {v: set() for v in adj_list}
for set_no in range(q):
	read_row()
	for x in read_row():
		elements[x].add(set_no)
root = next(iter(adj_list))
current = [root]
current_depth = 0
order = []
parent = {root: None}
depth = {root: current_depth}
while current:
	current_depth += 1
	order.extend(current)
	nxt = []
	for node in current:
		for neighbor in adj_list[node]:
			if neighbor not in parent:
				parent[neighbor] = node
				depth[neighbor] = current_depth
				nxt.append(neighbor)
	current = nxt
score = Counter()
state = {}
for node in reversed(order):
	states = [state[neighbor] for neighbor in adj_list[node] if neighbor != parent[node]]
	largest = {s: [depth[node], node, 0] for s in elements[node]}
	if states:
		max_index = max(range(len(states)), key=lambda x: len(states[x]))
		if len(states[max_index]) > len(largest):
			(states[max_index], largest) = (largest, states[max_index])
	sets = defaultdict(list)
	for cur_state in states:
		for (set_no, v) in cur_state.items():
			sets[set_no].append(v)
	for (set_no, states) in sets.items():
		if len(states) == 1 and set_no not in largest:
			largest[set_no] = states[0]
			continue
		if set_no in largest:
			states.append(largest.pop(set_no))
		total_flow = 0
		total_node_sum = 0
		for (node_depth, node_sum, node_flow) in states:
			flow_delta = mul(node_depth - depth[node], node_sum)
			total_flow = add(total_flow, flow_delta, node_flow)
			total_node_sum += node_sum
		set_score = 0
		for (node_depth, node_sum, node_flow) in states:
			node_flow = add(mul(node_depth - depth[node], node_sum), node_flow)
			diff = mul(sub(total_flow, node_flow), node_sum)
			set_score = add(set_score, diff)
		score[set_no] = add(score[set_no], set_score)
		largest[set_no] = (depth[node], total_node_sum, total_flow)
	state[node] = largest
print(*(score[i] for i in range(q)), sep='\n')


def put(d, a, b):
	if a in d:
		d[a].append(b)
	else:
		d[a] = [b]

def main():
	for n in ns[::-1]:
		r = [tt[s] for s in tree[n] if s != f[n]]
		bst = {s: [gl[n], n, 0] for s in queries[n]}
		if r:
			o = max(range(len(r)), key=lambda a: len(r[a]))
			if len(r[o]) > len(bst):
				(r[o], bst) = (bst, r[o])
		ry = {}
		for ae in r:
			for (y, v) in ae.items():
				put(ry, y, v)
		for (y, r) in ry.items():
			(eq, z, t) = (0, 0, 0)
			if len(r) == 1 and y not in bst:
				bst[y] = r[0]
				continue
			if y in bst:
				r.append(bst.pop(y))
			for (d, v, c) in r:
				eq += (d - gl[n]) * v + c
				z += v
			for (d, v, c) in r:
				c += (d - gl[n]) * v
				diff = (eq - c) * v
				t += diff
			returns[y] += t
			bst[y] = (gl[n], z, eq)
		tt[n] = bst

def locate():
	q = [r]
	level = 0
	while q:
		level += 1
		tmp = []
		ns.extend(q)
		for n in q:
			for s in tree[n]:
				if s not in f:
					f[s] = n
					gl[s] = level
					tmp.append(s)
		q = tmp
tree = {}
tt = {}
(n, q) = map(int, input().split())
returns = [0] * q
for _ in range(n - 1):
	(a, b) = map(int, input().split())
	put(tree, a, b)
	put(tree, b, a)
queries = {a: set() for a in tree}
for y in range(q):
	input()
	for x in map(int, input().split()):
		queries[x].add(y)
r = next(iter(tree))
ns = []
f = {r: None}
gl = {r: 0}
locate()
main()
for s in returns:
	print(s % (10 ** 9 + 7))


from collections import Counter, defaultdict
MOD = 10 ** 9 + 7

def read_row():
	return (int(x) for x in input().split())

def mul(x, y):
	return x * y % MOD

def add(*args):
	return sum(args) % MOD

def sub(x, y):
	return (x - y) % MOD
(n, q) = read_row()
adj_list = defaultdict(list)
for _ in range(n - 1):
	(u, v) = read_row()
	adj_list[u].append(v)
	adj_list[v].append(u)
elements = {v: set() for v in adj_list}
for set_no in range(q):
	read_row()
	for x in read_row():
		elements[x].add(set_no)
root = next(iter(adj_list))
current = [root]
current_depth = 0
order = []
parent = {root: None}
depth = {root: current_depth}
while current:
	current_depth += 1
	order.extend(current)
	nxt = []
	for node in current:
		for neighbor in adj_list[node]:
			if neighbor not in parent:
				parent[neighbor] = node
				depth[neighbor] = current_depth
				nxt.append(neighbor)
	current = nxt
score = Counter()
state = {}
for node in reversed(order):
	states = [state[neighbor] for neighbor in adj_list[node] if neighbor != parent[node]]
	largest = {s: [depth[node], node, 0] for s in elements[node]}
	if states:
		max_index = max(range(len(states)), key=lambda x: len(states[x]))
		if len(states[max_index]) > len(largest):
			(states[max_index], largest) = (largest, states[max_index])
	sets = defaultdict(list)
	for cur_state in states:
		for (set_no, v) in cur_state.items():
			sets[set_no].append(v)
	for (set_no, states) in sets.items():
		if len(states) == 1 and set_no not in largest:
			largest[set_no] = states[0]
			continue
		if set_no in largest:
			states.append(largest.pop(set_no))
		total_flow = 0
		total_node_sum = 0
		for (node_depth, node_sum, node_flow) in states:
			flow_delta = mul(node_depth - depth[node], node_sum)
			total_flow = add(total_flow, flow_delta, node_flow)
			total_node_sum += node_sum
		set_score = 0
		for (node_depth, node_sum, node_flow) in states:
			node_flow = add(mul(node_depth - depth[node], node_sum), node_flow)
			diff = mul(sub(total_flow, node_flow), node_sum)
			set_score = add(set_score, diff)
		score[set_no] = add(score[set_no], set_score)
		largest[set_no] = (depth[node], total_node_sum, total_flow)
	state[node] = largest
print(*(score[i] for i in range(q)), sep='\n')


from collections import Counter, defaultdict
MOD = 10 ** 9 + 7

def read_row():
	return (int(x) for x in input().split())

def mul(x, y):
	return x * y % MOD

def add(*args):
	return sum(args) % MOD

def sub(x, y):
	return (x - y) % MOD
(n, q) = read_row()
adj_list = defaultdict(list)
for _ in range(n - 1):
	(u, v) = read_row()
	adj_list[u].append(v)
	adj_list[v].append(u)
elements = {v: set() for v in adj_list}
for set_no in range(q):
	read_row()
	for x in read_row():
		elements[x].add(set_no)
root = next(iter(adj_list))
current = [root]
current_depth = 0
order = []
parent = {root: None}
depth = {root: current_depth}
while current:
	current_depth += 1
	order.extend(current)
	nxt = []
	for node in current:
		for neighbor in adj_list[node]:
			if neighbor not in parent:
				parent[neighbor] = node
				depth[neighbor] = current_depth
				nxt.append(neighbor)
	current = nxt
score = Counter()
state = {}
for node in reversed(order):
	states = [state[neighbor] for neighbor in adj_list[node] if neighbor != parent[node]]
	largest = {s: [depth[node], node, 0] for s in elements[node]}
	if states:
		max_index = max(range(len(states)), key=lambda x: len(states[x]))
		if len(states[max_index]) > len(largest):
			(states[max_index], largest) = (largest, states[max_index])
	sets = defaultdict(list)
	for cur_state in states:
		for (set_no, v) in cur_state.items():
			sets[set_no].append(v)
	for (set_no, states) in sets.items():
		if len(states) == 1 and set_no not in largest:
			largest[set_no] = states[0]
			continue
		if set_no in largest:
			states.append(largest.pop(set_no))
		total_flow = 0
		total_node_sum = 0
		for (node_depth, node_sum, node_flow) in states:
			flow_delta = mul(node_depth - depth[node], node_sum)
			total_flow = add(total_flow, flow_delta, node_flow)
			total_node_sum += node_sum
		set_score = 0
		for (node_depth, node_sum, node_flow) in states:
			node_flow = add(mul(node_depth - depth[node], node_sum), node_flow)
			diff = mul(sub(total_flow, node_flow), node_sum)
			set_score = add(set_score, diff)
		score[set_no] = add(score[set_no], set_score)
		largest[set_no] = (depth[node], total_node_sum, total_flow)
	state[node] = largest
print(*(score[i] for i in range(q)), sep='\n')


from collections import Counter, defaultdict
MOD = 10 ** 9 + 7

def read_row():
	return (int(x) for x in input().split())

def mul(x, y):
	return x * y % MOD

def add(*args):
	return sum(args) % MOD

def sub(x, y):
	return (x - y) % MOD
(n, q) = read_row()
adj_list = defaultdict(list)
for _ in range(n - 1):
	(u, v) = read_row()
	adj_list[u].append(v)
	adj_list[v].append(u)
elements = {v: set() for v in adj_list}
for set_no in range(q):
	read_row()
	for x in read_row():
		elements[x].add(set_no)
root = next(iter(adj_list))
current = [root]
current_depth = 0
order = []
parent = {root: None}
depth = {root: current_depth}
while current:
	current_depth += 1
	order.extend(current)
	nxt = []
	for node in current:
		for neighbor in adj_list[node]:
			if neighbor not in parent:
				parent[neighbor] = node
				depth[neighbor] = current_depth
				nxt.append(neighbor)
	current = nxt
score = Counter()
state = {}
for node in reversed(order):
	states = [state[neighbor] for neighbor in adj_list[node] if neighbor != parent[node]]
	largest = {s: [depth[node], node, 0] for s in elements[node]}
	if states:
		max_index = max(range(len(states)), key=lambda x: len(states[x]))
		if len(states[max_index]) > len(largest):
			(states[max_index], largest) = (largest, states[max_index])
	sets = defaultdict(list)
	for cur_state in states:
		for (set_no, v) in cur_state.items():
			sets[set_no].append(v)
	for (set_no, states) in sets.items():
		if len(states) == 1 and set_no not in largest:
			largest[set_no] = states[0]
			continue
		if set_no in largest:
			states.append(largest.pop(set_no))
		total_flow = 0
		total_node_sum = 0
		for (node_depth, node_sum, node_flow) in states:
			flow_delta = mul(node_depth - depth[node], node_sum)
			total_flow = add(total_flow, flow_delta, node_flow)
			total_node_sum += node_sum
		set_score = 0
		for (node_depth, node_sum, node_flow) in states:
			node_flow = add(mul(node_depth - depth[node], node_sum), node_flow)
			diff = mul(sub(total_flow, node_flow), node_sum)
			set_score = add(set_score, diff)
		score[set_no] = add(score[set_no], set_score)
		largest[set_no] = (depth[node], total_node_sum, total_flow)
	state[node] = largest
print(*(score[i] for i in range(q)), sep='\n')


from collections import Counter, defaultdict
MOD = 10 ** 9 + 7

def read_row():
	return (int(x) for x in input().split())

def mul(x, y):
	return x * y % MOD

def add(*args):
	return sum(args) % MOD

def sub(x, y):
	return (x - y) % MOD
(n, q) = read_row()
adj_list = defaultdict(list)
for _ in range(n - 1):
	(u, v) = read_row()
	adj_list[u].append(v)
	adj_list[v].append(u)
elements = {v: set() for v in adj_list}
for set_no in range(q):
	read_row()
	for x in read_row():
		elements[x].add(set_no)
root = next(iter(adj_list))
current = [root]
current_depth = 0
order = []
parent = {root: None}
depth = {root: current_depth}
while current:
	current_depth += 1
	order.extend(current)
	nxt = []
	for node in current:
		for neighbor in adj_list[node]:
			if neighbor not in parent:
				parent[neighbor] = node
				depth[neighbor] = current_depth
				nxt.append(neighbor)
	current = nxt
score = Counter()
state = {}
for node in reversed(order):
	states = [state[neighbor] for neighbor in adj_list[node] if neighbor != parent[node]]
	largest = {s: [depth[node], node, 0] for s in elements[node]}
	if states:
		max_index = max(range(len(states)), key=lambda x: len(states[x]))
		if len(states[max_index]) > len(largest):
			(states[max_index], largest) = (largest, states[max_index])
	sets = defaultdict(list)
	for cur_state in states:
		for (set_no, v) in cur_state.items():
			sets[set_no].append(v)
	for (set_no, states) in sets.items():
		if len(states) == 1 and set_no not in largest:
			largest[set_no] = states[0]
			continue
		if set_no in largest:
			states.append(largest.pop(set_no))
		total_flow = 0
		total_node_sum = 0
		for (node_depth, node_sum, node_flow) in states:
			flow_delta = mul(node_depth - depth[node], node_sum)
			total_flow = add(total_flow, flow_delta, node_flow)
			total_node_sum += node_sum
		set_score = 0
		for (node_depth, node_sum, node_flow) in states:
			node_flow = add(mul(node_depth - depth[node], node_sum), node_flow)
			diff = mul(sub(total_flow, node_flow), node_sum)
			set_score = add(set_score, diff)
		score[set_no] = add(score[set_no], set_score)
		largest[set_no] = (depth[node], total_node_sum, total_flow)
	state[node] = largest
print(*(score[i] for i in range(q)), sep='\n')


def put(d, a, b):
	if a in d:
		d[a].append(b)
	else:
		d[a] = [b]

def main():
	for n in ns[::-1]:
		r = [tt[s] for s in tree[n] if s != f[n]]
		bst = {s: [gl[n], n, 0] for s in queries[n]}
		if r:
			o = max(range(len(r)), key=lambda a: len(r[a]))
			if len(r[o]) > len(bst):
				(r[o], bst) = (bst, r[o])
		ry = {}
		for ae in r:
			for (y, v) in ae.items():
				put(ry, y, v)
		for (y, r) in ry.items():
			(eq, z, t) = (0, 0, 0)
			if len(r) == 1 and y not in bst:
				bst[y] = r[0]
				continue
			if y in bst:
				r.append(bst.pop(y))
			for (d, v, c) in r:
				eq += (d - gl[n]) * v + c
				z += v
			for (d, v, c) in r:
				c += (d - gl[n]) * v
				diff = (eq - c) * v
				t += diff
			returns[y] += t
			bst[y] = (gl[n], z, eq)
		tt[n] = bst

def locate():
	q = [r]
	level = 0
	while q:
		level += 1
		tmp = []
		ns.extend(q)
		for n in q:
			for s in tree[n]:
				if s not in f:
					f[s] = n
					gl[s] = level
					tmp.append(s)
		q = tmp
tree = {}
tt = {}
(n, q) = map(int, input().split())
returns = [0] * q
for _ in range(n - 1):
	(a, b) = map(int, input().split())
	put(tree, a, b)
	put(tree, b, a)
queries = {a: set() for a in tree}
for y in range(q):
	input()
	for x in map(int, input().split()):
		queries[x].add(y)
r = next(iter(tree))
ns = []
f = {r: None}
gl = {r: 0}
locate()
main()
for s in returns:
	print(s % (10 ** 9 + 7))


from collections import Counter, defaultdict
MOD = 10 ** 9 + 7

def read_row():
	return (int(x) for x in input().split())

def mul(x, y):
	return x * y % MOD

def add(*args):
	return sum(args) % MOD

def sub(x, y):
	return (x - y) % MOD
(n, q) = read_row()
adj_list = defaultdict(list)
for _ in range(n - 1):
	(u, v) = read_row()
	adj_list[u].append(v)
	adj_list[v].append(u)
elements = {v: set() for v in adj_list}
for set_no in range(q):
	read_row()
	for x in read_row():
		elements[x].add(set_no)
root = next(iter(adj_list))
current = [root]
current_depth = 0
order = []
parent = {root: None}
depth = {root: current_depth}
while current:
	current_depth += 1
	order.extend(current)
	nxt = []
	for node in current:
		for neighbor in adj_list[node]:
			if neighbor not in parent:
				parent[neighbor] = node
				depth[neighbor] = current_depth
				nxt.append(neighbor)
	current = nxt
score = Counter()
state = {}
for node in reversed(order):
	states = [state[neighbor] for neighbor in adj_list[node] if neighbor != parent[node]]
	largest = {s: [depth[node], node, 0] for s in elements[node]}
	if states:
		max_index = max(range(len(states)), key=lambda x: len(states[x]))
		if len(states[max_index]) > len(largest):
			(states[max_index], largest) = (largest, states[max_index])
	sets = defaultdict(list)
	for cur_state in states:
		for (set_no, v) in cur_state.items():
			sets[set_no].append(v)
	for (set_no, states) in sets.items():
		if len(states) == 1 and set_no not in largest:
			largest[set_no] = states[0]
			continue
		if set_no in largest:
			states.append(largest.pop(set_no))
		total_flow = 0
		total_node_sum = 0
		for (node_depth, node_sum, node_flow) in states:
			flow_delta = mul(node_depth - depth[node], node_sum)
			total_flow = add(total_flow, flow_delta, node_flow)
			total_node_sum += node_sum
		set_score = 0
		for (node_depth, node_sum, node_flow) in states:
			node_flow = add(mul(node_depth - depth[node], node_sum), node_flow)
			diff = mul(sub(total_flow, node_flow), node_sum)
			set_score = add(set_score, diff)
		score[set_no] = add(score[set_no], set_score)
		largest[set_no] = (depth[node], total_node_sum, total_flow)
	state[node] = largest
print(*(score[i] for i in range(q)), sep='\n')


from collections import Counter, defaultdict
MOD = 10 ** 9 + 7

def read_row():
	return (int(x) for x in input().split())

def mul(x, y):
	return x * y % MOD

def add(*args):
	return sum(args) % MOD

def sub(x, y):
	return (x - y) % MOD
(n, q) = read_row()
adj_list = defaultdict(list)
for _ in range(n - 1):
	(u, v) = read_row()
	adj_list[u].append(v)
	adj_list[v].append(u)
elements = {v: set() for v in adj_list}
for set_no in range(q):
	read_row()
	for x in read_row():
		elements[x].add(set_no)
root = next(iter(adj_list))
current = [root]
current_depth = 0
order = []
parent = {root: None}
depth = {root: current_depth}
while current:
	current_depth += 1
	order.extend(current)
	nxt = []
	for node in current:
		for neighbor in adj_list[node]:
			if neighbor not in parent:
				parent[neighbor] = node
				depth[neighbor] = current_depth
				nxt.append(neighbor)
	current = nxt
score = Counter()
state = {}
for node in reversed(order):
	states = [state[neighbor] for neighbor in adj_list[node] if neighbor != parent[node]]
	largest = {s: [depth[node], node, 0] for s in elements[node]}
	if states:
		max_index = max(range(len(states)), key=lambda x: len(states[x]))
		if len(states[max_index]) > len(largest):
			(states[max_index], largest) = (largest, states[max_index])
	sets = defaultdict(list)
	for cur_state in states:
		for (set_no, v) in cur_state.items():
			sets[set_no].append(v)
	for (set_no, states) in sets.items():
		if len(states) == 1 and set_no not in largest:
			largest[set_no] = states[0]
			continue
		if set_no in largest:
			states.append(largest.pop(set_no))
		total_flow = 0
		total_node_sum = 0
		for (node_depth, node_sum, node_flow) in states:
			flow_delta = mul(node_depth - depth[node], node_sum)
			total_flow = add(total_flow, flow_delta, node_flow)
			total_node_sum += node_sum
		set_score = 0
		for (node_depth, node_sum, node_flow) in states:
			node_flow = add(mul(node_depth - depth[node], node_sum), node_flow)
			diff = mul(sub(total_flow, node_flow), node_sum)
			set_score = add(set_score, diff)
		score[set_no] = add(score[set_no], set_score)
		largest[set_no] = (depth[node], total_node_sum, total_flow)
	state[node] = largest
print(*(score[i] for i in range(q)), sep='\n')


from collections import Counter, defaultdict
MOD = 10 ** 9 + 7

def read_row():
	return (int(x) for x in input().split())

def mul(x, y):
	return x * y % MOD

def add(*args):
	return sum(args) % MOD

def sub(x, y):
	return (x - y) % MOD
(n, q) = read_row()
adj_list = defaultdict(list)
for _ in range(n - 1):
	(u, v) = read_row()
	adj_list[u].append(v)
	adj_list[v].append(u)
elements = {v: set() for v in adj_list}
for set_no in range(q):
	read_row()
	for x in read_row():
		elements[x].add(set_no)
root = next(iter(adj_list))
current = [root]
current_depth = 0
order = []
parent = {root: None}
depth = {root: current_depth}
while current:
	current_depth += 1
	order.extend(current)
	nxt = []
	for node in current:
		for neighbor in adj_list[node]:
			if neighbor not in parent:
				parent[neighbor] = node
				depth[neighbor] = current_depth
				nxt.append(neighbor)
	current = nxt
score = Counter()
state = {}
for node in reversed(order):
	states = [state[neighbor] for neighbor in adj_list[node] if neighbor != parent[node]]
	largest = {s: [depth[node], node, 0] for s in elements[node]}
	if states:
		max_index = max(range(len(states)), key=lambda x: len(states[x]))
		if len(states[max_index]) > len(largest):
			(states[max_index], largest) = (largest, states[max_index])
	sets = defaultdict(list)
	for cur_state in states:
		for (set_no, v) in cur_state.items():
			sets[set_no].append(v)
	for (set_no, states) in sets.items():
		if len(states) == 1 and set_no not in largest:
			largest[set_no] = states[0]
			continue
		if set_no in largest:
			states.append(largest.pop(set_no))
		total_flow = 0
		total_node_sum = 0
		for (node_depth, node_sum, node_flow) in states:
			flow_delta = mul(node_depth - depth[node], node_sum)
			total_flow = add(total_flow, flow_delta, node_flow)
			total_node_sum += node_sum
		set_score = 0
		for (node_depth, node_sum, node_flow) in states:
			node_flow = add(mul(node_depth - depth[node], node_sum), node_flow)
			diff = mul(sub(total_flow, node_flow), node_sum)
			set_score = add(set_score, diff)
		score[set_no] = add(score[set_no], set_score)
		largest[set_no] = (depth[node], total_node_sum, total_flow)
	state[node] = largest
print(*(score[i] for i in range(q)), sep='\n')