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

ll c,T,n,k,f[100005] = {},N = 1e9+7;
/*
struct bian{
	int u,v;
}a[100005];
*/
int main(){
	freopen("traverse.in","r",stdin);
	freopen("traverse.out","w",stdout);
	memset(f,0,sizeof(f));
	cin>>c>>T;
	while(T--){
		cin>>n>>k;
		if(k == 1){
			int u,v,ans = 1;
			for(int i = 1;i <= n-1;i++){
				cin>>u>>v;
				//cout<<f[u]<<" "<<f[v]<<endl;
				f[u]++;
				f[v]++;
				//cout<<f[u]<<" "<<f[v]<<endl;
			}
			int rt;
			cin>>rt;
			//cout<<endl;
			for(int i = 1;i <= n;i++){
				if(f[i]-1>0){
					//cout<<i<<" "<<f[i]<<" "<<ans<<endl;
					ans *= f[i]-1;
				}
			}
			cout<<ans%N<<endl;
			memset(f,0,sizeof(f));
			continue;
		}
		cout<<(k*n)/2<<endl;
	}
	fclose(stdin);
	fclose(stdout);
	return 0;
}

