#include<bits/stdc++.h>
#define rep(rp,a,b) for(int rp=a;rp<=b;++rp)
#define per(pr,a,b) for(int pr=a;pr>=b;--pr)
#define Rep(rp,a,b) for(int rp=a;rp<b;++rp)
#define fi first
#define se second
#define eb emplace_back
using namespace std;
const int N = 1e5+5, MOD = 1e9+7;
inline int read() {
	int x = 0, f = 1; char ch = 0;
	while (!isdigit(ch)) {ch = getchar(); if (ch == '-') f *= -1;}
	while (isdigit(ch)) x = x*10+(ch^48), ch = getchar();
	return f*x;
}

int fac[N];

void init() {
	fac[0] = 1; rep (i, 1, N-5) fac[i] = 1ll*fac[i-1]*i%MOD;
}

int c, deg[N], kk[N];
pair<int, int> e[N];

void solve() {
	int n = read(), k = read();
	memset(deg, 0, sizeof(deg));
	rep (i, 1, n-1) {
		int u = read(), v = read();
		++deg[u], ++deg[v];
		e[i] = {u, v};
	}
	rep (i, 1, k) kk[i] = read();
	if (c >= 1 && c <= 6) {
		int ans1 = 1;
		rep (i, 1, n) ans1 = 1ll*ans1*fac[deg[i]-1]%MOD;
		printf("%d\n", ans1);
		return ;
	}
	// if (c >= 7 && c <= 10) {
	// 	int ans7 = 1;
	// 	rep (i, 1, n) ans1 = 1ll*ans1*fac[deg[i]-1]%MOD;
	// 	ans7 = ans7*2%MOD;
	// 	printf("%d\n", ans1);
	// 	return ;
	// }
	if (c == 18) return puts("1"), void();
	if (c >= 19 && c <= 21) {
		int ans19 = (1ll*k*(k-1)/2%MOD+1ll*k*(n-1-k)%MOD)%MOD;
		ans19 = 1ll*ans19*fac[n-3]%MOD;
		printf("%d\n", ans19);
		return ;
	}
}

int main() {
	freopen("traverse.in", "r", stdin);
	freopen("traverse.out", "w", stdout);
	init();
	c = read();
	int T = read(); while (T --) solve();
	return 0;
}