Problem:
Score : 600 points

Problem Statement

We have a tree with N vertices numbered 1 to N. The i-th edge in this tree connects Vertex a_i and b_i.
For each k=1, ..., N, solve the problem below:

Consider writing a number on each vertex in the tree in the following manner:

First, write 1 on Vertex k.

Then, for each of the numbers 2, ..., N in this order, write the number on the vertex chosen as follows:

Choose a vertex that still does not have a number written on it and is adjacent to a vertex with a number already written on it. If there are multiple such vertices, choose one of them at random.

Find the number of ways in which we can write the numbers on the vertices, modulo (10^9+7).

Constraints

2 \leq N \leq 2 \times 10^5

1 \leq a_i,b_i \leq N

The given graph is a tree.

Input

Input is given from Standard Input in the following format:

N
a_1 b_1
:
a_{N-1} b_{N-1}

Output

For each k=1, 2, ..., N in this order, print a line containing the answer to the problem.

Sample Input 1

3
1 2
1 3

Sample Output 1

2
1
1

The graph in this input is as follows:

For k=1, there are two ways in which we can write the numbers on the vertices, as follows:

Writing 1, 2, 3 on Vertex 1, 2, 3, respectively

Writing 1, 3, 2 on Vertex 1, 2, 3, respectively

Sample Input 2

2
1 2

Sample Output 2

1
1

The graph in this input is as follows:

Sample Input 3

5
1 2
2 3
3 4
3 5

Sample Output 3

2
8
12
3
3

The graph in this input is as follows:

Sample Input 4

8
1 2
2 3
3 4
3 5
3 6
6 7
6 8

Sample Output 4

40
280
840
120
120
504
72
72

The graph in this input is as follows:

Solution in C++ (GCC 9.2.1) (judge verdict: Accepted)

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int MOD = 1e9 + 7;
const int N = 2e5 + 5;

vector<int> G[N];

int n;

ll fac[N], invf[N];

void init() {
    fac[0] = 1;
    for (int i = 1; i < N; i++)
        fac[i] = fac[i - 1] * i % MOD;
    invf[0] = 1, invf[1] = 1;
    for (int i = 2; i < N; i++)
        invf[i] = (MOD - MOD / i) * invf[MOD % i] % MOD;
    for (int i = 2; i < N; i++)
        invf[i] = invf[i] * invf[i - 1] % MOD;
}

inline ll C(int n, int m) {
    return fac[n] * invf[n - m] % MOD * invf[m] % MOD;
}

ll f[N];

int sz[N];

void dfs(int u, int p = 0) {
    sz[u] = 1; f[u] = 1;
    for (auto v : G[u]) if (v != p) {
        dfs(v, u);
        sz[u] += sz[v];
    }    
    int cnt = 1;
    for (auto v : G[u]) if (v != p) {
        (f[u] *= C(sz[u] - cnt, sz[v]) * f[v] % MOD) %= MOD;
        cnt += sz[v];
    }
}

ll res[N];

ll qpow(ll a, ll b) {
    ll ret = 1;
    for (; b; (a *= a) %= MOD, b >>= 1)
        if (b & 1)
            (ret *= a) %= MOD;
    return ret;
}

inline ll inv(ll x) {
    return qpow(x, MOD - 2);
}

void solve(int u, int p = 0, ll val_p = 1) {
    res[u] = f[u] * val_p % MOD;
    if (u != 1) {
        (res[u] *= C(n - 1, n - sz[u])) %= MOD;
    }
    ll prod = 1, prod2 = 1;
    for (auto v : G[u]) if (v != p) {
        (prod *= fac[sz[v]]) %= MOD;
        (prod2 *= f[v]) %= MOD;
    }
    if (u > 1) {
        (prod *= fac[n - sz[u]]) %= MOD;
        (prod2 *= val_p) %= MOD;
    }
    for (auto v : G[u]) if (v != p) {
        ll cur = prod * inv(fac[sz[v]]) % MOD;
        ll cur2 = prod2 * inv(f[v]) % MOD;
        cur = inv(cur);
        ll val = cur2 * fac[n - sz[v] - 1] % MOD * cur % MOD;
        solve(v, u, val);
    }
}

int main() {
    ios::sync_with_stdio(false); cin.tie(0);
    cin >> n;
    for (int i = 1; i < n; i++) {
        int u, v;
        cin >> u >> v;
        G[u].push_back(v);
        G[v].push_back(u);
    }
    init();
    dfs(1);
    solve(1);
    for (int i = 1; i <= n; i++)
        cout << res[i] << endl;
    return 0;
}