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>
#define _ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define int long long
#define F first
#define S second
#define L long double
#define P pair<int, int>
const int inf = 0x3f3f3f3f3f3f3f3LL;
const int mod = (int)1e9 + 7;
using namespace std;
#if LOCAL
#include "../../tr.h"
#else
#define tr(...)
#define endl '\n'
#endif
template<class T> void smin(T& a, T val) {if (a > val) a = val;}template<class T> void smax(T& a, T val) {if (a < val) a = val;}
const int N = 5*(int)1e5 + 10;

int n;
vector<int> v[N];

#define ll int
ll power(ll a, ll b, ll m = mod){if(b < 0)b += m-1; ll r = 1; while(b){if(b&1)r=(r*a)%m; a = (a*a)%m; b>>=1;}return r;}

int fact[N+10], ifact[N+10];
void pre(int N){
   fact[0] = 1;
   for (int i = 1; i <= N; ++i)
   {
      fact[i] = (i*fact[i-1])%mod;
   }
   ifact[N] = power(fact[N], -1);
   for (int i = N-1; i >= 0; --i)
   {
      ifact[i] = ((i+1)*ifact[i+1])%mod;
   }
}

int nCr(int n, int r){
   if(n < r or n < 0 or r < 0)return 0;
   return ((fact[n]*ifact[r])%mod * ifact[n-r])%mod;
}

int star_bars(int a, int b){
   return nCr(a+b, a);
}

int dpU[N], dpD[N], sz[N];
vector<int> prefDP[N];
vector<int> sufffDP[N];

vector<int> prefsz[N];
vector<int> sufffsz[N];

int merge(int dp1, int dp2, int beforeSize, int currSize){
   dp1 %= mod;
   dp2 %= mod;

   beforeSize %= mod;
   currSize %= mod;

   int ans = dp1 * dp2;
   ans %= mod;
   ans *= star_bars(beforeSize, currSize);
   ans %= mod;
   return ans;
}


void dfs(int s, int p){
   if (p != -1){
      v[s].erase(find(v[s].begin(), v[s].end(), p));
   }

   dpD[s] = 1;
   for(auto i: v[s]){
      dfs(i, s);
      dpD[s] = merge(dpD[s], dpD[i], sz[s], sz[i]);
      sz[s] += sz[i];   
      prefDP[s].push_back(dpD[s]);
      prefsz[s].push_back(sz[s]);
   }

   reverse(v[s].begin(), v[s].end());

   dpD[s] = 1;
   sz[s] = 0;
   for(auto i: v[s]){
      dpD[s] = merge(dpD[s], dpD[i], sz[s], sz[i]);
      sz[s] += sz[i];
      sufffDP[s].push_back(dpD[s]);
      sufffsz[s].push_back(sz[s]);
   }

   reverse(sufffDP[s].begin(), sufffDP[s].end());
   reverse(sufffsz[s].begin(), sufffsz[s].end());
   reverse(v[s].begin(), v[s].end());

   tr(s, prefDP[s], prefsz[s], sufffDP[s], sufffsz[s]);
   sz[s]++;
}

int ans[N];

void dfs(int s){
   
   if(s == 3)
      tr("-------------------", v[s]);
   for (int i = 0; i < (int)(v[s].size()); ++i)
   {
      int curr = v[s][i];
      int currsz = sz[1] - sz[s];
      dpU[curr] = dpU[s];

      if (i-1 >= 0){
         if(s == 3){
            tr(curr, prefDP[s][i-1], currsz, prefsz[s][i-1]);
         }
         dpU[curr] = merge(dpU[curr], prefDP[s][i-1], currsz, prefsz[s][i-1]);
         currsz += prefsz[s][i-1];
      }
      if (i + 1 < (int)(v[s].size())){
         if(s == 3){
            tr(curr, sufffDP[s][i+1], currsz, sufffsz[s][i+1]);
         }
         dpU[curr] = merge(dpU[curr], sufffDP[s][i+1], currsz, sufffsz[s][i+1]);
         currsz += sufffsz[s][i+1];
      }

      dfs(curr);
   }

   tr(s, dpD[s], dpU[s]);
   ans[s] = merge(dpD[s], dpU[s], sz[s] - 1, sz[1] - sz[s]);
}

int32_t main(){_
   
   pre(N);   
   cin>>n;

   for (int i = 0; i+1 < n; ++i)
   {
      int x, y;
      cin >> x >> y;
      v[x].push_back(y);
      v[y].push_back(x);
   }
   
   dpU[1] = 1;
   dfs(1, -1);
   tr(dpD[1], dpU[1]);
   dfs(1);

   for (int i = 1; i <= n; ++i)
   {
      cout<<ans[i]<<endl;
   }

   return 0;
}