#include<bits/stdc++.h>
using namespace std;
#define LL long long
const LL MAXN=1e5+5,MOD=1e9+7;
template<typename T>inline void read(T &x){
    bool f=1;x=0;char ch=getchar();
    while(ch>'9'||ch<'0'){if(ch=='-')f=!f;ch=getchar();}
    while(ch<='9'&&ch>='0'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
    x=f?x:-x;return ;
}
template<typename T>inline void write(T x){
    if(x<0)putchar('-'),x=-x;
    if(x>9)write(x/10);
    putchar(x%10+'0');return ;
}
LL n,k,e[MAXN];
pair<LL,LL>edge[MAXN];
vector<LL>a[MAXN];
LL p[MAXN],v[MAXN];
set<pair<LL,LL> >vis;
map<set<pair<LL,LL> >,bool>viss;
bool cmp(LL x,LL y){
    return p[x]<p[y];
}
bool vi[MAXN];
void dfs(LL x){
    sort(a[x].begin(),a[x].end(),cmp);
    for(LL i=0;i<a[x].size();i++){
        LL y=a[x][i];
        if(vi[y])continue;
        vi[y]=true;
        vis.insert({x,y});
        vis.insert({y,x});
        dfs(y);
    }
}
void solve1(){
    for(LL i=1;i<=n;i++)a[i].clear();
    for(LL i=1;i<n;i++){
        for(LL j=i+1;j<n;j++){
            if(edge[i].first==edge[j].first||edge[i].second==edge[j].first||edge[i].first==edge[j].second||edge[i].second==edge[j].second){
                a[i].push_back(j);
                a[j].push_back(i);
            }
        }
    }
    viss.clear();
    LL anss=0;
    for(LL ii=1;ii<=k;ii++){
        for(LL i=1;i<=n;i++)p[i]=i;
        do{
            for(LL i=1;i<=n;i++){
                v[p[i]]=i;
            }
            for(LL i=1;i<=n;i++)vi[i]=false;
            vi[e[ii]]=true;
            vis.clear();
            dfs(e[ii]);
            if(!viss[vis]){
                viss[vis]=true;
                anss++;
            }
        }while(next_permutation(p+1,p+n+1));
    }
    write(anss);putchar('\n');
}
void solve2(){
    LL cntt=0;
    for(LL i=1;i<=k;i++){
        if(e[i]==1){
            cntt++;
        }
    }
    LL anss=1;
    for(LL i=1;i<=k-cntt;i++){
        anss=(anss*i)%MOD;
    }
    printf("%lld\n",anss);
}
int main(){
    freopen("traverse.in","r",stdin);
    freopen("traverse.out","w",stdout);
    LL c,T;
    read(c);read(T);
    while(T--){
        read(n);read(k);
        for(LL i=1;i<n;i++){
            read(edge[i].first);read(edge[i].second);
        }
        for(LL i=1;i<=k;i++){
            read(e[i]);
        }
        if(c<=3){
            solve1();
            continue;
        }
        if(c==18){
            printf("1\n");
            continue;
        }
        if(c==19){
            solve2();
            continue;
        }
        solve1();
    }
}