#include<cstdio>

namespace IO{
	constexpr int SIZE=1<<19;
	char iBuf[SIZE],*iS,*iT,ch;bool f;
	char oBuf[SIZE],*oS=oBuf,*oT=oBuf+SIZE;
	inline char gc(){return(iS==iT&&(iT=(iS=iBuf)+fread(iBuf,1,SIZE,stdin),iS==iT)?EOF:*iS++);}
	template<typename I>inline void read(I &x){
		for(f=0,ch=gc();ch<'0'||ch>'9';ch=gc())if(ch=='-')f=1;
		for(x=0;ch>='0'&&ch<='9';ch=gc())x=(x<<3)+(x<<1)+(ch&15);
		if(f)x=-x;return;
	}
	inline void flush(){fwrite(oBuf,1,oS-oBuf,stdout);oS=oBuf;return;}
	inline void putc(char x){*oS++=x;if(oS==oT)flush();return;}
	template<typename I>inline void write(I x){
		static char qu[55];char*tmp=qu;
		if(x<0)putc('-'),x=-x;
		do*tmp++=(x%10)^48;while(x/=10);
		while(tmp--!=qu)putc(*tmp);
		return;
	}
	struct flusher{~flusher(){flush();}}_;
}
using IO::read;using IO::write;using IO::gc;using IO::putc;

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

int op,T;
int n,K;
int st[N*2],ne[N*2],la[N],em=1;
void insert(int x,int y){
	em++;st[em]=y;
	ne[em]=la[x];la[x]=em;
	return;
}
int id[N];

int Ans,cnt;

int dp[N];bool bz[N];
bool dfs(int x,int fa){
	dp[x]=1;
	if(bz[x])return 1;
	int cnt=0;bool f=0;
	for(int i=la[x];i!=0;i=ne[i]){
		if(st[i]==fa)continue;
		cnt++;
		if(dfs(st[i],x))cnt--,f=1;
		else dp[x]=1ll*dp[x]*cnt%mod;
		dp[x]=1ll*dp[x]*dp[st[i]]%mod;
	}
	return f;
}
bool pd;
long long calc(int x){
	pd=dfs(st[x*2],st[x*2+1]);
	pd=dfs(st[x*2+1],st[x*2]);
	return 1ll*dp[st[x*2]]*dp[st[x*2+1]]%mod;
}

long long quc(int x,int y){
	int ls1=0,ls2=0;
	bool f=0;
	bz[st[x*2]]=1;bz[st[x*2+1]]=1;
	pd=dfs(st[y*2],st[y*2+1]);
	pd=dfs(st[y*2+1],st[y*2]);
	ls1=dp[st[y*2]];ls2=dp[st[y*2+1]];
	if(ls1==1||ls2==1)f=1;
	bz[st[x*2]]=0;bz[st[x*2+1]]=0;
	bz[st[y*2]]=1;bz[st[y*2+1]]=1;
	pd=dfs(st[x*2],st[x*2+1]);
	pd=dfs(st[x*2+1],st[x*2]);
	if(dp[st[x*2]]==1||dp[st[x*2+1]]==1)f=1;
	if(dp[st[x*2]]!=ls1&&dp[st[x*2+1]]!=ls2)ls1=1ll*ls1*dp[st[x*2]]%mod;
	else ls1=1ll*ls1*dp[st[x*2+1]]%mod;
	if(!f)return 0;
	return 1ll*ls1*ls2%mod;
}

int main(){
	
	freopen("traverse.in","r",stdin);
	freopen("traverse.out","w",stdout);
	
	read(op);read(T);
	while(T--){
		
		while(em>1)st[em]=0,ne[em]=0,em--;
		for(int i=1;i<=n;i++)la[i]=0;
		
		read(n);read(K);
		for(int i=1;i<n;i++){
			int x,y;read(x);read(y);
			insert(x,y);insert(y,x);
		}
		for(int i=1;i<=K;i++){
			read(id[i]);
		}
		
		Ans=0;
		
		if(op<=6)Ans=calc(id[1]);
		else if(op==18)Ans=1;
		else if(op>=19&&op<=21){
			Ans=K;
			for(int i=1;i<=n-2;i++)Ans=1ll*Ans*i%mod;
			cnt=1ll*K*(K-1)%mod*((mod+1)/2)%mod;
			for(int i=1;i<=n-3;i++)cnt=1ll*cnt*i%mod;
			Ans=(Ans-cnt)%mod;
		}
		else if(op>=7&&op<=12){
			for(int i=1;i<=K;i++)Ans=(Ans+calc(id[i]))%mod;
			for(int i=1;i<=K;i++)
			for(int j=i+1;j<=K;j++){
				Ans=(Ans-quc(id[i],id[j]))%mod;
			}
		}
		
		Ans=(Ans%mod+mod)%mod;
		write(Ans);putc('\n');
		
	}
	
	return 0;
}
