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 Kotlin (1.0.0) (judge verdict: Time Limit Exceeded)

import java.util.*

fun main(args:Array<String>) {

    val mod = 1000000007L
    val n = readLine()!!.toInt()
    val dp = LongArray(n)
    val factorial = LongArray(n+1)
    val edges = Array(n){HashSet<Int>()}
    val s = Array(n){0}
    val visited = BooleanArray(n)

    factorial[0] = 1
    factorial[1] = 1

    for(i in 2 ..n) {
        factorial[i] = (factorial[i-1] * i) % mod
    }

    for(i in 0 until n - 1) {
        var (a,b) = readLine()!!.split(" ").map { it.toInt() }
        a--
        b--
        edges[a].add(b)
        edges[b].add(a)
    }

    fun dfs(cur:Int): Int {
        var cnt = 0
        visited[cur] = true
        for(adj in edges[cur]) {
            if(visited[adj]) continue
            cnt += dfs(adj)
        }
        visited[cur] = false
        s[cur] = cnt + 1
        dp[cur] = (factorial[s[cur] - 1])
        for(adj in edges[cur]) {
            if(visited[adj]) {
                continue
            }
            dp[cur] = (dp[cur] * dp[adj] / factorial[s[adj]]) % mod
            dp[cur] %= mod
        }
        while (dp[cur]==0L) {
            
        }
        
        //dp[i] = (s[cur] - 1)! * dp[
        return s[cur]
    }

    fun dfs2(root:Int) {
        visited[root] = true
        for(child in edges[root]) {
            if(visited[child]) continue
            //dp[child] = dp[cur] * s[child] / factorial[ n - s[child] ]
//            dp[child] = dp[root] * s[child] / factorial [ n - s[child]]
            val v = (n - s[child]).toLong()
            dp[child] = (dp[root] * s[child]) % mod
            
            while (dp[child] % v != 0L) {
                
            }

            if(dp[child] / v == 0L) {
                dp[child] = (dp[child] + mod) / v
                if(dp[child]==0L) {
                    while (true){
                    }
                }
            } else {
                dp[child] = dp[child] / v
            }
            dp[child] %= mod
            dfs2(child)
        }
        visited[root] = false
    }
    dfs(0)
    dfs2(0)

    for(i in 0 until n) {
        println(dp[i])
    }
}