#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define bll __int128
#define vii vector<int>
#define vll vector<ll>
#define pb push_back
#define ppb pop_back
#define pii pair<int,int>
#define pll pair<ll,ll>
#define fi first
#define sec second
#define mp make_pair
#define qii queue<int>
#define puu pair<ull,ull>
#define clr clear
using namespace std;
const int N=2e5+5,mod=1e9+7;
int c,T;
int n,m,fx,fy;
int head[N],nex[N],to[N],tot;
int a[N],X[N],Y[N],son[N];
ll fac[N],ifac[N],f[N];
void add_edge(int x,int y){tot++,nex[tot]=head[x],head[x]=tot,to[tot]=y;}
ll ksm(ll x,int y){ll z=1;while(y){if(y&1)z=z*x%mod;x=x*x%mod,y>>=1;}return z;}
void clear(){for(int i=1;i<=n;i++)head[i]=0;tot=0;}
void get(int x,int from)
{
	f[x]=1,son[x]=0;
	for(int i=head[x];i;i=nex[i])
	{
		int y=to[i];
		if(y!=from)get(y,x),f[x]=f[x]*f[y]%mod,son[x]++;
	}
	f[x]=f[x]*fac[son[x]]%mod;
}
int main()
{
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	freopen("traverse.in","r",stdin);
	freopen("traverse.out","w",stdout);
	fac[0]=1;for(int i=1;i<=1e5;i++)fac[i]=fac[i-1]*i%mod;
	for(int i=0;i<=1e5;i++)ifac[i]=ksm(fac[i],mod-2);
	cin>>c>>T;
	if(c==18)
	{
		while(T--)
		{
			cin>>n>>m;
			for(int i=1,x,y;i<n;i++)cin>>x>>y;
			for(int i=1;i<=m;i++)cin>>a[i];
			cout<<1<<endl;
		}
		return 0;
	}
	if(19<=c&&c<=21)
	{
		while(T--)
		{
			cin>>n>>m;ll ans=0;
			for(int i=1,x,y;i<n;i++)cin>>x>>y;
			for(int i=1;i<=m;i++)cin>>a[i];
			ans=1ll*m*fac[n-2];
			if(m>1)ans=(ans-(1ll*m*(m-1)/2)%mod*fac[n-3]%mod+mod)%mod;
			cout<<ans<<endl;
		}
		return 0;
	}
	if(c<=6)
	{
		while(T--)
		{
			clear();
			cin>>n>>m;
			for(int i=1,x,y;i<n;i++)cin>>x>>y,add_edge(x,y),add_edge(y,x),X[i]=x,Y[i]=y;
			int x;cin>>x;
			get(X[x],Y[x]),get(Y[x],X[x]);
			cout<<f[X[x]]*f[Y[x]]%mod<<endl;
		}
		return 0;
	}
}
