#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e5+5,MOD=1e9+7;
int n,k,e[N],c;
vector <int> v[N];
void solve()
{
	cin>>n>>k;
	for(int i=1;i<=n-1;i++)
	{
		int u,v2;
		cin>>u>>v2;
		v[u].push_back(v2);
		v[v2].push_back(u);
	}
	for(int i=1;i<=n;i++) cin>>e[i];
	if(c==18)
	{
		cout<<"1\n";
	}else if(c>=19 && c<=21)
	{
//		cout<<"a";
		int ans=k;
		for(int i=n-1;i>=1;i--)
		{
			ans=(ans*i)%MOD;
		}
		cout<<ans<<"\n";
	}
}
signed main()
{
	freopen("traverse.in","r",stdin);
	freopen("traverse.out","w",stdout);
	int _;
	cin>>c>>_;
	while(_--)
	{
		solve();
	}
}
