#include <cstdio>
const int N = 100050, M = 1e9 + 7;
int C, T, n, m, i, j, k, ans;
void work2(){
	scanf("%d%d", &n, &m);
	for(i=1; i<n; i++) scanf("%*d%*d");
	for(i=1; i<=m; i++) scanf("%*d");
	for(ans=m, i=1; i<n; i++){
		ans = (1ll*ans*i) % M;
	}
	printf("%d\n", ans);
}
int main(){
//	freopen("traverse10.in", "r", stdin);
	scanf("%d%d", &C, &T);
	if(19 <= C && C <= 21){
		while(T--) work2();
	}
	return 0;
}
