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 Go (1.6) (judge verdict: Accepted)

package main

import (
  "bufio"
  "fmt"
  "os"
  "strconv"
  "strings"
)

var n int
var size []int
var g [][]int
var ans []int64
var m ModUtil

func main() {
  io := IO{bufio.NewReaderSize(os.Stdin,100),bufio.NewWriterSize(os.Stdout,100)}
  n = io.NextInt()
  size = make([]int,n)
  g = make([][]int,n)
  ans = make([]int64,n)
  m = ModUtil{mod:1000000007}.Init(n)
  for i:=0;i<n-1;i++ {
    a,b := io.NextInts()
    g[a-1] = append(g[a-1],b-1)
    g[b-1] = append(g[b-1],a-1)
  }
  dfs1(0,-1)
  ans[0] = 1
  for i:=0;i<n;i++ { ans[0] = (ans[0]*int64(size[i]))%m.mod }
  dfs2(0,-1)
  for i:=0;i<n;i++ { ans[i] = (m.f[n]*m.Inv(ans[i]))%m.mod }
  for i:=0;i<n;i++ { io.Println((ans[i]+m.mod)%m.mod) }
  io.Flush()
}

func dfs1(v,p int) {
  size[v] = 1
  for _,u := range g[v] {
    if u == p { continue }
    dfs1(u,v)
    size[v] += size[u]
  }
}
func dfs2(v,p int) {
  if p != -1 { ans[v] = ((ans[p]*int64(n-size[v])%m.mod)*m.Inv(int64(size[v])))%m.mod }
  for _,u := range g[v] {
    if u == p { continue }
    dfs2(u,v)
  }
}

type ModUtil struct {
  mod int64
  f,invf []int64
}
func(m ModUtil) Init(n int) ModUtil {
  m.f = make([]int64,n+1)
  m.invf = make([]int64,n+1)
  m.f[0] = 1
  for i:=1;i<n+1;i++ { m.f[i] = (int64(i)*m.f[i-1])%m.mod }
  m.invf[n] = m.Inv(m.f[n])
  for i:=n;i>0;i-- { m.invf[i-1] = (int64(i)*m.invf[i])%m.mod }
  return m
}
func(m ModUtil) Inv(n int64) int64 {
  var a,b,c,x int64 = n%m.mod,m.mod,0,1
  for b != 0 { a,b,c,x = b,a%b,x-(a/b)*c,c }
  return (x%m.mod+m.mod)%m.mod
}
func(m ModUtil) Perm(n,k int) int64 {
  if n < 0 || k < 0 || n-k < 0 { return 0 }
  return (m.f[n]*m.invf[n-k])%m.mod
}
func(m ModUtil) Comb(n,k int) int64 {
  if n < 0 || k < 0 || n-k < 0 { return 0 }
  return (m.Perm(n,k)*m.invf[k])%m.mod
}
func(m ModUtil) Homo(n,k int) int64 {
  if n == 0 && k == 0 { return 1 }
  return m.Comb(n+k-1,k)
}

type IO struct {
  reader *bufio.Reader
  writer *bufio.Writer
}
func(io *IO) NextLine() string {
  var buffer []byte
  for {
    line,isPrefix,err := io.reader.ReadLine()
    if err != nil { panic(err) }
    buffer = append(buffer,line...)
    if !isPrefix { break }
  }
  return string(buffer)
}
func(io *IO) Println(a ...interface{}) (int,error) {
  return fmt.Fprintln(io.writer,a...)
}
func(io *IO) Flush() error {
  return io.writer.Flush()
}
func(io *IO) NextInt() int {
  n,_ := strconv.Atoi(io.NextLine())
  return n
}
func(io *IO) NextInts() (int,int) {
  s := strings.Fields(io.NextLine())
  x,_ := strconv.Atoi(s[0])
  y,_ := strconv.Atoi(s[1])
  return x,y
}