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: Time Limit Exceeded)

#include <iostream>
#include <bits/stdc++.h>
#include <cmath>
#include <vector>
#define ll long long int
#define mp make_pair
#define pb push_back
#define vi vector<int>
using namespace std;
vector<vector<int>> edge;
vector<pair<ll,int>> dp;
const ll p =1e9 + 7;
ll modpower(ll base, ll power, ll mod=p){
    ll ans =1;
    base%=p;
    while(power){
        if(power&1){
            ans*=base;
            ans%=p;
        }
        base*=base;
        base%=p;
        power>>=1;
    }
    return ans;
}
ll fact[200007];
ll invfact[200007];
void computefactorial(){
    fact[0]=1;
    for(int i=1;i<200007;i++){
        fact[i]=i*fact[i-1];
        fact[i]%=p;
    }
    invfact[200006]=modpower(fact[200006],p-2);
    for(int i=200005;i>=0;i--){
        invfact[i]=(i+1)*invfact[i+1];
        invfact[i]%=p;
    }
}
vector<int> euler;
vector<bool> vis;
void eulerTree(int u, int &indx) 
{ 
    vis[u] = 1; 
    euler[indx++] = u; 
    for (auto it : edge[u]) { 
        if (!vis[it]) { 
            eulerTree(it, indx); 
            euler[indx++] = u; 
        } 
    } 
}
void solve(int u, int v){
    if(dp[u].first!=-1){
        return;
    }
    if(u!=v && edge[u].size()==1){
        dp[u]=mp(1,1);
        return;
    }
    ll ans=1;
    int size=0;
    for(int i=0;i<edge[u].size();i++){
        if(edge[u][i]==v){
            continue;
        }
        solve(edge[u][i],u);
        ans*=dp[edge[u][i]].first;
        ans%=p;
        ans*=invfact[dp[edge[u][i]].second];
        ans%=p;
        size+=dp[edge[u][i]].second;
    }
    ans*=fact[size];
    ans%=p;
    dp[u]=mp(ans,size+1);
    return;
}
ll solve(){
    int n;
    cin>>n;
    edge.resize(n);
    for(int i=0;i<n-1;i++){
        int u,v;
        cin>>u>>v;
        edge[--u].pb(--v);
        edge[v].pb(u);
    }
    dp.resize(n,mp(-1,0));
    vector<ll> ans(n);
    solve(0,0);
    ans[0]=dp[0].first;
    int idx=0;
    euler.resize(2*n + 7);
    vis.resize(n + 5,0);
    eulerTree(0,idx);
    for(int i=1;i<idx;i++){
        dp[euler[i]]=mp(-1,0);
        dp[euler[i-1]]=mp(-1,0);
        solve(euler[i],euler[i]);
        ans[euler[i]]=dp[euler[i]].first;
    }
    for(int i=0;i<n;i++){
        cout<<ans[i]<<"\n";
    }
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    computefactorial();
    solve();
}