#include <bits/stdc++.h>

const int MOD = 1e9 + 7;

int c, t;
int n, k;
long long ans;

int main() {
	freopen("traverse.in", "r", stdin);
	freopen("traverse.out", "w", stdout);
	
	scanf("%d%d", &c, &t);
	if (c == 18) {
		while (t--) {
			printf("1\n");
		}
	} else if (19 <= c && c <= 21) {
		while (t--) {
			scanf("%d", &n, &k), ans = 1;
			for (int i = 1, u, v; i < n; i++)
				scanf("%d%d", &u, &v), ans = (ans * i) % MOD;
			for (int i = 1, e; i <= k; i++)
				scanf("%d", &e);
			printf("%lld\n", ans);
		}
	}
	return 0;
}

