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 C# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
using System.Collections.Generic;
using System.Collections;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;
using static System.Math;
using System.Numerics;
static class Program{
	const int mod=(int)1e9+7;
	static List<int>[] li;
	static bool[] b;
	static int[] h,g,ans;
	static Mint[] f;
	static Cb r;
	static int n,o;
	static void Main(){
		Sc sc=new Sc();
		var s=sc.Ia;
		n=s[0];
		int m=n-1;
		li=new List<int>[n+1];
		b=new bool[n+1];
		h=new int[n+1];
		g=new int[n+1];
		f=new Mint[n+1];
		ans=new int[n];
		for(int i=1;i<=n;i++){li[i]=new List<int>();}
		o=1;
		for(int i=0;i<m;i++){
			var e=sc.Ia;
			li[e[0]].Add(e[1]);
			li[e[1]].Add(e[0]);
		}
		for(int i=1;i<=n;i++){
			if(li[i].Count==1){o=i;break;}
		}
		r=new Cb(n+1);
		Fu(o);
		ans[o-1]=(int)f[o];
		b=new bool[n+1];
		b[o]=true;
		Fu2(li[o][0],o,1);
		Console.WriteLine("{0}",String.Join("\n",ans));
	}
	static void Fu2(int a,int g,Mint u){
		b[a]=true;
		Mint z=f[a]*r.C1(n-1,n-h[a])*u;
		ans[a-1]=(int)z;
		for(int i=0;i<li[a].Count;i++){
			if(!b[li[a][i]]){
				Fu2(li[a][i],a,z/r.C1(n-1,n-1-h[li[a][i]])/f[li[a][i]]);
			}
		}
	}
	static int Fu(int a){
		b[a]=true;
		int p=0;
		Mint z=1;
		for(int i=0;i<li[a].Count;i++){
			if(!b[li[a][i]]){
				var d=Fu(li[a][i]);
				z*=r.C1(p+d,d)*f[li[a][i]];
				g[li[a][i]]=a;
				p+=d;
			}
		}
		f[a]=(int)z;
		return h[a]=p+1;
	}
}
public class Cb{
	private const int mod=(int)1e9+7;
	public long[] r;
	public Cb(int n){
		r=new long[n];
		r[0]=1;
		for(int i=1;i<n;i++){r[i]=(r[i-1]*i)%mod;}
	}
	public long C1(long n,long k){return (r[n]*Mi(r[k]*r[n-k]))%mod;}
	public long C2(long n,long k){
		long l=1;
		n%=mod;
		for(int i=1;i<=k;i++){l=(l*(n-i+1))%mod;l=(l*Mi(i))%mod;}
		return l;
	}
	public long P1(long n,long k){return n<k?0:(r[n]*Mi(r[n-k]))%mod;}
	public long Mi(long a){
		a=(a+mod)%mod;
		long b=mod,u=1,v=0;
		while(b>0){long t=a/b;a-=t*b;a^=b;b^=a;a^=b;u-=t*v;u^=v;v^=u;u^=v;}
		u%=mod;
		if(u<0){u+=mod;}
		return u%mod;
	}
}
public struct Mint{
	const int mod=(int)1e9+7;
	private long d;
	public Mint(long d){this.d=d;}
	public static implicit operator Mint(long d){return new Mint(d%mod);}
	public static explicit operator long(Mint d){return d.d;}
	public override string ToString(){return d.ToString();}
	public static Mint operator+(Mint a,long b){a.d=(a.d+b)%mod;return a;}
	public static Mint operator+(Mint a,Mint b){a.d=(a.d+b.d)%mod;return a;}
	public static Mint operator-(Mint a,long b){a.d=(mod+a.d-b)%mod;return a;}
	public static Mint operator-(Mint a,Mint b){a.d=(mod+a.d-b.d)%mod;return a;}
	public static Mint operator*(Mint a,long b){a.d=(a.d*b)%mod;return a;}
	public static Mint operator*(Mint a,Mint b){a.d=(a.d*b.d)%mod;return a;}
	public static Mint operator/(Mint a,long b){a.d=(a.d*Mi(b))%mod;return a;}
	public static Mint operator/(Mint a,Mint b){a.d=(a.d*Mi(b.d))%mod;return a;}
	private static long Mi(long a){
		a=(a+mod)%mod;
		long b=mod,u=1,v=0;
		while(b>0){
			long t=a/b;
			a-=t*b;a^=b;b^=a;a^=b;
			u-=t*v;u^=v;v^=u;u^=v;
		}
		u%=mod;
		if(u<0){u+=mod;}
		return u%mod;
	}
}
public class Sc{
	public int I{get{return int.Parse(Console.ReadLine());}}
	public long L{get{return long.Parse(Console.ReadLine());}}
	public double D{get{return double.Parse(Console.ReadLine());}}
	public string S{get{return Console.ReadLine();}}
	public int[] Ia{get{return Array.ConvertAll(Console.ReadLine().Split(),int.Parse);}}
	public long[] La{get{return Array.ConvertAll(Console.ReadLine().Split(),long.Parse);}}
	public double[] Da{get{return Array.ConvertAll(Console.ReadLine().Split(),double.Parse);}}
	public string[] Sa{get{return Console.ReadLine().Split();}}
	public object[] Oa{get{return Console.ReadLine().Split();}}
	public int[] Ia2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),int.Parse);}}
	public int[] Ia3(int a){return Array.ConvertAll((a.ToString()+" "+Console.ReadLine()).Split(),int.Parse);}
	public int[] Ia3(bool a,int b,bool c,int d){return Array.ConvertAll(((a?b.ToString()+" ":"")+Console.ReadLine()+(c?" "+d.ToString():"")).Split(),int.Parse);}
	public long[] La2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),long.Parse);}}
	public long[] La3(int a){return Array.ConvertAll((a.ToString()+" "+Console.ReadLine()).Split(),long.Parse);}
	public long[] La3(bool a,int b,bool c,int d){return Array.ConvertAll(((a?b.ToString()+" ":"")+Console.ReadLine()+(c?" "+d.ToString():"")).Split(),long.Parse);}
	public double[] Da2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),double.Parse);}}
	public double[] Da3(int a){return Array.ConvertAll((a.ToString()+" "+Console.ReadLine()).Split(),double.Parse);}
	public double[] Da3(bool a,int b,bool c,int d){return Array.ConvertAll(((a?b.ToString()+" ":"")+Console.ReadLine()+(c?" "+d.ToString():"")).Split(),double.Parse);}
	public T[] Arr<T>(int n,Func<T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f();}return a;}
	public T[] Arr<T>(int n,Func<int,T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(i);}return a;}
	public T[] Arr<T>(int n,Func<string[],T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(Console.ReadLine().Split());}return a;}
	public T[] Arr<T>(int n,Func<int,string[],T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(i,Console.ReadLine().Split());}return a;}
}