#include<iostream>
#include<cstdio>
#define int long long
using namespace std;
const int Mode = 1e9 + 7;
const int N = 1e5 + 5;
int T,c,n,k;
signed main()
{
	freopen("traverse.in","r",stdin);
	freopen("traverse.out","w",stdout);
	scanf("%lld%lld",&c,&T);
	while(T--)
	{
		scanf("%lld%lld",&n,&k);
		//cout << n << " " << k << endl;
		if(c == 18)
		{
			for(int i = 1; i < n; i++)
			{
				int s1,s2;
				scanf("%lld%lld",&s1,&s2);
			}
			for(int i = 1; i <= k; i++)
			{
				int s;
				scanf("%lld",&s);
			}
			printf("1\n");
		}
		if(c >= 19 && c <= 21)
		{
			int res = 1;
			for(int i = 1; i < n; i++)
			{
				int s1,s2;
				scanf("%lld%lld",&s1,&s2);
			}
			for(int i = 1; i <= k; i++)
			{
				int s;
				scanf("%lld",&s);
			}
			for(int i = 1; i <= n - 2; i++) res = (res * i) % Mode;
			printf("%lld\n",k * res % Mode);
		}
	}
	fclose(stdin);
	fclose(stdout);
	return 0;
}
