#include <bits/stdc++.h>
#define p 1000000007
using namespace std;
int n,t,c,m,head[100005],cnt,sum[100005],u,v,b[100005];
long long summ;
char ch;
inline void read(int &x){
	x=0;
	ch=getchar();
	while(ch<48||ch>57) ch=getchar();
	while(ch>=48&&ch<=57){
		x=(x<<3)+(x<<1)+(ch^48);
		ch=getchar();
	}
}
int main(){
	freopen("traverse.in","r",stdin);
	freopen("traverse.out","w",stdout);
	read(c);read(t);
	while(t--){
		read(n);read(m);
		cnt=0;
		summ=1;
		for(int i=1;i<n;i++) read(u),read(v);
		for(int i=1;i<=m;i++) read(b[i]);
		if(c==18) puts("1");
		else if(c==19){
			for(int i=1;i<n;i++) summ=summ*i%p;
			printf("%lld\n",m*summ%p);
		}
		else if(c==1||c==4){
			for(int i=1;i<=n;i++) summ=summ*max(1,sum[i]-1)%p;
			printf("%lld\n",summ);
		}
		else if(c==7){
			for(int i=1;i<=n;i++) summ=summ*max(1,sum[i]-1)%p;
			printf("%lld\n",(summ*2-1)%p);
		}
	}
	return 0;
}
