#include<bits/stdc++.h>
using namespace std;
const int N=1e6+1,mod=1e9+7;
int v,T,n,k,x,y,pi[N];
int main(){
	freopen("traverse.in","r",stdin);
	freopen("traverse.out","w",stdout);
	cin>>v>>T;
	pi[1]=1;
	for(int i=2;i<N;++i)pi[i]=pi[i-1]*i%mod;
	if(v==18){//A
		while(T--){
			scanf("%d%d",&n,&k);
			for(int i=1;i<n;++i)scanf("%d%d",&x,&y);
			for(int i=1;i<=k;++i)scanf("%d",&x),printf("1\n");
		}
	}else if(19<=v&&v<=21){//B
		while(T--){
			scanf("%d%d",&n,&k);
			for(int i=1;i<n;++i)scanf("%d%d",&x,&y);
			for(int i=1;i<=k;++i)scanf("%d",&x),printf("%d\n",pi[n-1]);
		}
	}
	return 0;
}
/*
20 1
5 1
1 2
2 3
3 4
4 5
1
*/
