#include <bits/stdc++.h>
#define N 100010
#define ll long long
#define P 1000000007
using namespace std;
int c, T, n, k;
int U[N], V[N], dep[N], son[N], e[N];
int head[N], nxt[2 * N], to[2 * N], cnt;
ll f[N], g[N], ff[N], dp[N], fac[N], ans;
mt19937 rnd(114514);
void addEdge(int u, int v) {
    cnt ++;
    to[cnt] = v;
    nxt[cnt] = head[u];
    head[u] = cnt;
}
int read() {
    int x = 0;
    char c = '.';
    while(c < '0' || c > '9') c = getchar();
    while(c >= '0' && c <= '9') {
        x = x * 10 + c - '0';
        c = getchar();
    }
    return x;
}
ll qpow(ll x, ll y) {
    if(y == 0) return 1;
    if(y & 1) return x * qpow(x, y - 1) % P;
    ll tmp = qpow(x, y / 2);
    return tmp * tmp % P;
}
vector<pair<int, int>> a;
bool vis[N];
void dfs(int x) {
    vis[x] = 1;
    vector<int> go;
    for(int i = 1; i < n; i ++) {
        go.push_back(i);
    }
    shuffle(go.begin(), go.end(), rnd);
    for(int i : go) if(!vis[i]) {
        if(U[i] == U[x] || V[i] == U[x] || U[i] == V[x] || V[i] == V[x]) {  // 相邻边
            dfs(i);
            if(i < x) a.push_back(make_pair(i, x));
            else a.push_back(make_pair(x, i));
        }
    }
}
int main() {
    freopen("traverse.in", "r", stdin);
    freopen("traverse.out", "w", stdout);
    fac[0] = 1;
    for(int i = 1; i < N; i ++) fac[i] = fac[i - 1] * i % P;
    c = read(); T = read();
    while(T --) {
        set<vector<pair<int, int>>> ma;
        n = read(), k = read();
        cnt = 0;
        for(int i = 1; i <= n; i ++) head[i] = 0;
        for(int i = 1; i < n; i ++) {
            U[i] = read(); V[i] = read();
            addEdge(U[i], V[i]);
            addEdge(V[i], U[i]);
        }
        for(int i = 1; i <= k; i ++) {
            e[i] = read();
            if(dep[U[e[i]]] > dep[V[e[i]]]) {
                swap(U[e[i]], V[e[i]]);
            }
        }
        if(c <= 3 || n <= 5) {
            ans = 0;
            for(int _ = 1; _ <= 500; _ ++) {
                for(int i = 1; i <= k; i ++) {
                    a.clear();
                    for(int j = 1; j <= n; j ++) vis[j] = 0;
                    dfs(e[i]);
                    sort(a.begin(), a.end());
                    if(!ma.count(a)) {
                        ma.insert(a);
                        ans ++;
                    }
                }
            }
            printf("%lld\n", ans);
            continue;
        } else if(c == 18) {
            printf("1\n");
            continue;
        } else if(19 <= c && c <= 21) {
            ans = k * (k - 1) / 2 % P * fac[n - 1 - 2] % P;
            ans += (n - k - 1) * k % P  * fac[n - 1 - 2] % P;
            ans %= P;
            printf("%lld\n", ans);
            continue;
        }
    }
}
