#include<bits/stdc++.h>
using namespace std;
const long long inf = 1e18;
const int mininf = 1e9 + 13;
#define pb emplace_back
#define int long long
inline int read(){int x=0,f=1;char ch=getchar();while(ch<'0' or ch>'9'){if(ch=='-'){f=-1;}ch=getchar();}while(ch>='0' and ch<='9'){x=(x<<3)+(x<<1)+(ch^48);ch=getchar();} return x * f;}
inline void write(int x){if(x<0){putchar('-');x=-x;}if(x>=10){write(x/10);}putchar(x%10+'0');}
#define put() putchar(' ')
#define endl puts("")
const int MAX = 5e5 + 10;
const int mod = 1e9 + 7;

inline int quickPower(int a, int b, int p){
	int ans = 1, base = a;
	while(b){
		if(b & 1)	ans = ans * base % p;
		base = base * base % p;
		b >>= 1;
	}
	return ans;
}

int pre[MAX];
namespace subtask1{
	int n, k;

	vector <int> g[MAX];
	int rt[MAX];
	int ans;
	int siz[MAX];
	
	void dfs(int u, int fa){
		int cnt = 0;
		for(int v : g[u]){
			if(v == fa)	continue;
			dfs(v, u);
			siz[u] += siz[v];
			cnt++;
		}
		if(u <= n)	ans = ans * pre[cnt] % mod;
	}
	
	struct edge{
		int u, v;
	}; edge a[MAX];
	
	void solve(){
		n = read(), k = read();
		for(int i = 1; i < n; i++){
			pre[i] = pre[i - 1] * i % mod;
			int u = read(), v = read();
			g[u].pb(n + i);
			g[n + i].pb(u);
			g[n + i].pb(v);
			g[v].pb(n + i);
		}
		int ret = 0;
		for(int i = 1; i <= k; i++){
			int id = read();
			ans = 1;
			dfs(n + id, n + id);
			ret = (ret + ans) % mod;
		}
		// for(int i = 1; i < n; i++)	ans = ans * i % mod;
		write(ret), endl;
		for(int i = 1; i <= 2 * n; i++)	g[i].clear();
	}
}


namespace subtask2{
	int n, k;

	vector <int> g[MAX];
	vector <int> g2[MAX];
	int fa[MAX], siz[MAX];
	int find(int x){
		if(fa[x] == x)	return x;
		return fa[x] = find(fa[x]);
	}
	
	void merge(int x, int y){
		if(find(x) == find(y))	return ;
		siz[find(y)] += siz[find(x)];
		siz[find(x)] = 0;
		fa[find(x)] = find(y);
	}
	
	int rt[MAX];
	int ans;
	
	void dfs(int u, int fa){
		int cnt = 0;
		int pree = -1;
		for(int v : g2[u]){
			if(v == fa)	continue;
			dfs(v, u);
			if(u <= n){
				if(pree == -1){
					pree = v;
				}else{
					merge(pree, v);
				}
			}
			cnt++;
		}
		if(u <= n)	ans = ans * pre[cnt] % mod;
	}
	// 197157871 - 120921679
	struct edge{
		int u, v;
	}; edge a[MAX];
	
	map <pair <int,int>, bool> mp;
	
	void solve(){
		n = read(), k = read();
		for(int i = 1; i <= 2 * n; i++)	fa[i] = i, siz[i] = 1;
		for(int i = 1; i < n; i++){
			pre[i] = pre[i - 1] * i % mod;
			int u = read(), v = read();
			g[u].pb(n + i);
			g[n + i].pb(u);
			g[n + i].pb(v);
			g[v].pb(n + i);
		}
		// 需要注意 vector 效率问题！
		int ret = 0;
		for(int i = 1; i <= k; i++){
			mp.clear();
			for(int j = 1; j <= 2 * n - 1; j++)	g2[j].clear();	
			for(int j = n + 1; j <= 2 * n - 1; j++){
				for(int v : g[j]){
					pair <int, int> E = make_pair(find(j), v);
					if(!mp[E]){
						mp[E] = 1;
						g2[find(j)].pb(v);
						g2[v].pb(find(j));
					}
				}
			}
			ans = 1;
			int id = read();
			// write(siz[find(n + id)]), endl;
			if(i == 1 or siz[find(n + id)] > 1){
				// puts("hi");
				// write(find(n + id)), put(), write(n + id), endl;
				dfs(find(n + id), find(n + id));
				// write(ans), endl;
				ret = (ret + ans) % mod;
			}
		}
		// ans = 1;
		// // for(int i = 1; i < n; i++)	ans = ans * i % mod;
		// dfs(n + id, n + id);
		// dfs(n + id2, n + id2);
		write(ret), endl;
		
		for(int i = 1; i <= 2 * n; i++)	g[i].clear();
		for(int i = 1; i <= 2 * n; i++)	g2[i].clear();
	}
}


signed main(){
	freopen("traverse.in", "r", stdin);
	freopen("traverse.out", "w", stdout);
	int id = read();
	if(id <= 6){
		pre[0] = 1;
		for(int i = 1; i < MAX; i++)	pre[i] = pre[i - 1] * i % mod;
		int t = read();
		while(t--) subtask1::solve();
		return 0;
	}else if(id <= 10){
		pre[0] = 1;
		for(int i = 1; i < MAX; i++)	pre[i] = pre[i - 1] * i % mod;
		int t = read();
		while(t--) subtask2::solve();
		return 0;
	}else{
		pre[0] = 1;
		for(int i = 1; i < MAX; i++)	pre[i] = pre[i - 1] * i % mod;
		int t = read();
		while(t--) subtask1::solve();
		return 0;
	}
	// int t = read();
	// while(t--)	solve();
	return 0;
}