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

#define ll long long
#define INF 0x3f3f3f3f
#define int ll

int rd()
{
	int x = 0,f= 1;
	char c= getchar();
	while (c < '0' || c > '9')
	{
		if (c == '-') f = -1;
		c = getchar();
	}
	while (c >= '0' && c <= '9') x = (x<<1)+(x<<3) +(c^48),c = getchar();
	return x * f;
}

const int N = 4e5+5,mod = 1e9 + 7;

int n ,t,k,c;

vector<int> v[N];

int deg[N];
int jc[N];

void solve6()
{
	
	for (int i = 1;i <= n-1;i++)
	{
		int x= rd(),y = rd();
		deg[x]++,deg[y]++;
	}
	int bh = rd();
	ll ans = 1;
	for (int i = 1;i <= n;i++)
	{
		ans = ans * jc[deg[i]-1] %mod;	
	}
	printf("%lld\n",ans);
}

int bh[N];

void add(int a,int b)
{
	v[a].push_back(b);
}

int fat[N];

void dfs(int x,int fa)
{
	fat[x] = fa;
//	cout << x<< endl;
	for (auto y : v[x])
	{
		if (y == fa) continue;
		dfs(y,x);
	}
}
int rec[N];
int cnt[N];
signed main()
{
	freopen("traverse.in","r",stdin);
	freopen("traverse.out","w",stdout);
	jc[0] = 1;
	for (int i = 1;i <= (int)1e5;i++) jc[i] = jc[i-1] * i % mod;
	cin >>c >> t;
	while (t--)
	{
		memset(rec,0,sizeof rec);memset(cnt,0,sizeof cnt);memset(fat,0,sizeof fat);
		n =rd(),k =rd();
		for (int i = 0;i <= n*2;i++) v[i].clear();
		for (int i = 1;i <= n*2;i++) deg[i] = 0;
		if (c <= 6)
		{
			solve6();
			continue;
		}
		else if (c == 18)
		{
			for (int i = 1;i <= n-1;i++) 
			{
				int x= rd(),y =rd();
			}
			for (int i = 1;i <= k;i++) 
			{
				int bh = rd();
			}
			puts("1");
			continue;
		}
		else if (c >= 19 && c <= 21)
		{
			for (int i = 1;i <= n-1;i++) 
			{
				int x= rd(),y =rd();
			}
			for (int i = 1;i <= k;i++) 
			{
				int bh = rd();
			}
			int ans = 0;
			if (n == 2) 
			{
				puts("1");
				continue;
			}
			for (int i = 1;i <= k;i++)
			{
				(ans += (n-i-1))%=mod;
			}
			ans = ans * jc[n-3] % mod;
			printf("%lld\n",ans);
		}
		else
		{
			for (int i = 1;i <= n-1;i++)
		{
			int x= rd(),y = rd();
			add(x,n+i);add(n+i,y);add(y,n+i);add(n+i,x);
			deg[x]++,deg[y]++;
		}
		
		for (int i = 1;i <= k;i++)
		{
			bh[i] = rd();
		}
		ll ans = 1;
		for (int i = 1;i <= n;i++)
		{
			rec[i] = deg[i]-1;	cnt[i] = 1;
		}
	//	printf("%lld\n",ans);
		dfs(bh[1]+n,0);
		for (int i = 2;i <= k;i++)
		{
			int x = bh[i] + n;
			int a = v[x][0],b = v[x][1];
			if (fat[a] != x) cnt[a]++,(rec[a] += (deg[a]-cnt[a]))%=mod;
			if (fat[b] != x) cnt[b]++,(rec[b] += (deg[b]-cnt[b]))%=mod;
		}
		
		for (int i = 1;i <= n;i++)
		{
			if (deg[i] < 2) continue;
			int jj = 1;
			if (deg[i] >= 2) jj = jc[deg[i]-2]; 
			ans = ans * jj %mod * rec[i] % mod;
		}
		printf("%lld\n",ans);
		}
		
	}
	return 0;
}

/*
7 1
5 2
1 2
1 3
2 4
2 5
1 2
*/

