(Analysis by Benjamin Qi, code by Dong Liu)

Let's iterate over all $p$ and compute the probability of step 1 generating the tree in the input. For all $x\neq p_1$, let $a_{p_1}(x)$ denote the next vertex on the path from $x$ to $p_1$ in the input tree. The probability is then

We can calculate $1/(N-1)!\pmod{10^9+7}$ using modular exponentiation.

Subtask 1: $N\le 8$

Iterate over all $p$ to count how many are valid, which takes $O(N!\cdot poly(N))$ time. Finally, multiply by $1/N!\pmod{10^9+7}$.

Subtask 2: $N\le 1000$

For each value of $v\in [1,N]$, let's compute the probability of $p$ being generated so that $p_1=v$ and $a_v(x)$ appears before $x$ for all $x\neq v$. The probability of $v$ being the first element in $p$ is just one over the size of the tree, or $1/N$. Conditioned on $v$ being the first element of $p$, the probabilities of the condition being satisfied for the subtrees of all children of $v$ are independent, and we can repeat the argument for those subtrees. That is, for each child of $v$, the probability of it appearing before all of its descendants in $p$ is one over its subtree size. We can continue this argument down the tree, so we get that the probability is just one over the product of subtree sizes when the tree is rooted at $v$.

We can precompute the modular inverses of $1\dots N$, then for each $v$ perform a DFS in $O(N)$ time to get the product of subtree size inverses. Then sum them and divide by $(N-1)!$. The time complexity is $O(N^2)$.

Full Solution: No additional constraints.

Suppose we have two adjacent vertices $v$ and $w$ -- then the product of subtree sizes when the tree is rooted at $v$ or $w$ is nearly the same, and the only difference is that the first product contains the subtree size of $w$ when rooted at $v$ and the second product contains the subtree size of $v$ when rooted at $w$. So, given the product at any vertex of the tree we can find the product at any of its neighbors in $O(1)$ time. So our algorithm is now as follows.

  1. Do one DFS to find the inverse product rooted at one node of the tree.
  2. Do another DFS to find the inverse product at every other node of the tree.

The rest of the solution is the same as subtask 2.

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;
int main() {
    cin.tie(0)->sync_with_stdio(0);
    int t;
    cin >> t;
    while (t--) {
        int n;
        cin >> n;
        vector<vector<int>> adj(n);
        for (int i = 0; i < n - 1; i++) {
            int u, v;
            cin >> u >> v, u--, v--;
            adj[u].push_back(v);
            adj[v].push_back(u);
        }
        vector<int> inv(n + 1);
        inv[1] = 1;
        for (int i = 2; i <= n; i++)
            inv[i] = (ll)inv[MOD % i] * (MOD - MOD / i) % MOD;
        vector<int> sz(n), dp(n);
        auto dfs = [&](auto dfs, int p, int i) -> void {
            sz[i] = 1;
            dp[i] = 1;
            for (int j : adj[i])
                if (p != j) {
                    dfs(dfs, i, j);
                    sz[i] += sz[j];
                    dp[i] = (ll)dp[i] * dp[j] % MOD;
                }
            dp[i] = (ll)dp[i] * inv[sz[i]] % MOD;
        };
        dfs(dfs, -1, 0);  // dp[0] = inverse product for root
        auto make = [&](auto make, int p, int i) -> void {
            for (int j : adj[i])
                if (p != j) {
                    dp[j] = (ll)dp[i] * sz[j] % MOD * inv[n - sz[j]] % MOD;
                    make(make, i, j);
                }
        };
        make(make, -1, 0);  // get inverse product (dp) for all other verts
        ll ways = 0;
        for (int i = 0; i < n; i++) ways += dp[i];
        ways %= MOD;
        for (int i = 1; i < n; i++)
            ways = ways * inv[i] % MOD;  // divide by (n-1)!
        cout << ways << '\n';
    }
}