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<vector>
#include<functional>
#include<iostream>
#include<string>

template <typename T>
struct ReRooting{
private:
    using F = std::function<T(T, int)>;
    using F2 = std::function<T(T, T)>;
    int n;
    std::vector<std::vector<int>> G;
    std::vector<std::vector<T>> dp;
    F f, g;
    F2 merge;
    T ide;

    T dfs1(int p, int k){
        T res = ide;
        int cnt = -1;
        for(int i = 0; i < G[k].size(); i++){
            if(G[k][i] == p) continue;
            dp[k][i] = dfs1(k, G[k][i]);
            res = merge(res, f(dp[k][i], G[k][i]));
        }
        return g(res, k);
    }

    void dfs2(int p, int k, T add){
        for(int i = 0; i < G[k].size(); i++){
            if(G[k][i] == p){
                dp[k][i] = add;
                break;
            }
        }
        std::vector<T> pL(G[k].size() + 1), pR(G[k].size() + 1);

        pL[0] = ide;
        for(int i = 0; i < G[k].size(); i++)
            pL[i + 1] = merge(pL[i], f(dp[k][i], G[k][i]));

        pR[G[k].size()] = ide;
        for(int i = G[k].size() - 1; i >= 0; i--)
            pR[i] = merge(pR[i + 1], f(dp[k][i], G[k][i]));

        for(int i = 0; i < G[k].size(); i++){
            if(G[k][i] == p) continue;
            T val = merge(pL[i], pR[i + 1]);
            dfs2(k, G[k][i], g(val, k));
        }
    }

    void build(int root){
        for(int i = 0; i < n; i++) 
            dp[i].resize(G[i].size());
        dfs1(-1, root);
        dfs2(-1, root, ide);
    }

public:
    ReRooting(std::vector<std::vector<int>> G_, F f_, F2 merge_, T ide_, F g_ = [](T a, int b){return a;})
    : G(G_), f(f_), merge(merge_), ide(ide_), g(g_), n(G_.size()), dp(G_.size()) {}

    std::vector<T> solve(int root = 0){
        build(root);
        std::vector<T> vec(n);
        for(int k = 0; k < n; k++){
            T res = ide;
            for(int i = 0; i < G[k].size(); i++) res = merge(res, f(dp[k][i], G[k][i]));
            vec[k] = g(res, k);
        }
        return vec;
    }
};

//ModInt
template <const int &mod>
struct ModInt {
private:
    long long x;

    long long ext_gcd(long long a, long long b, long long &x, long long &y){
        if(b == 0){
            x = 1, y = 0;
            return a;
        }
        long long q = a / b;
        long long g = ext_gcd(b, a - q * b, x, y);
        long long z = x - q * y;
        x = y, y = z;
        return g;
    }

public:    
    ModInt() : x(0) {}

    ModInt(long long x_) {
        if ((x = x_ % mod + mod) >= mod) x -= mod;
    }

    constexpr ModInt& operator+=(ModInt rhs){
        if ((x += rhs.x) >= mod) x -= mod;
        return *this;
    }

    constexpr ModInt& operator-=(ModInt rhs){
        if ((x -= rhs.x) < 0) x += mod;
        return *this;
    }

    constexpr ModInt& operator*=(ModInt rhs){
        x = (unsigned long long)x * rhs.x % mod;
        return *this;
    }

    constexpr ModInt& operator/=(ModInt rhs){
        x = (unsigned long long)x * rhs.inv().x % mod;
        return *this;
    }
  
    constexpr ModInt operator-() const noexcept{ return -x < 0 ? mod - x : -x; }

    constexpr ModInt operator+(ModInt rhs) const noexcept{ return ModInt(*this) += rhs; }

    constexpr ModInt operator-(ModInt rhs) const noexcept{ return ModInt(*this) -= rhs; }

    constexpr ModInt operator*(ModInt rhs) const noexcept{ return ModInt(*this) *= rhs; }

    constexpr ModInt operator/(ModInt rhs) const noexcept{ return ModInt(*this) /= rhs; }

    constexpr ModInt &operator++ () { *this += 1; return *this;}

    constexpr ModInt operator++ (int) { *this += 1; return *this - 1;}

    constexpr ModInt &operator-- () { *this -= 1; return *this;}

    constexpr ModInt operator-- (int) { *this -= 1; return *this + 1;}

    bool operator==(ModInt rhs) const { return x == rhs.x; }    

    bool operator!=(ModInt rhs) const { return x != rhs.x; }

    bool operator<=(ModInt rhs) const { return x <= rhs.x; }

    bool operator>=(ModInt rhs) const { return x >= rhs.x; }

    bool operator<(ModInt rhs) const { return x < rhs.x; }

    bool operator>(ModInt rhs) const { return x > rhs.x; }

    ModInt inv() {
        long long x, y;
        ext_gcd(this->x, mod, x, y);
        return (ModInt) x;
    }
  
    friend std::ostream& operator<<(std::ostream& s, ModInt<mod> a) {
        s << a.x;
        return s;
    }
    
    friend std::istream& operator>>(std::istream& s, ModInt<mod>& a) {
        s >> a.x;
        return s;
    }
};

//Modulo Calculation
static int MOD = 1e9 + 7;
// static int MOD = 998244353;
using mint = ModInt<MOD>;

const int CombMAX = 1000000;
mint fac[CombMAX + 1],finv[CombMAX + 1],inv[CombMAX + 1];

struct Combinationinit{
    Combinationinit(){
        fac[0] = fac[1] = 1;
        finv[0] = finv[1] = 1;
        inv[1] = 1;
        for(int i = 2; i <= CombMAX; i++){
            fac[i] = fac[i - 1] * (mint)i; 
            inv[i] = (mint)MOD - inv[MOD % i] * (MOD / i);
            finv[i] = finv[i - 1] * inv[i];
        }
    }
}Combinationinit_;

//nCk
mint COM(int n,int k){
    if(n < k or n < 0 or k < 0)return 0;
    return fac[n]* finv[k]* finv[n - k];
}

//nPk
mint PER(int n, int k){
    if(n < k or n < 0 or k < 0) return 0;
    return fac[n] / fac[n - k];
}

//nHk
mint HOM(int n, int k) {return COM(n + k - 1, k);};

int main(){
    int n;
    std::cin >> n;
    std::vector<std::vector<int>> G(n);
    for(int i = 0; i < n - 1; i++){
        int a, b;
        std::cin >> a >> b;
        a--; b--;
        G[a].push_back(b);
        G[b].push_back(a);
    }
    using P = std::pair<mint, int>;
    auto f = [&](P p, int v){return P(p.first * finv[p.second], p.second);};
	auto merge = [&](P p, P q){return P(p.first * q.first, p.second + q.second);};
	auto g = [&](P p, int v){return P(p.first * fac[p.second], p.second + 1);};
	ReRooting<P> ReRoot(G, f, merge, P{1, 0}, g);
	auto res = ReRoot.solve();
	for(int i = 0; i < n; i++)
        std::cout << res[i].first << '\n';
}