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.Linq;
using System.Collections.Generic;

namespace Contest
{
    class Program
    {
        static void Main(string[] args)
        {
            ABC160.F();
        }
        class ABC160
        {
            public static void A()
            {
                var s = Read.Str();
                Console.WriteLine(s[2] == s[3] && s[4] == s[5] ? "Yes" : "No");
            }
            public static void B()
            {
                var X = Read.Long();
                Console.WriteLine(X / 500 * 1000 + (X % 500) / 5 * 5);
            }
            public static void C()
            {
                var K = Read.Long();
                var N = Read.Int();
                var A = Read.IntN();
                var max = K + A[0] - A[N - 1];
                for(int i = 0; i < N - 1; ++i)
                {
                    max = Math.Max(max, A[i + 1] - A[i]);
                }
                Console.WriteLine(K - max);
            }
            public static void D()
            {
                var N = Read.Int();
                var X = Read.Int();
                var Y = Read.Int();

                Dictionary<int, HashSet<int>> Adj = new Dictionary<int, HashSet<int>>();
                for (int i = 0; i <= N; ++i)
                {
                    Adj.Add(i, new HashSet<int>());
                }
                for(int i = 1; i < N; ++i)
                {
                    Adj[i].Add(i + 1);
                    Adj[i + 1].Add(i);
                }
                Adj[X].Add(Y);
                Adj[Y].Add(X);

                int[] count = new int[N];
                for(int v = 1; v <= N; ++v)
                {
                    int[] dist = Enumerable.Range(0, N + 1).Select(i => -1).ToArray();
                    Queue<int> queue = new Queue<int>();
                    dist[v] = 0;
                    queue.Enqueue(v);
                    while (queue.Count > 0)
                    {
                        var w = queue.Dequeue();
                        foreach(var x in Adj[w])
                        {
                            if (dist[x] < 0)
                            {
                                dist[x] = dist[w] + 1;
                                count[dist[x]]++;
                                queue.Enqueue(x);
                            }
                        }
                    }
                }
                for(int k = 1; k <= N - 1; ++k)
                {
                    Console.WriteLine(count[k] / 2);
                }
            }
            public static void E()
            {
                var X = Read.Int();
                var Y = Read.Int();
                var A = Read.Int();
                var B = Read.Int();
                var C = Read.Int();
                var P = Read.LongN();
                var Q = Read.LongN();
                var R = Read.LongN();
                List<Tuple<long, int>> list = new List<Tuple<long, int>>();
                for(int i = 0; i < A; ++i)
                {
                    list.Add(new Tuple<long, int>(P[i], 0));
                }
                for (int i = 0; i < B; ++i)
                {
                    list.Add(new Tuple<long, int>(Q[i], 1));
                }
                for (int i = 0; i < C; ++i)
                {
                    list.Add(new Tuple<long, int>(R[i], 2));
                }
                list = list.OrderBy(x => -x.Item1).ToList();
                long sum = 0;
                int countX = 0;
                int countY = 0;
                int res = X + Y;
                int idx = 0;
                while (res > 0)
                {
                    var a = list[idx];
                    switch (a.Item2)
                    {
                        case 0:
                            if (countX < X)
                            {
                                sum += a.Item1;
                                countX++;
                                res--;
                            }
                            break;
                        case 1:
                            if (countY < Y)
                            {
                                sum += a.Item1;
                                countY++;
                                res--;
                            }
                            break;
                        case 2:
                            sum += a.Item1;
                            res--;
                            break;
                    }
                    idx++;
                }
                Console.WriteLine(sum);
            }
            public static void F()
            {
                var N = Read.Int();
                var Adj = Read.AdjacencyList(N, N - 1);
                var comb = Util.Combin(210000, Util.P);

                MOD[] Ans = new MOD[N + 1];
                MOD[] Count = new MOD[N + 1];
                MOD[] ProdC = new MOD[N + 1];
                var dist = Util.CalcDistance_OnTree(Adj, N, 1);
                var order = Enumerable.Range(1, N).OrderBy(x => -dist[x]).ToArray();
                var child = new int[N + 1];
                foreach(var v in order)
                {
                    var parent = -1;
                    var children = new List<int>();
                    foreach(var c in Adj[v])
                    {
                        if (dist[c] < dist[v]) { parent = c; } else { children.Add(c); child[v] += child[c] + 1; }
                    }
                    if (children.Count == 0)
                    {
                        Count[v] = 1;
                        continue;
                    }
                    MOD prod = Count[children[0]];
                    var childTotal = child[children[0]] + 1;
                    ProdC[v] = 1;
                    for(int i = 1; i < children.Count; ++i)
                    {
                        prod *= Count[children[i]];
                        var c = child[children[i]] + 1;
                        var com = comb(childTotal + c, c);
                        prod *= com;
                        ProdC[v] *= com;
                        childTotal += c;
                    }
                    Count[v] = prod;
                }
                Ans[1] = Count[1];
                var queue = new Queue<int>();
                queue.Enqueue(1);
                while (queue.Count > 0)
                {
                    var v = queue.Dequeue();
                    //var vProd = Ans[v] / ProdC[v];
                    foreach (var w in Adj[v])
                    {
                        var vProd = Ans[v] / comb(N - 1, child[w] + 1);
                        if (Ans[w].V > 0) { continue; }
                        queue.Enqueue(w);
                        var PROD = vProd;
                        List<int> childCounts = new List<int>();
                        childCounts.Add(N - 1 - child[w]);
                        foreach(var wc in Adj[w])
                        {
                            if (dist[wc] > dist[w])
                            {
                                childCounts.Add(child[wc] + 1);
                            }
                        }
                        if (childCounts.Count == 1)
                        {
                            Ans[w] = PROD;
                        }
                        else
                        {
                            MOD prodc = 1;
                            var total = childCounts[0];
                            for(int i=1; i < childCounts.Count; ++i)
                            {
                                var com = comb(total + childCounts[i], childCounts[i]);
                                PROD *= com;
                                prodc *= com;
                                total += childCounts[i];
                            }
                            Ans[w] = PROD / ProdC[w];
                            ProdC[w] = prodc;
                        }
                    }
                }
                for (int i = 1; i <= N; ++i)
                {
                    Console.WriteLine(Ans[i]);
                }
            }
        }
        class Util
        {
            public static readonly long P = 1000000007;

            public static int[] CalcDistance_OnTree(Dictionary<int, HashSet<int>> adj, int N, int start)
            {
                var dist = Enumerable.Range(0, N + 1).Select(i => -1).ToArray();
                dist[start] = 0;
                Queue<int> queue = new Queue<int>();
                queue.Enqueue(start);
                while (queue.Count > 0)
                {
                    var v = queue.Dequeue();
                    foreach (var w in adj[v])
                    {
                        if (dist[w] < 0)
                        {
                            dist[w] = dist[v] + 1;
                            queue.Enqueue(w);
                        }
                    }
                }
                return dist;
            }
            public static long Power(long a, long n, long p)
            {
                long res = 1;
                long mult = a;
                for (int i = 0; i < 40; ++i)
                {
                    if ((n & ((long)1 << i)) > 0)
                    {
                        res = (res * mult) % p;
                    }
                    mult = (mult * mult) % p;
                }
                return res;
            }
            public static long Inverse(long a, long p)
            {
                long b = p, c = 1, d = 0, e = 0, f = 0;
                while (b != 0)
                {
                    e = a / b;
                    f = b; b = a - e * b; a = f;
                    f = d; d = c - e * d; c = f;
                }
                c %= p;
                return c + (c < 0 ? p : 0);
            }
            public static long[] Facts(int n, long p)
            {
                var facts = new long[n + 1];
                facts[0] = 1;
                for (int i = 1; i <= n; ++i)
                {
                    facts[i] = (i * facts[i - 1]) % p;
                }
                return facts;
            }
            public static Func<int, int, long> Combin(int n, long P)
            {
                var fact = Facts(n, P);
                var inv = fact.Select(x => Inverse(x, P)).ToArray();
                return (N, M) =>
                {
                    if (N < 0 || N < M || M < 0) { return 0; }
                    return (((fact[N] * inv[M]) % P) * inv[N - M]) % P;
                };
            }
            public static Func<int, int, long> CombinLarge(int mMax, long p)
            {
                var inv = Enumerable.Range(0, mMax + 1).Select(x => Util.Inverse(x, Util.P)).ToArray();
                return (n, m) =>
                {
                    long mult = 1;
                    for (int i = 1; i <= m; ++i)
                    {
                        mult = (((mult * (n + 1 - i)) % Util.P) * inv[i]) % Util.P;
                    }
                    return mult;
                };
            }

        }
        struct MOD
        {
            public static long P = 1000000007;
            public readonly long V;

            public MOD(long a) { this.V = a; }
            public static implicit operator MOD(long a) { return new MOD(a); }
            public static MOD operator +(MOD a, MOD b) { return new MOD((a.V + b.V) % P); }
            public static MOD operator -(MOD a, MOD b) { return new MOD((a.V - b.V + P) % P); }
            public static MOD operator *(MOD a, MOD b) { return new MOD((a.V * b.V) % P); }
            public static MOD operator /(MOD a, MOD b) { return new MOD((a.V * b.Inverse().V) % P); }
            public MOD Inverse() { return Util.Inverse(V, P); }
            public MOD Power(long n) { return Util.Power(V, n, P); }
            public override string ToString() { return V.ToString(); }
        }

        static class Read
        {
            static Queue<string> queue = new Queue<string>();
            static void Enqueue() { var ss = Console.ReadLine().Split(' '); foreach (var s in ss) { queue.Enqueue(s); } }
            public static int Int() { if (queue.Count == 0) { Enqueue(); } return Convert.ToInt32(queue.Dequeue()); }
            public static long Long() { if (queue.Count == 0) { Enqueue(); } return Convert.ToInt64(queue.Dequeue()); }
            public static string Str() { if (queue.Count == 0) { Enqueue(); } return queue.Dequeue(); }
            public static int[] IntN() { return Console.ReadLine().Split(' ').Select(s => Convert.ToInt32(s)).ToArray(); }
            public static long[] LongN() { return Console.ReadLine().Split(' ').Select(s => Convert.ToInt64(s)).ToArray(); }
            public static string[] StrN() { return Console.ReadLine().Split(' '); }
            public static Tuple<int, int>[] TupleInt(int n) { List<Tuple<int, int>> list = new List<Tuple<int, int>>(); for (int i = 0; i < n; ++i) { var ab = Read.IntN(); list.Add(new Tuple<int, int>(ab[0], ab[1])); } return list.ToArray(); }
            public static Tuple<long, long>[] TupleLong(int n) { List<Tuple<long, long>> list = new List<Tuple<long, long>>(); for (int i = 0; i < n; ++i) { var ab = Read.LongN(); list.Add(new Tuple<long, long>(ab[0], ab[1])); } return list.ToArray(); }
            public static Tuple<string, string>[] TupleStr(int n) { List<Tuple<string, string>> list = new List<Tuple<string, string>>(); for (int i = 0; i < n; ++i) { var ab = Read.StrN(); list.Add(new Tuple<string, string>(ab[0], ab[1])); } return list.ToArray(); }
            public static Tuple<double, double>[] TupleDouble(int n) { List<Tuple<double, double>> list = new List<Tuple<double, double>>(); for (int i = 0; i < n; ++i) { var ab = Read.LongN(); list.Add(new Tuple<double, double>(ab[0], ab[1])); } return list.ToArray(); }
            public static long[,] LongMatrix(int r, int c)
            {
                var mat = new long[r, c];
                for (int i = 0; i < r; ++i)
                {
                    var x = Read.LongN();
                    for (int j = 0; j < c; ++j)
                    {
                        mat[i, j] = x[j];
                    }
                }
                return mat;
            }
            public static Dictionary<int, HashSet<int>> AdjacencyList(int N, int M)
            {
                Dictionary<int, HashSet<int>> dict = new Dictionary<int, HashSet<int>>();
                for (int i = 0; i <= N; ++i)
                {
                    dict.Add(i, new HashSet<int>());
                }
                for (int i = 0; i < M; ++i)
                {
                    var ab = Read.IntN();
                    dict[ab[0]].Add(ab[1]);
                    dict[ab[1]].Add(ab[0]);
                }
                return dict;
            }
            public static Dictionary<int, Dictionary<int, long>> AdjacencyListWithWeight(int N, int M)
            {
                Dictionary<int, Dictionary<int, long>> dict = new Dictionary<int, Dictionary<int, long>>();
                for (int i = 0; i <= N; ++i)
                {
                    dict.Add(i, new Dictionary<int, long>());
                }
                for (int i = 0; i < M; ++i)
                {
                    var ab = Read.IntN();
                    dict[ab[0]].Add(ab[1], ab[2]);
                    dict[ab[1]].Add(ab[0], ab[2]);
                }
                return dict;
            }
        }
    }
}