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 Java8 (OpenJDK 1.8.0) (judge verdict: Accepted)

import java.io.*;
import java.util.*;

public class Main {
    static PrintWriter out = new PrintWriter(System.out);
    static Scanner sc = new Scanner(System.in);
    static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    int[] size; // size of subtree, including self
    List<Integer>[] graph;
    long[] dp,ans;
    final long mod = 1000000000+7;
    long[] frac,inv; // inv[i] = the inversion of i! mod M
    public static void main(String[] args) throws IOException {
        Main main = new Main();
        main.solve();
        out.flush();
//        System.out.println(main.solve());
    }
    long solve() throws IOException{
        int n = sc.nextInt();
        size = new int[n]; dp = new long[n]; ans = new long[n];
        graph = new List[n];
        for(int i=0;i<n;i++) graph[i] = new ArrayList<>();
        ini(n+1);
        for(int i=0;i<n-1;i++){
            int a = sc.nextInt()-1, b = sc.nextInt()-1;
            graph[a].add(b); graph[b].add(a);
        }
        dfsSubTree(0,-1);
        ans[0] = dp[0];
        // then tree transform
        for(int to:graph[0]){
            dfsAns(to,0,n);
        }
        for(int i=0;i<n;i++) out.println(ans[i]);
        return 0;
    }
    void dfsSubTree(int root, int from){
        size[root] = 1;
        long base = 1;
        for(int to:graph[root]){
            if(to==from) continue;
            dfsSubTree(to,root);
            size[root] += size[to];
            base = base*dp[to]%mod*inv[size[to]]%mod;
        }
        base = base*frac[size[root]-1]%mod;
        dp[root] = base;
    }
    void dfsAns(int root, int from, int n){
        // to find the final ans of each root. a little bit tricky
        long base = ans[from];
        base = base*inv[n-1]%mod*frac[size[root]]%mod*power(dp[root],mod-2)%mod;
        base = base*frac[n-size[root]-1]%mod;
        base = base*inv[n-size[root]]%mod;
//        base = base*inv[size[root]-1]%mod*dp[root]%mod;
        for(int to:graph[root]){
            if(to==from) continue;
            base = base*dp[to]%mod*inv[size[to]]%mod;
        }
        base = base*frac[n-1]%mod;
        ans[root] = base;
        for(int to:graph[root]){
            if(to==from) continue;
            dfsAns(to,root,n);
        }
    }
    void ini(int n){
        frac = new long[n+1];
        inv = new long[n+1];
        frac[0] = 1; inv[0] = 1; inv[1] = 1;
        for(int i=1;i<=n;i++) frac[i] = i*frac[i-1]%mod;
        inv[n] = power(frac[n],mod-2);
        for(int i=n-1;i>1;i--){
            inv[i] = (i+1)*inv[i+1]%mod;
        }
    }
    long power(long base, long p){
        long ans = 1;
        while(p>0){
            if(p%2==1) ans = ans*base%mod;
            base = base*base%mod;
            p /= 2;
        }
        return ans;
    }
}