#include <iostream>
#include <fstream>
#include <cstdint>
#include <cstddef>
#include <string>
#include <vector>
#include <algorithm>
#include <cassert>

#define FILE
#define FILE_NAME "traverse"

#define type using
#define fn auto
#define letmut auto
#define let auto const
#define String std::string
#define Vec std::vector

#ifdef FILE
std::ifstream in(FILE_NAME ".in");
std::ofstream out(FILE_NAME ".out");
#else
#define in std::cin
#define out std::cout
#endif // FILE

type i32 = std::int32_t;
type i64 = std::int64_t;
type u64 = std::uint64_t;
type usize = std::size_t;

const usize MAX_N = 1e5 + 10;
const i64 MOD = 1e9 + 7;

u64 c, t;
i64 n, k;

i64 qpow(i64 a, i64 b) {
	if (b == 0) {
		return 1;
	} else if (b % 2 == 0) {
		return qpow(a * a % MOD, b / 2);
	} else {
		return qpow(a, b - 1) * a % MOD;
	}
}

struct Edge {
	usize u{}, v{};
	Edge() {}
	Edge(usize u, usize v): u{u}, v{v} {}
};

Edge e[MAX_N];
//Edge ee[MAX_N];

std::vector<usize> key;

std::vector<std::pair<usize, usize>> node[MAX_N];
std::vector<usize> enode[MAX_N];
std::vector<usize> etree[MAX_N];
i64 fa[MAX_N];

bool tag[MAX_N];
i64 f[MAX_N];
i64 bro[MAX_N];
i64 mul_son[MAX_N];

i64 fact[MAX_N]{};

void build_e_tree() {
	for (usize u = 1; u <= n; ++u) {
		Vec<usize> tmp = Vec<usize>();
		for (let p: node[u]) {
			usize eid = p.first;
			tmp.push_back(eid);
		}
		for (let uu: tmp) {
			for (let vv: tmp) {
				if (uu != vv) {
					enode[uu].push_back(vv);
					enode[vv].push_back(uu);
				}
			}
		}
	}
} 

i64 dfs1(usize u) {
	i64 ans = 1;
	tag[u] = true;
	letmut size = i64(0);
	for (let v: enode[u]) {
		if (!tag[v]) {
			etree[u].push_back(v);
			fa[v] = u;
			ans *= dfs1(v);
			ans %= MOD;
			++size;
		}
	}
	mul_son[u] = ans;
	ans *= fact[size];
	ans %= MOD;
	f[u] = ans;
	return ans;
} 

void solve1() {
	i64 ans{};
	for (let k: key) {
		std::fill(tag, tag + n + 1, false);
		ans += dfs1(k);
		ans %= MOD;
	}
	out << ans << "\n";
}

void dfs2(usize u) {
	tag[u] = true;
	for (let v: enode[u]) {
		if (!tag[v]) {
			if (u != 1) {
//				g[v] = (g[u] * mul_bro[u] % MOD) * fact[bro[u] + 1] % MOD;
			} 
			
		}
	}
}

void solve2() {
	u64 ans{1};
	for (u64 i = 1; i <= n; ++i) {
		ans = ans * (node[i].size() <= 2 ? 1 : node[i].size() );
		ans %= MOD;
	}
	out << ans << "\n";
}

fn main() -> i32
{
	fact[0] = 1;
	for (i32 i = 1; i <= 100000; ++i) {
		fact[i] = fact[i - 1] * i % MOD;
	}
	in >> c >> t;
	while (t--) {
		in >> n >> k;
		for (letmut i = usize(1); i < n; ++i) {
			usize u, v;
			in >> u >> v;
			e[i] = Edge(u, v);
			node[u].push_back({i, v});
			node[v].push_back({i, u});
		}
		for (usize i = 1; i <= k; ++i) {
			usize x;
			in >> x;
			key.push_back(x);
		}
		build_e_tree();
		if (c == 18) {
			out << "1\n"; 
		} else if (19 <= c && c <= 21) {
			out << n - 1 << "\n";
		}else {
			solve2();
		}
	} 
    return 0;
}
