#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=1e5+10,mod=1e9+7;
ll c,t,n,m,ans;
ll fac[N],inv[N],a[N];
ll read()
{
	ll x=0;char c=getchar();
	for(;!isdigit(c);c=getchar());
	for(;isdigit(c);c=getchar())x=(x<<3)+(x<<1)+(c^48);
	return x;
}
void init()
{
	fac[0]=fac[1]=inv[0]=inv[1]=1;
	for(ll i=2;i<N;i++)
	{
		fac[i]=fac[i-1]*i%mod;
		inv[i]=(mod-mod/i)*inv[mod%i]%mod;
	}
	for(ll i=2;i<N;i++)
		inv[i]*=inv[i-1],inv[i]%=mod;
}
int C(ll x,ll y)
{
	return fac[x]*inv[y]%mod*inv[x-y]%mod;
}
int main()
{
	freopen("traverse.in","r",stdin);
	freopen("traverse.out","w",stdout);
	c=read(),t=read();
	init();
	while(t--)
	{
		ans=0;
		n=read(),m=read();
		for(ll i=1,x,y;i<n;i++)
			x=read(),y=read();
		for(int i=1;i<=m;i++)
			a[i]=read();
		if(c==18)
		{
			puts("1");
			continue;
		}
		if(c>=19&&c<=21)
		{
			if(n<=3)printf("%d\n",n==2?1:1);
			else
			{
				for(int i=1;i<=m;i++)
					ans+=fac[n-2]-(i-1)*fac[n-3]%mod+mod,ans%=mod;
				printf("%lld\n",ans);
			}
			continue;
		}
	}
	return 0;
}

