#include<bits/stdc++.h>
using namespace std;
#define N 100005
#define mod 1000000007
inline void Del(int &x,int y){x-=y;(x<0)&&(x+=mod);}
inline int qpow(int x,int y){int ret=1;for(;y;y>>=1,x=1ll*x*x%mod)if(y&1)ret=1ll*ret*x%mod;return ret;}
int n,m,is[N];
int tot,head[N];
struct Edge{int next,to;}e[N<<1];
void add_edge(int u,int v){e[++tot]={head[u],v};head[u]=tot;}
void clrG()
{
	tot=1;
	memset(head,0,n+1<<2);
	memset(is,0,n+1<<2);
}
namespace Solve1
{
	struct edge{int u,v;}E[N<<1];
	int f[N],fac[N];
	void dfs(int u,int fa)
	{
		f[u]=1;int son=0;
		for(int i=head[u];i;i=e[i].next)
		{
			int v=e[i].to;if(v==fa) continue;
			son++;dfs(v,u);f[u]=1ll*f[u]*f[v]%mod;
		}
		f[u]=1ll*f[u]*fac[son]%mod;
	}
	void solve()
	{
		scanf("%d%d",&n,&m);
		fac[0]=1;
		for(int i=1;i<=n;i++)
			fac[i]=1ll*fac[i-1]*i%mod;
		clrG();
		for(int i=1;i<n;i++)
		{
			int x,y;scanf("%d%d",&x,&y);
			add_edge(x,y);add_edge(y,x);
			E[i]={x,y};
		}
		int ed;scanf("%d",&ed);
		int x=E[ed].u,y=E[ed].v;
		dfs(x,y);dfs(y,x);
		printf("%lld\n",1ll*f[x]*f[y]%mod);
	}
}
namespace Solve8
{
	void solve()
	{
		scanf("%d%d",&n,&m);
		clrG();
		for(int i=1;i<n;i++)
		{
			int x,y;scanf("%d%d",&x,&y);
			add_edge(x,y);add_edge(y,x);
		}
		for(int i=1;i<=m;i++)
		{
			int x;scanf("%d",&x);
			is[x]=1;
		}
		printf("1\n");
	}
}
namespace Solve9
{
	int fac[N];
	void solve()
	{
		scanf("%d%d",&n,&m);
		clrG();
		for(int i=1;i<n;i++)
		{
			int x,y;scanf("%d%d",&x,&y);
			add_edge(x,y);add_edge(y,x);
		}
		for(int i=1;i<=m;i++)
		{
			int x;scanf("%d",&x);
			is[x]=1;
		}
		fac[0]=1;
		for(int i=1;i<=n;i++)
			fac[i]=1ll*fac[i-1]*i%mod;
		if(n==2) return printf("1\n"),void();
		int inv2=qpow(2,mod-2),ans=1ll*fac[n-1]*inv2%mod;
		if(m>=n-2) return printf("%d\n",ans),void();
		Del(ans,1ll*(n-1-m)*(n-2-m)%mod*inv2%mod*fac[n-3]%mod);
		printf("%d\n",ans);
	}
}
int main()
{
	freopen("traverse.in","r",stdin);
	freopen("traverse.out","w",stdout);
	int C,T;scanf("%d%d",&C,&T);
	while(T--)
	{
		if(C>=1&&C<=6) Solve1::solve();	
		if(C==18) Solve8::solve();
		if(C>=19&&C<=21) Solve9::solve();
	}
}
