#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#define int long long
#define x first
#define y second
using namespace std;
typedef pair<int, int> PII;
const int N = 2e5 + 10, mod = 1e9 + 7;
bool Mst;
int n, c, k, fac[N];
vector<int> g[N];
void work() {
	cin >> n >> k;
	for(int i = 1, a, b; i < n; i ++)
		cin >> a >> b;
	for(int i = 1, x; i <= k; i ++) cin >> x;
	if(n == 1 || c == 18) cout << 1 << '\n'; 
	else if(c >= 19 && c <= 21) {
		cout << (fac[n - 1] * k % mod - k * (k - 1) % mod * fac[n - 2] % mod + mod) % mod << '\n';
		return ; 
	}
	else {
		cout << (fac[n - 1] * k % mod - k * (k - 1) % mod * fac[n - 2] % mod + mod) % mod << '\n';
	}
}
bool Med;
signed main() {
	freopen("traverse.in", "r", stdin);
	freopen("traverse.out", "w", stdout);
	ios::sync_with_stdio(0), cin.tie(0);
	cerr << (abs(&Mst - &Med) / 1048576.0) << "Mb\n";
	fac[0] = 1;
	for(int i = 1; i < N; i ++) fac[i] = 1ll * fac[i - 1] * i % mod;
	int T = 1; cin >> c >> T;
	while(T --) work();

	return 0;
}


