#include<bits/stdc++.h>
using namespace std;
const int N=2e5;
const int mod=1e9+7;
int n,k;

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