// 哇沃！这种题暴力连边求生成树的暴力分肯定很多呢，先打了再说喵。 

#include <bits/stdc++.h>
#define il inline
using namespace std;
using LL = long long;
const int N = 1e5 + 3;
const int MOD = 1e9 + 7;
int n, k;
basic_string<int> G[N];
int eu[N], ev[N];
int hvy_e[N];
il void solve18() { // c == 18
	cin >> n >> k;
	for (int i = 1; i <= n; i++) G[i].clear();
	for (int i = 1, u, v; i <= n - 1; i++) cin >> u >> v, eu[i] = u, ev[i] = v;
	for (int i = 1; i <= k; i++) cin >> hvy_e[i];
	cout << 1 << '\n';
}
il void solve19_21() {
	cin >> n >> k;
	for (int i = 1; i <= n; i++) G[i].clear();
	for (int i = 1, u, v; i <= n - 1; i++) cin >> u >> v, eu[i] = u, ev[i] = v;
	for (int i = 1; i <= k; i++) cin >> hvy_e[i];
	LL s1 = 1, s2 = 1;
	for (int i = 1; i <= n - 2; i++) s1 = s1 * i % MOD;
	for (int i = 1; i <= n - 3; i++) s2 = s2 * i % MOD;
	cout << (1ll * k * s1 % MOD - 1ll * k * (k - 1) / 2 % MOD * s2 % MOD + MOD) % MOD << '\n';
}
set<set<pair<int, int>>> S;
bool vis[N]; int fa[N];
void dfs(int u, set<pair<int, int>> T) {
//		cerr << "dfs : " << endl;
//		for (pair<int, int> x : T) cerr << x.first << ' ' << x.second << endl;
//		cerr << endl << endl;
	if (T.size() == n - 2) {
		S.insert(T);
		return;
	}
	int cnt = 0;
	for (int v : G[u]) if (!vis[v]) {
		vis[v] = 1, fa[v] = u;
		pair<int, int> p = {u, v};
		if (u > v) p = {v, u};
		T.insert(p);
		dfs(v, T);
		T.erase(p);
		vis[v] = 0, fa[v] = 0;
		++cnt;
	}
	if (!cnt) dfs(fa[u], T);
}
il void solve1_3() {
	cin >> n >> k;
	for (int i = 1; i <= n; i++) G[i].clear();
	for (int i = 1, u, v; i <= n - 1; i++) cin >> u >> v, eu[i] = u, ev[i] = v;
	for (int i = 1; i <= k; i++) cin >> hvy_e[i];
	
	for (int i = 1; i <= n - 1; i++) for (int j = i + 1; j <= n - 1; j++) {
		if (eu[i] == eu[j] || eu[i] == ev[j] || ev[i] == eu[j] || ev[i] == ev[j]) G[i].push_back(j), G[j].push_back(i);
	}
	S.clear(); for (int i = 1; i <= n - 1; i++) vis[i] = 0;
	for (int i = 1; i <= k; i++) vis[hvy_e[i]] = 1, dfs(hvy_e[i], set<pair<int, int>>{}), vis[hvy_e[i]] = 0;
	cout << S.size() << '\n';
}
il void solve4_6() {
	cin >> n >> k;
	for (int i = 1; i <= n; i++) G[i].clear();
	for (int i = 1, u, v; i <= n - 1; i++) cin >> u >> v, eu[i] = u, ev[i] = v;
	for (int i = 1; i <= k; i++) cin >> hvy_e[i];
}
signed main() {
	freopen("traverse.in", "r", stdin);
	freopen("traverse.out", "w", stdout);
	ios::sync_with_stdio(false), cin.tie(nullptr);
	int c, T; cin >> c >> T;
	if (c == 18) {
		while (T--) solve18();
	}
	else if (c >= 19 && c <= 21) {
		while (T--) solve19_21();
	}
	else if (c >= 1 && c <= 3 || 1) solve1_3();
	return 0;
}
