#include<bits/stdc++.h>
#define ls p<<1
#define rs p<<1|1
using namespace std;

const int N=1e5+5,mod=1e9+7,M=500+5;

int kase,cid,n,m,cnt,ans,inv,d[N],jc[N],head[N];
bool b[M][M],c[N];

struct giao{
	int x,y;
}a[N];

struct edge{
	int to,id,nxt;
}e[N<<1];

void add(int u,int v,int id){
	e[++cnt]=(edge){v,id,head[u]};
	head[u]=cnt;
}

void dfs(int u,int fa){
//	cout<<u<<' '<<fa<<endl;
	b[u][fa]=true;
	for(int i=head[u];i;i=e[i].nxt){
		int v=e[i].to;
		if(e[i].id==fa) continue;
		dfs(v,e[i].id);
	}
}

int qpow(int x,int y){
	int res=1;
	for(;y;y>>=1,x=1ll*x*x%mod)
		if(y&1) res=1ll*res*x%mod;
	return res; 
}

void init(){
	memset(head,0,sizeof(head));
	memset(b,0,sizeof(b));
	memset(d,0,sizeof(d));
	cnt=0;
}

void work1(){
	int x;
	scanf("%d",&x);
	ans=1;
	for(int i=1;i<=n;i++)
		ans=1ll*ans*jc[d[i]-1]%mod;
	printf("%d\n",ans);
}

int main(){
	freopen("traverse.in","r",stdin);
	freopen("traverse.out","w",stdout);
	scanf("%d%d",&cid,&kase);
	while(kase--){
		init();
		scanf("%d%d",&n,&m);
		jc[0]=1;
		for(int i=1;i<=n;i++)
			jc[i]=1ll*jc[i-1]*i%mod;
		for(int i=1,x,y;i<n;i++){
			scanf("%d%d",&x,&y);
			add(x,y,i),add(y,x,i);
			a[i]=(giao){x,y};
			d[x]++;d[y]++;
		}
		if(m==1){
			work1();
			continue;
		}
		if(cid==18){
			for(int i=1,x;i<=m;i++)
				scanf("%d",&x);
			printf("1\n");
			continue;
		}
		if(cid>=19&&cid<=21){
			for(int i=1,x;i<=m;i++)
				scanf("%d",&x);
			if(n==2) printf("%d\n",1);
			else printf("%lld\n",(1ll*m*jc[n-2]%mod-1ll*m*(m-1)/2*jc[n-3]%mod+mod)%mod);
			continue;
		}
		for(int i=1,x;i<=m;i++){
			scanf("%d",&x);
			dfs(a[x].x,x);
			dfs(a[x].y,x);
			c[a[x].x]=c[a[x].y]=true;
		}
		ans=1;
		for(int i=1,x;i<=n;i++){
			if(d[i]==1) continue;
			x=0;int res=1;
			for(int j=1;j<n;j++)
				if(b[i][j]) x++;
			res=1ll*x*jc[d[i]-1]%mod;
			res=(res-1ll*x*(x-1)/2%mod*jc[d[i]-2]%mod);
			ans=1ll*ans*res%mod;
		}
		printf("%d\n",ans);
	}
	return 0;
}
