#include <bits/stdc++.h>
#define int long long
using namespace std;
int read(){
    int x = 0, fg = 1;
    char ch = getchar();
    while(ch < '0' || ch > '9'){if(ch == '-') fg = -1;ch = getchar();}
    while(ch >= '0' && ch <= '9'){x = x * 10 + ch - '0';ch = getchar();}
    return x * fg;
}
const int N = 1e5 + 100;
const int mod = 1e9 + 7;
int fc[N];
int n, k, ie[N], deg[N];
vector<int> g[N];
int32_t main(){
    freopen("traverse.in","r",stdin);
    freopen("traverse.out","w",stdout);
    fc[0] = 1;for(int i = 1;i < N;i++) fc[i] = fc[i - 1] * i % mod;
    int _c = read();
    int cas = read();
    while(cas--){
        n = read(), k = read();
        for(int i = 1;i <= n;i++) g[i].clear(), deg[i] = 0;
        for(int i = 1;i < n;i++){
            int u = read();
            int v = read();
            deg[u]++, deg[v]++;
            g[u].push_back(v);
            g[v].push_back(u);
        }
        for(int i = 1;i <= k;i++) ie[i] = read();
        if(_c <= 6){
            int ans = 1;
            for(int i = 1;i <= n;i++) ans = ans * fc[deg[i] - 1] % mod;
            cout << ans << endl;
            continue;
        }
        if(_c == 18){
            puts("1");
            continue;
        }
        if(_c >= 19 && _c <= 21){
            if(n == 2) cout << 1 << endl;
            else{
                int x = k * fc[n - 2] % mod;
                int y = k * (k - 1) / 2 % mod * fc[n - 3] % mod;
                cout << (x - y + mod) % mod << endl;
            }
            continue;
        }
    }
}