#include<cstdio>
#include<cctype>
#include<vector>
#include<cstring>
using namespace std;
template<typename _Tp>
inline void read(_Tp& x){
    static char ch;
    while(ch=getchar(),!isdigit(ch));
    x=(ch^48);
    while(ch=getchar(),isdigit(ch))x=(x<<3)+(x<<1)+(ch^48);
}
template<typename _Tp,typename ..._Args>
inline void read(_Tp& x,_Args &...args){
    read(x);
    read(args...);
}
const int N=1e5+10,mod=1e9+7;
int n,c,k;
using ll=long long;
ll res;
namespace lnk{
void run(){
    read(n,k);
    res=1;
    for(int i=1,x;i<n;i++)read(x,x);
    for(int i=1,x;i<=k;i++)read(x);
    printf("%lld\n",res);
}
}
inline ll fsp(ll x,ll y){
    ll ret=1;
    while(y){
        if(y&1)ret=ret*x%mod;
        x=x*x%mod;
        y>>=1;
    }
    return ret;
}
namespace flower{
ll prod;
void run(){
    read(n,k);
    res=1;
    prod=1;
    for(int i=1;i<n-2;i++)prod=prod*i%mod;
    for(int i=1,x;i<n;i++)read(x,x);
    for(int i=1,x;i<=k;i++)read(x);
    res=prod*k%mod*(n-k-1)%mod;
    res=(res+prod*(1ll*k*(k-1)/2%mod)%mod)%mod;
    printf("%lld\n",res);
}
}
namespace cheat{
vector<int> road[N];
ll tk,ts,res;
bool buc[N];
void run(){
    read(n,k);
    res=1;
    for(int i=1;i<=n;i++)road[i].clear();
    for(int i=1,x,y;i<n;i++){
        read(x,y);
        road[x].emplace_back(i);
        road[y].emplace_back(i);
    }
    memset(buc+1,0,n-1);
    for(int i=1,x;i<=k;i++){
        read(x);
        buc[x]=true;
    }
    for(int i=1;i<=n;i++){
        ts=road[i].size();
        if(ts==1)continue;
        tk=0;
        for(auto& j:road[i])tk+=buc[j];
        tk=max(tk,1ll);
        res=res*(tk*(ts-tk)%mod+tk*(tk-1)/2%mod)%mod;
    }
    printf("%lld\n",res);
}
}
void run(){
    if(c==18)return lnk::run();
    if(c>=19 and c<=21)return flower::run();
    return cheat::run();
}
int main(){
    freopen("traverse.in","r",stdin);
    freopen("traverse.out","w",stdout);
    int T=1;
    read(c,T);
    while(T--)run();
}
/*
10:37 link captured. 4 points.
10:43 flower failed. Still 4 points.
11:05 flower captured. 16 points.
11:32 16+eps points.
*/