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: Time Limit Exceeded)

use std::io::{self, BufRead};
use std::collections::{HashSet, HashMap};
use std::ops::{Mul, Div, Rem};
use std::cmp::Ordering;

fn find_prime_factors(n: i64) -> HashMap<i64, i64> {
  let mut res = HashMap::new();
  let mut i = 2;
  let mut m = n;
  while i * i <= m {
    while m % i == 0 {
      let val = 1 + res.get(&i).unwrap_or(&0);
      res.insert(i, val);
      m /= i;
    }
    i += 1
  }
  if m != 1 {
    let val = 1 + res.get(&m).unwrap_or(&0);
    res.insert(m, val);
  }
  res
}

struct Decom {
  fs: HashMap<i64, i64>
}

impl Decom {
  pub fn new(n: i64) -> Self {
    Decom { fs: find_prime_factors(n) }
  }

//   pub fn to_i64(&self) -> i64 {
//     let mut result = 1;
//     for (k, v) in self.fs.iter() {
//       for _ in 0..*v {
//         result *= k;
//       }
//     }
//     result
//   }
}

impl Mul for Decom {
  type Output = Self;

  fn mul(self, rhs: Self) -> Self {
    let mut result = self.fs.clone();
    for (k, v) in rhs.fs.iter() {
      match self.fs.get(k) {
        None    => result.insert(*k, *v),
        Some(n) => result.insert(*k, n + *v),
      };
    }
    Decom { fs: result }
  }
}

impl Div for Decom {
  type Output = Option<Self>;

  fn div(self, rhs: Self) -> Option<Self> {
    let mut result = self.fs.clone();
    for (k, v) in rhs.fs.iter() {
      match self.fs.get(k) {
        None => return None,
        Some(n) => match n.cmp(v) {
          Ordering::Less => { return None; },
          Ordering::Equal => { result.remove(k); },
          Ordering::Greater => { result.insert(*k, *n - v); },
        },
      }
    }
    Some(Decom{ fs: result })
  }
}

impl Rem<i64> for Decom {
  type Output = i64;

  fn rem(self, rhs: i64) -> i64 {
    let mut result = 1;
    for (k, v) in self.fs.iter() {
      for _ in 0..*v {
        result = (result * *k) % rhs
      }
    }
    result
  }
}

fn fac(n: i64) -> Decom {
  let mut result = Decom::new(1);
  for i in 2..(n+1) {
    result = result * Decom::new(i);
  }
  result
}

fn dfs(tree: &Vec<Vec<i64>>, visited: &mut HashSet<i64>, n: i64) -> (i64, Decom) {
  visited.insert(n);
  let mut cs: Vec<i64> = Vec::new();
  for i in &tree[n as usize] {
    if !visited.contains(i) {
      cs.push(*i);
    }
  }
  if cs.len() == 1 {
    let (count, combo) = dfs(tree, visited, cs[0]);
    visited.remove(&n);
    return (count + 1, combo)
  }
  let mut counts: Vec<(i64, Decom)> = Vec::new();
  for i in cs {
    counts.push(dfs(tree, visited, i));
  }
  let total: i64 = counts.iter().map(|x| x.0).sum();
  let mut denom = Decom::new(1);
  for &(i, _) in &counts {
    denom = denom * fac(i);
  }
  let mut factor = Decom::new(1);
  for (_, i) in counts {
    factor = factor * i;
  }
  visited.remove(&n);
  if let Some(combo) = fac(total) * factor / denom {
    (total + 1, combo)
  } else {
    panic!("Failed to divide");
  }
}

fn main() {
  let stdin = io::stdin();
  let mut lines = stdin.lock().lines();
  let line = lines.next().unwrap().unwrap();
  let n: i64 = line.parse().unwrap();
  let mut tree = vec![vec![]; n as usize];
  for line in lines {
    let line = line.unwrap();
    let vs: Vec<i64> = line.split(' ').map(|x| x.parse().unwrap()).collect();
    let a = vs[0] - 1;
    let b = vs[1] - 1;
    tree[a as usize].push(b);
    tree[b as usize].push(a);
  }

  let mut visited = HashSet::new();
  for i in 0..n {
    let (_, combo) = dfs(&tree, &mut visited, i);
    println!("{}", combo % 1000000007)
  }
}