#include<bits/stdc++.h>
using namespace std;

#define F(i, l, r) for(int i = (l); i <= (r); ++i)
#define dF(i, r, l) for(itn i = (r); i >= (l); --i)

inline int ri() {
	int x = 0, f = 1;
	char c = getchar();
	while(c < '0' || c > '9') {
		if(c == '-') f = -1;
		c = getchar();
	}
	while(c >= '0' && c <= '9') {
		x = x * 10 + c - 48;
		c = getchar();
	} return x * f;
}

const int _ = 2e5 + 5;
const int mod = 1e9 + 7;
#define vi vector<int>
#define pb push_back
#define pii pair<int, int>
#define fi first
#define se second
#define debug(...) fprintf(stderr, __VA_ARGS__)
int c, n, k, fac[_], U[_], V[_];

vector<pii> d[_];

bool fg[_];

namespace part1 {
	void solve() {
		int T = ri();
		while(T--) {
			n = ri(), k = ri();
			F(i, 1, n) d[i].clear();
			F(i, 1, n - 1) {
				int u = ri(), v = ri();
				d[u].pb({v, i}), d[v].pb({u, i});
				U[i] = u, V[i] = v;
			}
			F(i, 1, n - 1) fg[i] = 0;
			F(i, 1, k) fg[ri()] = 1;
	//		F(i, 1, n - 1) if(fg[i]) {
	//			debug("[u] = %d, [v] = %d\n", U[i], V[i]);
	//		}
			int ans = 1;
			F(i, 1, n) ans = 1ll * ans * fac[(int)d[i].size() - 1] % mod;
			cout << ans << '\n';
		}
	}
}

int qpow(int x, int y) {
	int res =1 ;
	while(y) {
		if(y & 1) res = 1ll * res * x % mod;
		x = 1ll * x * x % mod, y >>= 1;
	} return res;
}

signed main() {
//	freopen("traverse4.in", "r", stdin);
	freopen("traverse.in", "r", stdin);
	freopen("traverse.out", "w", stdout);
	fac[0] = 1;
	F(i, 1, 1e5) fac[i] = 1ll * fac[i - 1] * i % mod;
	c = ri();
	if(c <= 6) {
		part1::solve();
		return 0;
	}
	if(c == 18) {
		part1::solve();
		return 0;
	}
	if(c >= 19 && c <= 21) {
		int T = ri();
		while(T--) {
			n = ri(), k = ri();
			F(i, 1, n) d[i].clear();
			F(i, 1, n - 1) {
				int u = ri(), v = ri();
				d[u].pb({v, i}), d[v].pb({u, i});
				U[i] = u, V[i] = v;
			}
			F(i, 1, n - 1) fg[i] = 0;
			F(i, 1, k) fg[ri()] = 1;
//			F(i, 1, n - 1) if(fg[i]) {
//				debug("[u] = %d, [v] = %d\n", U[i], V[i]);
//			}
			int ans = 1;
			F(i, 1, 1) {
				int z = 0;
				for(pii v : d[i]) if(fg[v.se]) {
					z++;
				}
				int z2 = (int)d[i].size() - z;
				debug("z = %d, [z2] = %d\n", z, z2);
				ans = 1ll * z * z2 % mod * fac[(int)d[i].size() - 2] % mod;
				ans = (ans + 1ll * z * (z - 1) / 2 % mod * fac[(int)d[i].size() - 2] % mod) % mod;
			}
			cout << ans << '\n';
		}
		return 0;
	}
	if(c <= 10) {
		
	}
//	int T = ri();
//	while(T--) {
//		n = ri(), k = ri();
//		F(i, 1, n) d[i].clear();
//		F(i, 1, n - 1) {
//			int u = ri(), v = ri();
//			d[u].pb({v, i}), d[v].pb({u, i});
//			U[i] = u, V[i] = v;
//		}
//		F(i, 1, n - 1) fg[i] = 0;
//		static int bel[_];
//		F(i, 1, k) fg[ri()] = 1;
//		vi A;
//		F(i, 1, n - 1) if(fg[i]) {
//			A.pb(i);
//			bel[U[i]] = bel[V[i]] = i;
//			debug("[u] = %d, [v] = %d\n", U[i], V[i]);
//		}
//		int res = 1;
//		F(i, 1, n) res = 1ll * res * fac[(int)d[i].size() - 1] % mod;
//		int ans = 2ll * res % mod;
//		bool flg = 0;
//		static int dep[_];
//		function<void(int, int)> dfs = [&](int u, int fa) -> void {
//			dep[u] = dep[fa] + 1;
//			for(pii v : d[u]) if(v.fi != fa) {
//				dfs(v.fi, u);
//			}
//		};
//		dep[0] = 0;
//		dfs(U[A[1]], 0);
//		if(dep[U[A[0]]] > dep[V[A[0]]]) swap(U[A[0]], V[A[0]]);
//		int w = 0;
//		w = 1ll * res * qpow(d[U[A[0]]].size() - 1, mod - 2) % mod;
//		dep[0] = 0;
//		dfs(U[A[0]], 0);
//		if(dep[U[A[1]]] > dep[V[A[1]]]) swap(U[A[1]], V[A[1]]);
//		w = 1ll * w * qpow(d[U[A[1]]].size() - 1, mod - 2) % mod;
//		ans = (ans + mod - w) % mod;
//		debug("[w] =%d\n", w);
//		cout << ans << '\n';
//	}
}

