#include<iostream>
#include <vector>
#define int long long
using namespace std;
const int N=1e5+5,mod=1e9+7;
int c,t,n,m,k;
vector<int> e[N];
bool vis[N];
int dfs(int u,int fa){
	int sum=0;
	if(vis[u]) return 1;
	for(int i=e[u].size()-1;i>=0;i--){
		int v=e[u][i];
		if(v==fa) continue;
		sum=(sum+(e[u].size()-1)*dfs(v,u)%mod)%mod;
	}
	return sum;
}
signed main()
{
	freopen("traverse.in","r",stdin);
	freopen("traverse.out","w",stdout);
	scanf("%lld%lld",&c,&t);
	if(c<=6)while(t--){
		scanf("%lld%lld",&n,&k);
		for(int i=1;i<=n;i++) e[i].clear(),vis[i]=0;
		int du,dv;
		for(int i=1;i<n;i++) {
			int u,v;
			scanf("%lld%lld",&u,&v);
			e[u].push_back(v);
			e[v].push_back(u);
			if(i==1) du=u,dv=v;
		}
		printf("%lld\n",dfs(du,0));
	}	
	else{
		while(t--){
		scanf("%lld%lld",&n,&k);
		for(int i=1;i<=n;i++) e[i].clear();
		for(int i=1;i<n;i++) {
			int u,v;
			scanf("%lld%lld",&u,&v);
			e[u].push_back(v);
			e[v].push_back(u);
		}
		if(c==18) printf("1\n");
		else{
			int ans=k;
			for(int i=1;i<n;i++) ans=ans*i%mod;
			printf("%lld\n",ans);
		}
		}
}
	return 0;
}

