#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxn = 1e5 + 50, mod = 1e9 + 7;
int T, c, n, k, head[maxn], cnt, u[maxn], v[maxn], b[maxn];
//vector<int> v[maxn];

void solveli(){
	cout << "1\n";
}

void solvejh(){
	int ans = 1;
	for(int i = n; i >= 1; i--) ans = ans * i % mod;
	cout << ans << '\n';
}

void solve(){
	cout << 0 << '\n';
}

signed main() {
//	freopen("traverse9.in","r",stdin);
	freopen("traverse.in","r",stdin);
	freopen("traverse.out","w",stdout);
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	cin >> c >> T;
	while(T--) {
		cin >> n >> k;
		for(int i = 1; i < n; i++){
			cin >> u[i] >> v[i];
		}
		for(int i = 1; i <= k; i++) cin >> b[i];
		if(c == 18) solveli();
		else if(c >= 19 && c <= 21) solvejh();
		else solve();
	}
	return 0;
}
