#include<bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
long long mul[200005], T, n, c, k, a[200005];
struct Edge{int u, v;} mem[200005];
vector<int> e[200005];
namespace SubTask1{
	long long ans, cnt = 0;
	void dfs(int x, int pr){ int cur = e[x].size();
		ans = ans * mul[cur - 1] % mod;
		for(int i = 0; i < e[x].size(); i++)
			if(e[x][i] != pr) dfs(e[x][i], x);
	}
	void solve(){
		cin >> n >> k, ans = 1, cnt++;
		for(int i = 1, u, v; i < n; i++) 
			cin >> u >> v, e[u].push_back(v), e[v].push_back(u), mem[i].u = u, mem[i].v = v;
		for(int i = 1; i <= k; i++) cin >> a[i];
		dfs(mem[a[1]].u, mem[a[1]].v), dfs(mem[a[1]].v, mem[a[1]].u);
		for(int i = 1; i <= n; i++) e[i].clear();
		cout << ans << endl; return ;
	}
}
namespace SubTask2{
	void solve(){
		cin >> n >> k;
		for(int i = 1, u, v; i < n; i++)
			cin >> u >> v, e[u].push_back(v), e[v].push_back(u), mem[i].u = u, mem[i].v = v;
		for(int i = 1; i <= k; i++) cin >> a[i];
		cout << 1 << endl; return ;
	}
}
namespace SubTask3{
	void solve(){
		cin >> n >> k;
		for(int i = 1, u, v; i < n; i++)
			cin >> u >> v, e[u].push_back(v), e[v].push_back(u), mem[i].u = u, mem[i].v = v;
		for(int i = 1; i <= k; i++) cin >> a[i];
		if(n == 2){cout << 1 << endl;return ;}
		long long ans = mul[n - 2] * k % mod, x = (1ll * k * (k - 1) / 2) % mod;
		cout << (ans + mod - x * mul[n - 3] % mod) % mod << endl; return ;
	}
}
namespace SubTask4{
	long long ans, cnt = 0;
	pair<int, int> tmp;
	void dfs(int x, int pr){ int cur = e[x].size();
		ans = ans * mul[cur - 1] % mod;
		for(int i = 0; i < e[x].size(); i++){
			if(e[x][i] != pr) dfs(e[x][i], x);
			if(e[x][i] == pr) continue;
			if(make_pair(min(x, e[x][i]), max(x, e[x][i])) == tmp) cnt = e[x].size() - 1;
		}
	}
	void solve(){
		cin >> n >> k, ans = 1, cnt = 0;
		for(int i = 1, u, v; i < n; i++) 
			cin >> u >> v, e[u].push_back(v), e[v].push_back(u), mem[i].u = u, mem[i].v = v;
		for(int i = 1; i <= k; i++) cin >> a[i]; 
		if(k == 2) tmp.first = min(mem[a[2]].u, mem[a[2]].v), tmp.second = max(mem[a[2]].u, mem[a[2]].v);
		dfs(mem[a[1]].u, mem[a[1]].v), dfs(mem[a[1]].v, mem[a[1]].u);
		long long v = ans / cnt; ans = 1; if(k == 2) dfs(mem[a[2]].u, mem[a[2]].v), dfs(mem[a[2]].v, mem[a[2]].u);
		for(int i = 1; i <= n; i++) e[i].clear();
		if(k == 1){cout << v * cnt << endl; return ;}
		cout << ans + v * (cnt - 1) << endl; return ;
	}
}
int main(){
	freopen("traverse.in", "r", stdin);
	freopen("traverse.out", "w", stdout);
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	cin >> c >> T, mul[0] = 1;
	for(int i = 1; i <= 200000; i++) mul[i] = mul[i - 1] * i % mod;
	if(c <= 6){while(T--) SubTask1::solve(); return 0;}
	if(c == 18){while(T--) SubTask2::solve(); return 0;}
	if(c >= 19 && c <= 21){while(T--) SubTask3::solve(); return 0;}
	while(T--) SubTask4::solve();
	return 0;
}
