#include<bits/stdc++.h>
using namespace std;
const long long mod=1e9+7;
int n,k;

long long f[100005];
void init()
{
	f[0]=1;
	for(int i=1;i<=100003;i++)
	{
		f[i]=f[i-1]*i%mod;
	}
}

inline void read()
{
	char c;
	while((c=getchar())>='0'&&c<='9');
}



int main()
{
	freopen("traverse.in","r",stdin);
	freopen("traverse.out","w",stdout);
	int c,t;  scanf("%d %d",&c,&t);
	if(c>=19&&c<=21)init();
	while(t--)
	{
		if(c==18)printf("1\n");
		if(c>=19&&c<=21)
		{
			scanf("%d %d\n",&n,&k);
			for(int i=1;i<n;i++)
			{
			read();read(); 
			}
			for(int i=1;i<=k;i++)read();
			printf("%lld\n",(f[n-2]*k%mod+(mod-1)*(k*(k-1)/2%mod)%mod*f[n-3]%mod)%mod);
		}
	}
	return 0;
} 
