#include<bits/stdc++.h>
#define FIO(file) freopen(file".in","r",stdin),freopen(file".out","w",stdout)
using namespace std;
const int N=1e5+5;
const int mod=1e9+7;

int n,k;
int typ,T;
struct nd{int u,v;}a[N];
int p[N];

int d[N];
vector<int> e[N];

namespace sub1{
	bool vs[N];
	int st[N],pp[N],as;
	
	map<int,bool> mp;
	int id(int x,int y){return (x-1)*n+y;}
	
	void dfs(int x){
		if(x==n){
			int s=0;
			for(int i=2;i<n;++i){
				s|=(1<<id(st[i],pp[i]));
				s|=(1<<id(pp[i],st[i]));
			}
			if(!mp[s]) ++as,mp[s]=1;
			return;
		}
		int lst=0;
		for(int i=x-1;i;--i) if(d[st[i]]){lst=st[i];break;}
		for(int i:e[lst]){
			if(vs[i]) continue;
			vs[i]=1,st[x]=i,pp[x]=lst;
			--d[lst],--d[i];
			dfs(x+1);
			vs[i]=0,++d[lst],++d[i];
		}
	}
	
	void solve(){
		mp.clear(),as=0;
		for(int i=1;i<=n;++i) d[i]=0,e[i].clear();
		for(int i=1;i<n;++i){
			for(int j=i+1;j<n;++j){
				if(a[i].u==a[j].u || a[i].u==a[j].v || a[i].v==a[j].u || a[i].v==a[j].v){
					e[i].push_back(j),e[j].push_back(i);
					++d[i],++d[j];
				}
			}
		}
		for(int i=1;i<=k;++i){
			st[1]=p[i],vs[p[i]]=1;
			dfs(2);
			vs[p[i]]=0;
		}
		printf("%d\n",as);
	}
}
namespace sub2{
	int fac[N],as;
	void solve(){
		as=1,fac[0]=1;
		for(int i=1;i<=n;++i) fac[i]=1ll*fac[i-1]*i%mod;
		for(int i=1;i<=n;++i) d[i]=0;
		for(int i=1;i<n;++i) ++d[a[i].u],++d[a[i].v];
		for(int i=1;i<=n;++i) as=1ll*as*fac[d[i]-1]%mod;
		printf("%d\n",as);
	}
}

void solve(){
	scanf("%d%d",&n,&k);
	for(int i=1;i<n;++i){
		int u,v; scanf("%d%d",&u,&v);
		a[i]={u,v};
	}
	for(int i=1;i<=k;++i) scanf("%d",&p[i]);
	if(typ<=3) return sub1::solve();
	return sub2::solve();
}

signed main(){
	FIO("traverse");
	scanf("%d%d",&typ,&T);
	while(T--) solve();
	return 0;
}