#include<iostream>

using namespace std;

const int mod=1e9+7;
const int maxn=1e5+10;
int c,T,n,head[maxn],k,a[maxn];
struct edge
{
	int to,nxt;
}g[maxn];

int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	freopen("traverse.in","r",stdin);
	freopen("traverse.out","w",stdout);
	cin>>c>>T;
	while(T--)
	{
		cin>>n>>k;
		for(int i=1;i<n;i++)
		{
			int u,v;
			cin>>u>>v;
		}
		for(int i=1;i<=k;i++) cin>>a[i];
		if(c==18)
		{
			if(k==0) cout<<0<<"\n";
			else cout<<1<<"\n";
			continue;
		}
		if(c==19||c==20||c==21)
		{
			long long aa=1,bb=1,cc,ans=0;
			for(int i=1;i<=n-3;i++) bb=bb*i%mod; aa=bb*(n-2)%mod;
			cc=aa;
			for(int i=k;i>=1;i--)
			{
				ans=(ans+cc)%mod;
				cc=(cc-bb)%mod;
			}
			cout<<(ans+mod)%mod<<"\n";
		}
	}
}


























