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)

#[allow(unused_imports)]
use std::char::from_digit;
#[allow(unused_imports)]
use std::cmp::{max, min};
#[allow(unused_imports)]
use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, VecDeque};
#[allow(unused_imports)]
use std::fs::File;
#[allow(unused_imports)]
use std::io::prelude::*;
#[allow(unused_imports)]
use std::io::{stdin, stdout, BufReader, BufWriter, Stdin, Stdout, Write};
#[allow(unused_imports)]
use std::mem::swap;
#[allow(unused_imports)]
use std::str::FromStr;

type BoxResult<T> = Result<T, Box<std::error::Error>>;

const MOD: u64 = 1_000_000_000 + 7;

fn main() {
    let mut scanner = Scanner::new(stdin());
    let n = scanner.read::<usize>().unwrap();
    let mut adj = AdjacencyList::new(n);
    for _ in 0..(n - 1) {
        let (a, b) = scanner.read_two::<usize, usize>().unwrap();
        adj.add(a - 1, b - 1);
        adj.add(b - 1, a - 1);
    }
    let ans = solve(n, &adj);
    let mut writer = BufWriter::new(stdout());
    for i in 0..n {
        writeln!(&mut writer, "{}", ans[i]).unwrap();
    }
}

fn solve(n: usize, adj: &AdjacencyList) -> Vec<u64> {
    let (dp, sizes) = forward(n, adj);
    let values = backward(&dp, &sizes, n, adj);
    values
}

fn forward(n: usize, adj: &AdjacencyList) -> (Vec<u64>, Vec<usize>) {
    let mut dp = vec![0; n];
    let mut sizes = vec![0; n];
    let factors = (0..(n as u64))
        .scan(1, |state, val| {
            if val > 0 {
                *state = (*state * val) % MOD;
            }
            Some(*state)
        })
        .collect::<Vec<_>>();
    let mut stack = vec![(0, std::usize::MAX, false)];
    while stack.len() > 0 {
        let (crt, prv, visited) = stack.pop().unwrap();
        if visited {
            let mut val = 1;
            let mut size = 1;
            for &ch in adj[crt].iter() {
                if ch == prv {
                    continue;
                }
                size += sizes[ch];
                val = (val * dp[ch]) % MOD;
                val = (val * power(factors[sizes[ch]], MOD - 2, MOD)) % MOD;
            }
            val = (val * factors[size - 1]) % MOD;
            dp[crt] = val;
            sizes[crt] = size;
            continue;
        }
        stack.push((crt, prv, true));
        for &nxt in adj[crt].iter() {
            if nxt == prv {
                continue;
            }
            stack.push((nxt, crt, false));
        }
    }
    (dp, sizes)
}

fn backward(
    dp: &[u64],
    sizes: &[usize],
    n: usize,
    adj: &AdjacencyList,
) -> Vec<u64> {
    let mut values = vec![0; n];
    values[0] = dp[0];
    let mut stack = vec![(0, std::usize::MAX)];
    while stack.len() > 0 {
        let (crt, prv) = stack.pop().unwrap();
        for &nxt in adj[crt].iter() {
            if nxt == prv {
                continue;
            }
            stack.push((nxt, crt));
        }
        if crt == 0 {
            continue;
        }
        let mut val = values[prv];
        val = (val * sizes[crt] as u64) % MOD;
        val = (val * power((n - sizes[crt]) as u64, MOD - 2, MOD)) % MOD;
        values[crt] = val;
    }
    values
}

fn power(x: u64, mut k: u64, modulo: u64) -> u64 {
    let mut pows = vec![x];
    for i in 0..63 {
        let square = (pows[i] * pows[i]) % modulo;
        pows.push(square);
    }
    let mut pow = 1;
    for i in 0..64 {
        let flag = k & 1;
        k >>= 1;
        if flag == 1 {
            pow = (pow * pows[i]) % modulo;
        }
    }
    pow
}

pub struct AdjacencyList(Vec<Vec<usize>>);

impl AdjacencyList {
    pub fn new(n: usize) -> Self {
        AdjacencyList(vec![vec![]; n])
    }
    pub fn add(&mut self, i: usize, j: usize) {
        self.0[i].push(j);
    }
    pub fn len(&self) -> usize {
        self.0.len()
    }
}

impl std::ops::Index<usize> for AdjacencyList {
    type Output = Vec<usize>;
    fn index(&self, i: usize) -> &Self::Output {
        &self.0[i]
    }
}

#[allow(dead_code)]
struct Scanner<R> {
    reader: BufReader<R>,
    buffer: String,
}
impl<R> Scanner<R>
where
    R: Read,
{
    #[allow(dead_code)]
    fn new(inner: R) -> Scanner<R> {
        let reader = BufReader::new(inner);
        let buffer = String::new();
        Scanner {
            reader: reader,
            buffer: buffer,
        }
    }
    #[allow(dead_code)]
    fn with_capacity(capacity: usize, inner: R) -> Scanner<R> {
        let reader = BufReader::with_capacity(capacity, inner);
        let buffer = String::new();
        Scanner {
            reader: reader,
            buffer: buffer,
        }
    }
    #[allow(dead_code)]
    fn read_line(&mut self) -> BoxResult<String> {
        self.reader.read_line(&mut self.buffer)?;
        let cloned = self.buffer.trim().to_string();
        self.buffer.clear();
        Ok(cloned)
    }
    #[allow(dead_code)]
    fn read<T>(&mut self) -> BoxResult<T>
    where
        T: FromStr,
        T::Err: std::fmt::Debug,
        T::Err: std::error::Error,
        T::Err: 'static,
    {
        self.reader.read_line(&mut self.buffer)?;
        let parsed = self.buffer.trim().parse()?;
        self.buffer.clear();
        Ok(parsed)
    }
    #[allow(dead_code)]
    fn read_two<T, U>(&mut self) -> BoxResult<(T, U)>
    where
        T: FromStr,
        T: Copy,
        T::Err: std::fmt::Debug,
        T::Err: std::error::Error,
        T::Err: 'static,
        U: FromStr,
        U: Copy,
        U::Err: std::fmt::Debug,
        U::Err: std::error::Error,
        U::Err: 'static,
    {
        self.reader.read_line(&mut self.buffer)?;
        let (a, b) = {
            let params = self.buffer.trim().split_whitespace().collect::<Vec<_>>();
            let a = params[0].parse()?;
            let b = params[1].parse()?;
            (a, b)
        };
        self.buffer.clear();
        Ok((a, b))
    }
    #[allow(dead_code)]
    fn read_three<T>(&mut self) -> BoxResult<(T, T, T)>
    where
        T: FromStr,
        T: Copy,
        T::Err: std::fmt::Debug,
        T::Err: std::error::Error,
        T::Err: 'static,
    {
        let nums = self.read_vector()?;
        Ok((nums[0], nums[1], nums[2]))
    }
    #[allow(dead_code)]
    fn read_vector<T>(&mut self) -> BoxResult<Vec<T>>
    where
        T: FromStr,
        T::Err: std::fmt::Debug,
        T::Err: std::error::Error,
        T::Err: 'static,
    {
        self.reader.read_line(&mut self.buffer)?;
        let parsed_vec = self
            .buffer
            .trim()
            .split_whitespace()
            .map(|s| s.parse())
            .collect::<Result<Vec<_>, _>>()?;
        self.buffer.clear();
        Ok(parsed_vec)
    }
    #[allow(dead_code)]
    fn read_matrix<T>(&mut self, lines: usize) -> BoxResult<Vec<Vec<T>>>
    where
        T: FromStr,
        T::Err: std::fmt::Debug,
        T::Err: std::error::Error,
        T::Err: 'static,
    {
        let mut parsed_matrix = vec![];
        for _ in 0..lines {
            self.reader.read_line(&mut self.buffer)?;
            parsed_matrix.push(
                self.buffer
                    .trim()
                    .split_whitespace()
                    .map(|s| s.parse())
                    .collect::<Result<Vec<_>, _>>()?,
            );
            self.buffer.clear();
        }
        Ok(parsed_matrix)
    }
    #[allow(dead_code)]
    fn read_chars(&mut self) -> BoxResult<Vec<char>> {
        self.reader.read_line(&mut self.buffer)?;
        let chars = self.buffer.trim().chars().collect::<Vec<_>>();
        self.buffer.clear();
        Ok(chars)
    }
}