#include<bits/stdc++.h>
using namespace std;
#define int long long
inline int read() {
	int x = 0, f = 1;
	char ch = getchar();
	while(ch < '0' || ch > '9') {
		if(ch == '-') f = -1;
		ch = getchar();
	}
	while(ch >= '0' && ch <= '9') {
		x = (x << 1) + (x << 3) + (ch ^ 48);
		ch = getchar();
	}
	return x * f;
}
#define pb push_back
#define fi first
#define se second
#define N 200010
//#define M
#define mo (int)(1e9 + 7)
int n, m, i, j, k, T, id;
int fac[N], U[N], V[N], ans, u, v, e[N]; 
int E[N], sum; 
vector<pair<int, int> >G[N]; 

namespace Sol1 {
	void dfs(int x, int fa) {
		int cnt = 0, y; 
		for(auto t : G[x]) if((y = t.fi) != fa) {
			dfs(y, x); ++cnt; 
		}
		ans *= fac[cnt]; ans %= mo; 
	}
	void main() {
		while(T--) {
			n = read(); k = read(); 
			for(i = fac[0] = 1; i <= n; ++i) fac[i] = fac[i - 1] * i % mo, G[i].clear(); 
			for(i = 1; i < n; ++i) {
				u = U[i] = read(); v = V[i] = read(); 
				G[u].pb({v, i}); G[v].pb({u, i}); 
			}
			for(i = 1; i <= k; ++i) e[i] = read(); ans = 1; 
			dfs(U[e[1]], V[e[1]]); dfs(V[e[1]], U[e[1]]); 
			printf("%lld\n", ans); 
		}
	}
}

namespace Sol2 {
	void dfs(int x, int fa) {
		int cnt = 0, flg = 0, y; 
		for(auto t : G[x]) if((y = t.fi) != fa) {
			dfs(y, x); ++flg; 
			if(!E[t.se]) ++cnt; 
		}
		if(flg && !cnt) sum = 0; 
		if(flg) sum *= (fac[flg - 1] * cnt % mo); sum %= mo; 
//		if(flg != cnt) printf("# %lld %lld | %lld\n", flg, cnt, sum); 
	}
	void main() {
		while(T--) {
			n = read(); k = read(); 
//			printf("> %lld %lld\n", n, k); 
			for(i = fac[0] = 1; i <= n; ++i) fac[i] = fac[i - 1] * i % mo, G[i].clear(), E[i] = 0; 
			for(i = 1; i < n; ++i) {
				u = U[i] = read(); v = V[i] = read(); 
				G[u].pb({v, i}); G[v].pb({u, i}); 
			}
			for(i = 1; i <= k; ++i) e[i] = read(); ans = 0; 
			if(n == 2) { printf("1\n"); continue; }
			for(i = k; i >= 1; --i) {
				sum = 1; 
				dfs(U[e[i]], V[e[i]]); dfs(V[e[i]], U[e[i]]); 
//				printf("%lld\n", sum); 
				E[e[i]] = 1; 
				ans += sum; ans %= mo; 
			}
			printf("%lld\n", ans); 
		}
	}
}

namespace Sol3 {
	void main() {
		while(T--) {
			n = read(); k = read(); 
//			printf("> %lld %lld\n", n, k); 
			for(i = fac[0] = 1; i <= n; ++i) fac[i] = fac[i - 1] * i % mo, G[i].clear(), E[i] = 0; 
			for(i = 1; i < n; ++i) {
				u = U[i] = read(); v = V[i] = read(); 
				G[u].pb({v, i}); G[v].pb({u, i}); 
			}
			for(i = 1; i <= k; ++i) e[i] = read(); ans = 0; 
			if(n == 2) { printf("1\n"); continue; }
			for(i = 1; i <= k; ++i) {
				sum = fac[n - 3] * (n - 2 - i + 1) % mo; 
				ans += sum; ans %= mo; 
			}
			printf("%lld\n", ans); 
		}
	}
}


namespace Sol4 {
	void main() {
		while(T--) {
		printf("1\n"); 
		}
	}
}

signed main() {
//	srand(time(0));
	freopen("traverse.in", "r", stdin);
	freopen("traverse.out", "w", stdout);
	id = read(); T = read(); 
//	if(id <= 6) return Sol1 :: main(), 0; 
//	if(id <= 10) 
	if(id == 18) return Sol4 :: main(), 0;  
	if(id >= 19 && id <= 21) return Sol3 :: main(), 0; 
	return Sol2 :: main(), 0; 
	return 0;
}

