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>
using namespace std;

using LL = long long;
const LL M = 1e9 + 7;

LL combination(LL n, LL r) {
  if(n < r) return 0;
  if(n < 0 || r < 0) return 0;
  static std::vector<LL> fact{1, 1}, inv{1, 1}, finv{1, 1};
  for(LL i = fact.size(); i <= n; ++i) {
    fact.emplace_back(fact.back() * i % M);
    inv.emplace_back(inv.at(M % i) * (M - M / i) % M);
    finv.emplace_back(finv.back() * inv.back() % M);
  }
  return (fact[n] * finv[n - r] % M) * finv[r] % M;
}

int main() {
  int N;
  cin >> N;
  vector<vector<int>> G(N+1);
  for(auto i=1; i<N; ++i) {
    int a, b;
    cin >> a >> b;
    G[a].emplace_back(b);
    G[b].emplace_back(a);
  }

  for(auto i=1; i<=N; ++i) G[0].emplace_back(i);

  vector<vector<LL>> dp(N+1), size(N+1);
  for(auto i=0; i<=N; ++i) dp[i].assign(G[i].size(), 0);
  for(auto i=0; i<=N; ++i) size[i].assign(G[i].size(), 0);

  function<LL(int,int)> dfs = [&](auto pre, auto e) {
    if(dp[pre][e]) return dp[pre][e];
    dp[pre][e] = 1;
    auto cur = G[pre][e];
    for(auto i=0; i<G[cur].size(); ++i) {
      auto nex = G[cur][i];
      if(nex == pre) continue;
      dfs(cur, i);
      (dp[pre][e] *= dp[cur][i]) %= M;
      (dp[pre][e] *= combination(size[pre][e] + size[cur][i], size[cur][i])) %= M;
      size[pre][e] += size[cur][i];
    }
    ++size[pre][e];
    return dp[pre][e];
  };

  for(auto i=0; i<N; ++i) cout << dfs(0, i) << endl;
}