#include<bits/stdc++.h>
using namespace std;
int T,c;
int n,k;
int u[100010],v[100010];
long long P=1e9+7;
vector<int>G[100010];
int e[100010];
long long a[510][510];
long long qpow(long long x,long long y){
	long long g=1;
	while(y){
		if(y&1)g=g*x%P;
		x=x*x%P;y>>=1;
	}return g;
}
long long d(){
	int m=n-2;
	long long ans=1;
	for(int i=1;i<=m;++i){
		if(a[i][i]==0){
			for(int j=i+1;j<=m;++j)
				if(a[j][i]!=0){
					for(int t=i;t<=m;++t)
						swap(a[j][t],a[i][t]);
					break;
				}
			if(a[i][i]==0)return 0;
			ans=P-ans;
		}
		ans=ans*a[i][i]%P; 
		for(int j=i+1;j<=m;++j){
			long long g=qpow(a[i][i],P-2)*a[j][i]%P;
			for(int t=i;t<=m;++t)
				a[j][t]=((a[j][t]-a[i][t]*g%P)%P+P)%P;
		}
	}return (ans%P+P)%P;
}
int main(){
	freopen("traverse.in","r",stdin);
	freopen("traverse.out","w",stdout);
	scanf("%d%d",&c,&T);
	while(T--){
		if(c==18){
			printf("1\n");
			continue;
		}
		scanf("%d%d",&n,&k);
		for(int i=1;i<n;++i){
			scanf("%d%d",&u[i],&v[i]);
		}
		for(int i=1;i<=k;++i)
			scanf("%d",&e[i]);
		for(int i=1;i<n;++i){
			G[u[i]].push_back(i);
			G[v[i]].push_back(i);
		}
		for(int i=1;i<=n;++i){
			int siz=((int)G[i].size())-1;
			for(int x=0;x<=siz;++x)
				for(int y=x+1;y<=siz;++y){
					a[G[i][x]][G[i][y]]=a[G[i][y]][G[i][x]]=1;
					a[G[i][x]][G[i][x]]=(a[G[i][x]][G[i][x]]-1+P)%P;
					a[G[i][y]][G[i][y]]=(a[G[i][y]][G[i][y]]-1+P)%P;
				} 
		}
		printf("%lld\n",d());
	}return 0;
}
