#include<bits/stdc++.h>
using namespace std;
#define int long long
const int mod=1e9+7;
int read(){
	int x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9') {
		if(ch=='-') f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9'){
		x=x*10+ch-'0';
		ch=getchar();
	}
	return x*f;
}
struct node{
	int v,id;
	bool operator<(const node &a)const{
		return v<a.v; 
	}
};
int st[100010],ed[100010],vis[100010];
vector<node> g[100010];
int e[100010];
int jc[100010],n,k;
int ksm(int a,int b){
	int ans=1;
	for(;b>=1;b>>=1){
		if(b&1) ans=ans*a%mod;
		a=a*a%mod;
	}
	return ans;
}
int inv(int x){
	return ksm(x,mod-2);
}
int anc3;
map<int,int> mpc3;
vector<int> cur;
int zamazc3;
void dfs(int u,int cntt){
	//cout<<u<<" "<<cntt<<endl;
	vis[u]=1;
	if(cur.size()==n-2){
	//	cout<<u<<endl;
		sort(cur.begin(),cur.end());
		int tt=0;
		for(auto v:cur){
			tt=(tt*233819%mod+v*17)%mod;
		} 
		if(mpc3.find(tt)==mpc3.end()){
			mpc3[tt]=1;
			anc3++;
		}
		return;
	}
	for(auto v:g[u]){
		if(vis[v.v]) continue;
		cur.push_back(v.id);
		dfs(v.v,cntt+1);
	}
}
void ddfs(int u){
	if(u==n){
		for(int i=1;i<=k;i++){
			zamazc3=i;
			cur.clear();
			for(int j=1;j<n;j++) vis[j]=0;
			dfs(e[i],0);
		}
		return;
	}
	do{
		ddfs(u+1);
	}while(next_permutation(g[u].begin(),g[u].end()));
}
signed main(){
	freopen("traverse.in","r",stdin);
	freopen("traverse.out","w",stdout);
	jc[0]=1;
	for(int i=1;i<=100000;i++) jc[i]=jc[i-1]*i%mod;
	int Cases,Tests;
	Cases=read(),Tests=read();
	while(Tests--){
		n=read(),k=read();
		for(int i=1;i<n;i++){
			int u=read(),v=read();
		//	g[u].push_back(node{v,i-1});
		//	g[v].push_back(node{u,i-1});
			st[i]=u,ed[i]=v;
		}
		for(int i=1;i<=k;i++){
			e[i]=read();
		}
		if(Cases==18){
			printf("1\n");
			continue;
		}
		if(Cases>=19&&Cases<=21){
			if(n==2){
				printf("1\n");
				continue;
			}
			printf("%lld\n",((jc[n-1]-(n-k-1)*(n-k-2)%mod*jc[n-3]%mod)%mod+mod)%mod*inv(2)%mod);
			continue;
		}
		if(Cases<=17){
			mpc3.clear();
			int cnt=0;
			anc3=0;
			for(int i=1;i<n;i++){
				for(int j=i+1;j<n;j++){
					if(i^j){
						if(st[i]==st[j]||st[i]==ed[j]||st[j]==ed[i]||ed[j]==ed[i]){
							g[i].push_back(node{j,++cnt});
							g[j].push_back(node{i,cnt});
						}
					}
				}
			}
			ddfs(1);
			
			printf("%lld\n",anc3);
			for(int i=1;i<n;i++) g[i].clear(),vis[i]=0;
		}
	}
}
