#include<bits/stdc++.h>
#define int long long
const int N=1e5+3,modd=1e9+7;
using namespace std;
int Test,n,k,id[N];
int U[N],V[N];
vector<int>e[N];
namespace n5{
	void solve(){
		cout<<"2\n";
	}
}
namespace test19{
	int fac[N];
	void solve(){
		fac[0]=1;
		for(int i=1;i<=n;++i)
			fac[i]=fac[i-1]*i%modd;
		int ans=0;
		for(int i=1;i<=k;++i){
			int u=n-i;
			ans=(ans+fac[u])%modd;
		}
		cout<<ans<<'\n';
	}
}
void Solve(){
	cin>>n>>k;
	for(int i=1;i<=n;++i)
		e[i].clear();
	for(int i=1;i<n;++i){
		int u,v; cin>>u>>v;
		e[u].push_back(v);
		e[v].push_back(u);
		U[i]=u; V[i]=v;
	}
	for(int i=1;i<=k;++i)
		cin>>id[i];
	if(Test==18){
		cout<<"1\n";
		return;
	} else if(Test<=3){
		n5::solve();
		return;
	} else if(19<=Test && Test<=21){
		test19::solve();
		return;
	} else cout<<"1\n";
}
signed main(){
	freopen("traverse.in","r",stdin);
	freopen("traverse.out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(nullptr); int Ks;
	cin>>Test>>Ks; while(Ks--)Solve();
	return 0;
}

//Away from OI
// by McIron233
