#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
const int N=1e5+5,M=2*N,mod=1e9+7;

int n,kk,ind[N];
int h[N],e[M],ne[M],idx;
LL jie[M];
bool vis[M];

void add(int u,int v){
	ne[idx]=h[u],e[idx]=v,h[u]=idx++;
}

LL dfs(int u){
	LL res=jie[ind[u]];
	for(int i=h[u];~i;i=ne[i]){
		if(vis[i]) continue;
		int v=e[i];
		vis[i]=vis[i^1]=true;
		ind[v]--;
		res=res*dfs(v)%mod;
		ind[v]++;
		vis[i]=vis[i^1]=false;
	}
	return res;
}

int main(){
	freopen("traverse.in","r",stdin);
	freopen("traverse.out","w",stdout);
	ios::sync_with_stdio(0);
	
	int ttt,ccc;
	int u,v,x,y;
	LL res;
	jie[0]=1;
	for(int i=1;i<M;i++) jie[i]=jie[i-1]*i%mod;
	cin>>ccc>>ttt;
	while(ttt--){
		cin>>n>>kk;
		memset(h,-1,sizeof h);
		memset(ind,0,sizeof ind);
		memset(vis,0,sizeof vis);
		idx=0;
		res=1;
		for(int i=1;i<n;i++){
			cin>>u>>v;
			add(u,v),add(v,u);
			ind[u]++,ind[v]++;
		}
		x=n-2;
		for(int i=1;i<=kk;i++){
			cin>>u;
			if(ccc==18) res=1;
			else if(ccc>=19&&ccc<=21){
				if(x) res=(res+jie[n-3]*x%mod)%mod,x--;
			}else{
				x=2*(u-1),y=2*(u-1)+1;
				vis[x]=vis[y]=true;
				ind[e[x]]--;
				ind[e[y]]--;
				res=dfs(e[x])*dfs(e[y])%mod;
				vis[x]=vis[y]=false;
				ind[e[x]]++;
				ind[e[y]]++;
			}
		}
		cout<<res%mod<<endl;
	}
	return 0;
}
/*

*/
