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 Java8 (OpenJDK 1.8.0) (judge verdict: Time Limit Exceeded)

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;

public class Main {
	static ArrayList<Integer>[] adj = null;
	static int[] cnt = null;
	static int MOD = 1000000007;
	static HashMap<Integer, Long>[] memo = null;

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int[] a = new int[N];
		int[] b = new int[N];
		adj = new ArrayList[N];
		for (int i = 0; i < N - 1; i++) {
			a[i] = sc.nextInt() - 1;
			b[i] = sc.nextInt() - 1;
			if ( adj[a[i]] == null ) {
				adj[a[i]] = new ArrayList<>();
			}
			adj[a[i]].add(b[i]);
			if ( adj[b[i]] == null ) {
				adj[b[i]] = new ArrayList<>();
			}
			adj[b[i]].add(a[i]);
		}
		sc.close();

		StringBuilder ans = new StringBuilder();
		for ( int i = 0 ; i < N ; i++ ) {
			cnt = new int[N];
			memo = new HashMap[N];
			rec(i, -1);
			ans.append(rec2(i, -1));
			ans.append("\n");
		}

		System.out.println(ans.toString());
	}

	static int rec(int cur, int last) {
		for ( int next : adj[cur] ) {
			if ( next == last ) {
				continue;
			}
			cnt[cur] += rec(next, cur);
		}
		cnt[cur]++;
		return cnt[cur];
	}

	static long rec2(int cur, int last) {
		if ( memo[cur] == null ) {
			memo[cur] = new HashMap();
		} else if ( memo[cur].containsKey(last) ) {
			return memo[cur].get(last);
		}
		long ret = 1;
		int rem = cnt[cur] - 1;
		for ( int next : adj[cur] ) {
			if ( next == last ) {
				continue;
			}
			ret = (ret * combination(rem, cnt[next], MOD)) % MOD;
			rem = rem - cnt[next];
			ret = (ret * rec2(next, cur)) % MOD;
		}
		memo[cur].put(last, ret);
		return ret;
	}

	static int[] xgcd(int a, int b) {
		int x0 = 1;
		int y0 = 0;
		int x1 = 0;
		int y1 = 1;
		while ( b != 0 ) {
			int q = a / b;
			int tmp = b;
			b = a % b;
			a = tmp;

			tmp = x1;
			x1 = x0 - q * x1;
			x0 = tmp;

			tmp = y1;
			y1 = y0 - q * y1;
			y0 = tmp;
		}
		return new int[] {a, x0, y0};
	}
	static int modInv(int a, int mod) {
		int[] xgcd = xgcd(a, mod);
		return (xgcd[1] + mod) % mod;
	}
	static int combination(int n, int a, int mod) {
		int aa = Math.min(a, n - a);
		long ret = 1;
		for ( int i = 0 ; i < aa ; i++ ) {
			ret = (ret * (n - i)) % mod;
			ret = (ret * modInv(i + 1, mod)) % mod;
		}
		return (int) ret;
	}

}