#include<bits/stdc++.h>
long long mod=1000000007;
long long c,t,n,k,a[100009],b[100009],e,f[100009],ans,mp[1009][1009],mx,vis[100009],vvis[100009];
long long circlesize(long long x,long long y)
{
	for(int i=1;i<=n;++i)
	{
		if(mp[x][i]&&vis[i]==0)
		{
			mp[x][i]=0;
			vis[i]=1;
			return circlesize(i,y+1);
			mp[x][i]=1;
		}
		if(mp[x][i]&&vis[i]==1)
		{
			vvis[i]=y;
		}
	}
	return vvis[x];
}
int main()
{
	freopen("traverse.in","r",stdin);
	freopen("traverse.out","w",stdout);
	scanf("%lld%lld",&c,&t);
	f[0]=1;
	for(int i=1;i<=100000;++i)
	{
		f[i]=f[i-1]*i;
		f[i]%=mod;
	}
	for(int tt=1;tt<=t;++tt)
	{
		mx=0;
		ans=0;
		if(c==18)
		{
			printf("1\n");
		}
		else
		{
			scanf("%lld%lld",&n,&k);
			for(int i=1;i<n;++i)
			{
				scanf("%lld%lld",&a[i],&b[i]);
			}
			for(int i=1;i<=k;++i)
			{
				scanf("%lld",&e);
			}
			for(int i=1;i<=n;++i)
			{
				vis[i]=0;
				vvis[i]=1;
				for(int j=1;j<=n;++j)
				{
					if(a[i]==a[j]||b[i]==b[j])
					{
						mp[i][j]=1;
					}
					else
					{
						mp[i][j]=0;
					}
				}
			}
			printf("%lld",f[circlesize(1,1)-1]);
		}
	}
	return 0;
}
