#include<bits/stdc++.h>
#define p 1000000007
using namespace std;
typedef long long ll;
ll c,ts,n,t,x,a[100010],b[100010];
ll r[20][20],g[20];
vector<ll> e[100010];
void sol1(){
	printf("%lld\n",1);
	while(t--){
		scanf("%lld",&x);	
	}
}
void sol2(){
	ll ans=((n-3+n-t)*(t)/2)%p;//n-2 + n-3 + ... + n-1-t
	for(ll i=1;i<=n-3;i++){
		ans=ans*i%p;
	}
	printf("%lld\n",ans);
	while(t--){
		scanf("%lld",&x);
	}
}
int main(){
	freopen("traverse.in","r",stdin);
	freopen("traverse.out","w",stdout);
	scanf("%lld%lld",&c,&ts);
	while(ts--){
		n=t=x=0;
		memset(a,0,sizeof(a));
		memset(b,0,sizeof(b));
		memset(r,0,sizeof(r));
		memset(g,0,sizeof(g));
		scanf("%lld%lld",&n,&t);
		for(ll i=1;i<n;i++){
			scanf("%lld%lld",&a[i],&b[i]);
			e[a[i]].push_back(b[i]);
			e[b[i]].push_back(a[i]);
		}
		if(c==18){
			sol1();
		}
		else if(c>=4&&c!=18){
			sol2();
		}
		else if(c<=3){
			ll m=n-1;
			for(ll i=1;i<=m;i++){
				for(ll j=1;j<=m;j++){
					if(i!=j){
						if(a[i]==a[j]||a[i]==b[j]||b[i]==a[j]||b[i]==b[j]){
							r[i][j]=1;
						}
					}
				}
			}
			for(ll i=1;i<=m;i++){
				g[i]=i;
			}
			scanf("%lld",&x);
			ll ans=0;
			do{
				bool ok=1;
				if(g[1]==x){
					for(ll i=2;i<=m;i++){
						if(r[g[i-1]][g[i]]==0){
							ok=0;
							break;
						}
					}
				}
				else ok=0;
				if(ok) ans++;
			}while(next_permutation(g+1,g+m+1));
			printf("%lld\n",ans);
		}
		for(int i=0;i<=n+3;i++){
			e[i].clear();
		}
	}
}
