#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

const int mod = 1e9 + 7;

int read()
{
	int ret = 0 ,f = 1;
	char ch = getchar();
	while (!isdigit(ch))
		f *= ch == '-' ? -1 : 1 ,ch = getchar();
	while (isdigit(ch))
		ret = ret * 10 + ch - '0' ,ch = getchar();
	return ret * f;
}

void solve1()
{
	int n = read() ,k = read();
	
	for (int i = 1;i < n;i++)
		int u = read() ,v = read();
	
	ll ans = 0;
	for (int i = 1;i <= k;i++)
		int e = read();
	
	puts("1");
}

void solve2()
{
	int n = read() ,k = read();
	
	for (int i = 1;i < n;i++)
		int u = read() ,v = read();
	
	for (int i = 1;i <= k;i++)
		int e = read();
	
	ll ans = 1;
	for (int i = 1;i < n;i++)
		ans = ans * i % mod;
//	ans = ans * k % mod;
	
	printf("%lld\n",ans);
}

int main()
{
	freopen("traverse.in" ,"r" ,stdin);
	freopen("traverse.out" ,"w" ,stdout);
	int c ,T;
	scanf("%d%d",&c ,&T);
	
	while (T--)
	{
		if (c == 18)
			solve1();
		else if (c == 19)
			solve2(); 
	}
	
	return 0;
}
