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 <bits/stdc++.h>
typedef long long ll;
using namespace std;
vector<int> cntd;
vector<int> sz;
vector<int> cntu;
vector<int> par;
vector<vector<int>> adj;
vector<int> fact;
vector<int> inv;
vector<int> ans;
const int MOD = 1e9 + 7;

void mul(int& a, int b){
  a = 1ll * a * b % MOD;
}

void add(int& a, int b){
  a += b;
  if(a >= MOD)
    a -= MOD;
}

ll pow1(ll n, ll p){
  ll r = 1, c = n;
  while(p){
    if(p & 1)
      (r *= c) %= MOD;
    p /= 2;
    (c *= c) %= MOD;
  }
  return r;
}

void dfs_d(int u, int p = -1){
  sz[u] = 1;
  cntd[u] = 1;
  par[u] = p;
  for(auto el : adj[u]){
    if(el == p)continue;
    dfs_d(el, u);
    sz[u] += sz[el];
    mul(cntd[u], cntd[el]);
    mul(cntd[u], inv[sz[el]]);
  }
  mul(cntd[u], fact[sz[u] - 1]);
}
  int n;

void dfs_u(int u, int p = -1){
  if(p == -1)
    cntu[u] = 1;
  else{
    cntu[u] = 1;
    int szp = n - sz[u];
    int szpp = szp - 1;
    for(auto el : adj[p]){
      if(el == u || el == par[p])continue;
      mul(cntu[u], cntd[el]);
      mul(cntu[u], inv[sz[el]]);
      szpp -= sz[el];
    }
    mul(cntu[u], cntu[p]);
    mul(cntu[u], inv[szpp]);
    mul(cntu[u], fact[szp - 1]);
  }
  for(auto el : adj[u]){
    if(el == p)continue;
    dfs_u(el, u);
  }
}


int main()
{
//  freopen("in.txt", "r", stdin);
//  freopen("out.txt", "w", stdout);
  cin >> n;
  cntd.resize(n);
  sz.resize(n);
  cntu.resize(n);
  par.resize(n);
  adj.resize(n);
  fact.resize(n + 1);
  inv.resize(n + 1);
  ans.resize(n);
  fact[0] = inv[0] = 1;
  for(int i = 1;i <= n;i++){
    fact[i] = 1ll * fact[i - 1] * i % MOD;
    inv[i] = pow1(fact[i], MOD - 2);
  }
//  assert(1ll * fact[3] * inv[3] % MOD == 1);

  for(int i = 0;i < n - 1;i++){
    int u, v;
    scanf("%d %d", &u, &v);
    u--, v--;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  dfs_d(0);
  dfs_u(0);
//  for(int i = 0;i < n;i++)
//    cout << cntu[i] << endl;
  for(int i = 0;i < n;i++){
    int szp = n - sz[i];
    int res = cntu[i];
    mul(res, inv[szp]);
    for(auto el : adj[i]){
      if(el == par[i])continue;
      mul(res, cntd[el]);
      mul(res, inv[sz[el]]);
    }
    mul(res, fact[n - 1]);
    cout << res << endl;
  }
  return 0;
}