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

const int N = 1e5 + 10, mod = 1e9 + 7;
long long fac[N];

int main()
{
	freopen("traverse.in", "r", stdin);
	freopen("traverse.out", "w", stdout);
//	freopen("assign2.in", "r", stdin);
//	freopen("assign.out", "w", stdout);
//	ios::sync_with_stdio(0), cin.tie(0);
	fac[0] = 1;
	for (int i = 1; i < N; ++i) fac[i] = fac[i - 1] * i % mod;
	int t, c;
	cin >> c >> t;
	while (t--)
	{
		int n, k;
		cin >> n >> k;
//		cout << n << ' ' << k << '\n';
		for (int i = 1, u, v; i < n; ++i) cin >> u >> v;
		for (int i = 1, e; i <= k; ++i) cin >> e;
		if (c == 18 || n < 3) cout << "1\n";
		else cout << fac[n - 2] * k % mod << '\n';
	}
	return 0;
}
/*
1 1
4 1
1 2
2 3
2 4
1
*/
