#include <bits/stdc++.h>
using namespace std;

const int maxn = 1e5 + 5;
const int mod = 1e9 + 7;

inline int read() {
    int x = 0, ch = getchar();
    for (; ch < '0' || ch > '9'; ch = getchar());
    for (; ch >= '0' && ch <= '9'; ch = getchar())
        x = (x << 3) + (x << 1) + (ch ^ 48);
    return x;
}

int n, k;
int ed[maxn];
bool fl[maxn];

int tot, h[maxn];
struct Edge {
    int v, nxt;
} e[maxn << 1];
void add(int u, int v) {
    e[++tot] = {v, h[u]}, h[u] = tot;
}

namespace A {
    void solve() {
        puts("1");
    }
}

namespace B {
    void solve() {
        int ans = k;
        for (int i = 1; i < n; ++i)
            ans = 1ll * ans * i % mod;
        printf("%d\n", ans);
    }
}

int main() {
    freopen("traverse.in", "r", stdin);
    freopen("traverse.out", "w", stdout);
    int sub = read(), T = read();
    while (T--) {
        n = read(), k = read();
        memset(h, tot = 0, sizeof(h));
        for (int i = 1; i < n; ++i) {
            int u = read(), v = read();
            add(u, v), add(v, u);
        }
        for (int i = 1; i <= k; ++i)
            fl[ed[i] = read()] = true;
        if (sub == 18)
            A::solve();
        else if (19 <= sub && sub <= 21)
            B::solve();
        for (int i = 1; i <= k; ++i)
            fl[ed[i]] = false;
    }
    return 0;
}
