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++14 (GCC 5.4.1) (judge verdict: Accepted)

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

constexpr int64_t MOD = 1e9 + 7;
constexpr int64_t MAX_N = 2e5;

//a^b % modを求める
int64_t MODpow(int64_t a, int64_t b, int64_t mod) {
    int64_t result = 1;
    while (b) {
        if (b % 2 == 1) {
            result *= a;
            result %= mod;
        }

        b /= 2;
        a *= a;
        a %= mod;
    }

    return result;
}

class Combination {
public:
    Combination(int64_t max_num, int64_t mod) {
        fact.resize(max_num + 1, 1);
        inv_fact.resize(max_num + 1, 1);
        mod_ = mod;
        for (int64_t i = 2; i <= max_num; i++) {
            fact[i] = i * fact[i - 1] % mod_;
            inv_fact[i] = MODpow(fact[i], mod_ - 2, mod_);
            assert(fact[i] * inv_fact[i] % mod_ == 1);
        }
    }
    int64_t operator()(int64_t n, int64_t m) const {
        if (m < 0 || m > n) return 0;
        return fact[n] * inv_fact[n - m] % mod_ * inv_fact[m] % mod_;
    }
    vector<int64_t> fact, inv_fact;
private:
    int64_t mod_;
};
Combination comb(MAX_N, MOD);

struct DP_Element {
    int64_t dp, size;
    DP_Element() : dp(1), size(1) {}
    DP_Element operator+=(const DP_Element& rhs) {
        (dp *= rhs.dp) %= MOD;
        (dp *= comb.inv_fact[rhs.size]) %= MOD;
        size += rhs.size;
        return *this;
    }
};

int main() {
    int64_t N;
    cin >> N;
    vector<vector<int64_t>> connect(N);
    for (int64_t i = 0; i < N - 1; i++) {
        int64_t a, b;
        cin >> a >> b;
        a--; b--;
        connect[a].push_back(b);
        connect[b].push_back(a);
    }


    //dp1[i] := 0を根としたときのノードi以下の部分木についての答え
    vector<DP_Element> dp1(N);
    function<DP_Element(int64_t, int64_t)> f1 = [&](int64_t curr, int64_t pre) {
        for (int64_t next_node : connect[curr]) {
            if (next_node == pre) {
                continue;
            }

            DP_Element next_dp = f1(next_node, curr);
            dp1[curr] += next_dp;
        }

        (dp1[curr].dp *= comb.fact[dp1[curr].size - 1]) %= MOD;
        return dp1[curr];
    };

    f1(0, -1);

    //dp2[i] := iを根としたときのiについての答え
    vector<DP_Element> dp2(N);
    dp2[0] = dp1[0];
    function<void(int64_t, int64_t)> f2 = [&](int64_t curr, int64_t pre) {
        for (int64_t next_node : connect[curr]) {
            if (next_node == pre) {
                continue;
            }

            DP_Element add = dp2[curr];
            //dp1[next_node]からの影響を取り除く
            (add.dp *= comb.inv_fact[add.size - 1]) %= MOD;
            (add.dp *= MODpow(dp1[next_node].dp, MOD - 2, MOD)) %= MOD;
            (add.dp *= comb.fact[dp1[next_node].size]) %= MOD;
            add.size -= dp1[next_node].size;
            (add.dp *= comb.fact[add.size - 1]) %= MOD;

            //dp2[next_node]に足す
            DP_Element& target = dp2[next_node];
            target = dp1[next_node];
            (target.dp *= comb.inv_fact[target.size - 1]) %= MOD;
            target += add;
            (target.dp *= comb.fact[target.size - 1]) %= MOD;

            //次へ遷移
            f2(next_node, curr);
        }
    };

    f2(0, -1);

    for (int64_t i = 0; i < N; i++) {
        cout << dp2[i].dp << endl;
    }
}