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 Rust (1.15.1) (judge verdict: Accepted)

use std::io::*;
use std::str::FromStr;

fn read<T: FromStr>() -> T {
    let stdin = stdin();
    let stdin = stdin.lock();
    let token: String = stdin
        .bytes()
        .map(|c| c.expect("failed to read char") as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect();
    token.parse().ok().expect("failed to parse token")
}

const M: i64 = 1_000_000_007;

fn modinv(mut x: i64, m: i64) -> i64 {
    let mut y = m;
    let mut u = 1;
    let mut v = 0;

    while y != 0 {
        let t = x / y;
        x -= t * y;
        u -= t * v;

        std::mem::swap(&mut x, &mut y);
        std::mem::swap(&mut u, &mut v);
    }

    (u + m) % m
}

#[derive(Debug, Clone)]
struct Edge {
    n: usize,
    s: i64,
    v: i64,
}

#[derive(Debug, Clone)]
struct Node {
    e: Vec<Edge>,
    v: i64,
}

fn dfs1(nodes: &mut [Node], idx: usize, pidx: usize, facts: &[i64], finvs: &[i64]) -> (i64, i64) {
    let mut size = 1;
    let mut value = 1;

    for i in 0..nodes[idx].e.len() {
        if nodes[idx].e[i].n != pidx {
            let n = nodes[idx].e[i].n;
            let (s, v) = dfs1(nodes, n, idx, facts, finvs);
            nodes[idx].e[i].s = s;
            nodes[idx].e[i].v = v;
            size += s;
            value = (value * v) % M;
            value = (value * finvs[s as usize]) % M;
        }
    }

    value = (value * facts[(size - 1) as usize]) % M;

    (size, value)
}

fn dfs2(
    nodes: &mut [Node],
    idx: usize,
    pidx: usize,
    size: i64,
    value: i64,
    facts: &[i64],
    finvs: &[i64],
) {
    let mut s = 0;
    let mut v = 1;

    for i in 0..nodes[idx].e.len() {
        if nodes[idx].e[i].n == pidx {
            nodes[idx].e[i].v = value;
            nodes[idx].e[i].s = size;
        }

        s += nodes[idx].e[i].s;
        v = (v * finvs[nodes[idx].e[i].s as usize]) % M;
        v = (v * nodes[idx].e[i].v) % M;
    }

    nodes[idx].v = (facts[s as usize] * v) % M;

    for i in 0..nodes[idx].e.len() {
        if nodes[idx].e[i].n != pidx {
            let ns = s - nodes[idx].e[i].s + 1;
            let mut nv = (v * modinv(nodes[idx].e[i].v, M)) % M;

            nv = (nv * facts[(ns - 1) as usize]) % M;
            nv = (nv * facts[nodes[idx].e[i].s as usize]) % M;

            let n = nodes[idx].e[i].n;
            dfs2(nodes, n, idx, ns, nv, facts, finvs);
        }
    }
}

fn main() {
    let in_n: usize = read();
    let mut nodes = vec![Node { e: vec![], v: 1 }; in_n];
    let mut facts = vec![1; in_n];
    let mut finvs = vec![1; in_n];

    for _ in 0..in_n - 1 {
        let mut a: usize = read();
        let mut b: usize = read();

        a -= 1;
        b -= 1;

        nodes[a].e.push(Edge { n: b, s: 0, v: 1 });
        nodes[b].e.push(Edge { n: a, s: 0, v: 1 });
    }

    for i in 2..in_n {
        facts[i] = (facts[i - 1] * i as i64) % M;
        finvs[i] = modinv(facts[i], M);
    }

    dfs1(&mut nodes, 0, 0, &facts, &finvs);
    dfs2(&mut nodes, 0, 0, 0, 0, &facts, &finvs);

    for n in &nodes {
        println!("{}", n.v);
    }
}