#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; const int mod = 1000000007; int inv[100005]; int add(int a, int b) { a += b; if (a >= mod) a -= mod; if (a < 0) a += mod; return a; } int mul(int a, int b) { return 1LL * a * b % mod; } int powm(int a, int b) { int r = 1; while (b) { if (b & 1) r = mul(r, a); b >>= 1; a = mul(a, a); } return r; } vector edges[100005]; int n, m; bool dfsFindPath(int u, int p, vector& ppp) { if (u == m) { ppp.push_back(u); return true; } for (int v: edges[u]) if (v != p) { if (dfsFindPath(v, u, ppp)) { ppp.push_back(u); return true; } } return false; } int choose(int a, int b) { return mul(a, inv[b]); } pair dfs(int u, int p, vector& ppp, int ii) { if (u == m) return {1, 0}; pair s = dfs(ppp[ii], u, ppp, ii + 1); int t = edges[u].size(); int p1 = mul(inv[t], s.first); int p21 = p == 0 ? choose(t - 1, t): choose(t - 2, t); p21 = mul(p21, choose(1, t - 1)); p21 = mul(p21, s.first); int p22 = mul(choose(1, t), s.second); int p2 = add( p21, p22 ); return {p1, p2}; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int t; cin >> t; for (int i = 1; i <= 100000; i++) { inv[i] = powm(i, mod - 2); } while (t--) { cin >> n >> m; for (int i = 0; i <= n; i++) edges[i].clear(); for (int i = 0; i < n - 1; i++) { int a, b; cin >> a >> b; edges[a].push_back(b); edges[b].push_back(a); } vector path; dfsFindPath(1, 0, path); reverse(path.begin(), path.end()); pair ret = dfs(1, 0, path, 1); cout << add(ret.first, ret.second) << "\n"; } }