#include<bits/stdc++.h>
using namespace std;
const int mod=1000000007;
int c,t,n,k,fac[100005],u[100005],v[100005],e[100005],deg[100005];
int main(){
  freopen("traverse.in","r",stdin);
  freopen("traverse.out","w",stdout);
  ios::sync_with_stdio(0),cin.tie(0),fac[0]=1;
  for(int i=1;i<=100000;i++)fac[i]=1ll*fac[i-1]*i%mod;
  cin>>c>>t;
  while(t--){
    cin>>n>>k;
    for(int i=1;i<n;i++)cin>>u[i]>>v[i];
    for(int i=1;i<=k;i++)cin>>e[i];
    if(c==18)cout<<"1\n";
    else if(c>=19&&c<=21)cout<<(n==2?1:(1ll*k*fac[n-2]%mod-1ll*k*(k-1)/2%mod*fac[n-3]%mod+mod)%mod)<<'\n';
    else{
      for(int i=1;i<n;i++)deg[u[i]]++,deg[v[i]]++;
      int ans=1;
      for(int i=1;i<=n;i++)ans=1ll*ans*fac[deg[i]-1]%mod;
      cout<<ans<<'\n',memset(deg,0,sizeof(deg));
    }
  }
  return 0;
}
