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: Wrong Answer)

#![allow(dead_code)]
use std::io::{stdin, Read};
const MOD: usize = 1000000007;

fn gcd(a: usize, b: usize) -> usize {
    match b {
        0 => a,
        _ => gcd(b, a % b),
    }
}

fn powmod(base: usize, exp: usize, m: usize) -> usize {
    let b: Vec<bool> = format!("{:b}", exp)
        .chars()
        .map(|x| x.to_digit(10).unwrap() != 0)
        .collect();
    let mut v = 1;
    for bi in b {
        v *= v;
        v %= m;
        if bi {
            v *= base;
            v %= m
        }
    }
    return v % m;
}

fn invmod(x: usize, m: usize) -> usize {
    powmod(x, m - 2, m)
}

fn prime_factors(x: usize) -> Vec<usize> {
    let mut res = vec![];
    let mut y = x;
    let mut p = 2;
    let upper = (y as f64).sqrt() as usize;
    while y > 1 && p <= upper {
        match y % p {
            0 => {
                res.push(p);
                y /= p;
            }
            _ => {
                p += 1;
            }
        }
    }
    if y > 1 {
        res.push(y)
    }
    return res;
}

fn get_factorials(n: usize, m: usize) -> Vec<usize> {
    let mut res = vec![0; n as usize];
    res[0] = 1;
    for i in 1..(n as usize) {
        res[i] = (res[i - 1] * (i as usize)) % m;
    }
    res
}

fn get_facinvs(n: usize, m: usize, facts: &Vec<usize>) -> Vec<usize> {
    let mut res = vec![0; n];
    res[n - 1] = invmod(facts[n - 1], m);
    for i in (0..(n - 1)).rev() {
        res[i] = (res[i + 1] * (i + 1)) % m;
    }
    res
}

fn combmod(a: usize, b: usize, m: usize, facts: &Vec<usize>, invs: &Vec<usize>) -> usize {
    if a < b {
        0
    } else {
        (((facts[a as usize] * invs[b as usize]) % m) * invs[(a - b) as usize]) % m
    }
}

macro_rules! parse {
    ($it: ident) => {
        $it.next().unwrap().parse().unwrap()
    };
    ($it: ident,2) => {
        (parse!($it), parse!($it))
    };
    ($it: ident,3) => {
        (parse!($it), parse!($it), parse!($it))
    };
    ($it: ident,4) => {
        (parse!($it), parse!($it), parse!($it), parse!($it))
    };
    ($it: ident, $n: expr) => {
        (&mut $it).take($n).map(|e| e.parse().unwrap()).collect();
    };
}

fn solve(s: &str) {
    let mut it = s.split_whitespace();
    let n: usize = parse!(it);
    let mut g = vec![vec![]; n];
    for _ in 0..(n - 1) {
        let (a, b): (usize, usize) = parse!(it, 2);
        g[a - 1].push(b - 1);
        g[b - 1].push(a - 1);
    }
    let facts = get_factorials(200010, MOD);
    let invs = get_facinvs(200010, MOD, &facts);

    let mut memoc = std::collections::HashMap::<(usize, usize), usize>::new();
    let mut memod = std::collections::HashMap::<(usize, usize), usize>::new();
    let mut q = std::collections::VecDeque::new();
    let mut seen = vec![false; n];
    seen[0] = true;
    q.push_back(0);
    let mut parents = vec![None; n];
    let mut topo = vec![];
    while let Some(u) = q.pop_front() {
        topo.push(u);
        for &v in &g[u] {
            if !seen[v] {
                getc(u, v, &g, &mut memoc, &mut memod, &facts, &invs);
                seen[v] = true;
                q.push_back(v);
                parents[v] = Some(u);
            }
        }
    }

    let mut ret = vec![0; n];
    for v in topo {
        if let Some(p) = parents[v] {
            ret[v] = ret[p] * combmod(n - 1, n - memod[&(p, v)], MOD, &facts, &invs)
                / combmod(n - 1, memod[&(p, v)], MOD, &facts, &invs)
        } else {
            let pairs: Vec<(usize, usize)> = g[v].iter().map(|&x| (v, x)).collect();
            ret[v] = cum(&pairs, &g, &mut memoc, &mut memod, &facts, &invs);
        }
    }
    for i in 0..n {
        println!("{}", ret[i]);
    }
}

fn cum(
    pairs: &[(usize, usize)],
    g: &Vec<Vec<usize>>,
    memoc: &mut std::collections::HashMap<(usize, usize), usize>,
    memod: &mut std::collections::HashMap<(usize, usize), usize>,
    facts: &Vec<usize>,
    invs: &Vec<usize>,
) -> usize {
    let mut ret = 1;
    let mut nodes = vec![];
    for &(i, j) in pairs {
        nodes.push(getd(i, j, g, memod));
        ret *= getc(i, j, g, memoc, memod, facts, invs);
        ret %= MOD;
    }
    let mut s = nodes.iter().sum();
    for v in nodes {
        ret *= combmod(s, v, MOD, facts, invs);
        ret %= MOD;
        s -= v;
    }
    ret
}

fn getc(
    i: usize,
    j: usize,
    g: &Vec<Vec<usize>>,
    memoc: &mut std::collections::HashMap<(usize, usize), usize>,
    memod: &mut std::collections::HashMap<(usize, usize), usize>,
    facts: &Vec<usize>,
    invs: &Vec<usize>,
) -> usize {
    if let Some(c) = memoc.get(&(i, j)) {
        return *c;
    }
    let mut pairs = vec![];
    for &u in &g[j] {
        if i != u {
            pairs.push((j, u));
        }
    }
    let ret = cum(&pairs, g, memoc, memod, facts, invs);
    memoc.insert((i, j), ret);
    ret
}

fn getd(
    i: usize,
    j: usize,
    g: &Vec<Vec<usize>>,
    memod: &mut std::collections::HashMap<(usize, usize), usize>,
) -> usize {
    if let Some(c) = memod.get(&(i, j)) {
        return *c;
    }
    let mut ret = 1;
    for &u in &g[j] {
        if u != i {
            ret += getd(j, u, g, memod);
        }
    }
    memod.insert((i, j), ret);
    ret
}

fn main() {
    let mut s = String::new();
    stdin().read_to_string(&mut s).unwrap();
    solve(&s);
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn test_input() {
        let s = "
        3
1 2
1 3
        ";
        solve(s);
    }
}