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

using Yorukatsu036.Questions;
using Yorukatsu036.Extensions;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;

namespace Yorukatsu036.Questions
{
    /// <summary>
    /// ABC160 F
    /// </summary>
    public class QuestionF_RerootingClass : AtCoderQuestionBase
    {
        List<int>[] edges;
        private DPState[] dpStates;

        public override IEnumerable<object> Solve(TextReader inputStream)
        {
            var nodesCount = inputStream.ReadInt();

            edges = Enumerable.Repeat(0, nodesCount).Select(_ => new List<int>()).ToArray();

            for (int i = 0; i < nodesCount - 1; i++)
            {
                var ab = inputStream.ReadIntArray().Select(j => j - 1).ToArray();
                edges[ab[0]].Add(ab[1]);
                edges[ab[1]].Add(ab[0]);
            }

            dpStates = new DPState[nodesCount];

            for (int i = 0; i < dpStates.Length; i++)
            {
                dpStates[i] = new DPState(new Modular(1), 0);
            }

            var rerooting = new Rerooting<DPState>(edges);
            var results = rerooting.Solve().Select(r => r.Count.Value);

            foreach (var result in results)
            {
                yield return result;
            }
        }

        struct DPState : ITreeDpState<DPState>
        {
            public Modular Count { get; }
            public int Size { get; }

            public DPState Identity => new DPState(new Modular(1), 0);

            public DPState(Modular count, int size)
            {
                Count = count;
                Size = size;
            }

            public DPState AddRoot() => new DPState(Count, Size + 1);

            public DPState Multiply(DPState other)
            {
                var size = Size + other.Size;
                var count = Modular.Combination(size, Size) * Count * other.Count;
                return new DPState(count, size);
            }
        }


        public interface ISemigroup<TSet> where TSet : ISemigroup<TSet>
        {
            TSet Multiply(TSet other);
        }

        public interface IMonoid<TSet> : ISemigroup<TSet> where TSet : IMonoid<TSet>, new()
        {
            TSet Identity { get; }
        }

        public interface ITreeDpState<TSet> : IMonoid<TSet> where TSet : ITreeDpState<TSet>, new()
        {
            TSet AddRoot();
        }

        public class Rerooting<TTreeDpState> where TTreeDpState : ITreeDpState<TTreeDpState>, new()
        {
            readonly IReadOnlyList<int>[] _graph;
            readonly TTreeDpState _identity;
            readonly Dictionary<int, TTreeDpState>[] _dp;
            readonly TTreeDpState[] _result;

            public Rerooting(IReadOnlyList<int>[] graph)
            {
                _graph = graph;
                _identity = new TTreeDpState().Identity;
                _dp = new Dictionary<int, TTreeDpState>[_graph.Length];
                _result = new TTreeDpState[_graph.Length];
            }

            public TTreeDpState[] Solve()
            {
                DepthFirstSearch();
                Reroot();
                return _result;
            }

            private TTreeDpState DepthFirstSearch() => DepthFirstSearch(0, -1);

            private TTreeDpState DepthFirstSearch(int root, int parent)
            {
                var sum = _identity;
                _dp[root] = new Dictionary<int, TTreeDpState>();

                foreach (var child in _graph[root])
                {
                    if (child == parent)
                        continue;
                    _dp[root].Add(child, DepthFirstSearch(child, root));
                    sum = sum.Multiply(_dp[root][child]);
                }
                return sum.AddRoot();
            }

            private void Reroot() => Reroot(0, -1, _identity);

            private void Reroot(int root, int parent, TTreeDpState toAdd)
            {
                var degree = _graph[root].Count;
                for (int i = 0; i < _graph[root].Count; i++)
                {
                    var child = _graph[root][i];
                    if (child == parent)
                    {
                        _dp[root].Add(child, toAdd);
                        break;
                    }
                }

                // 累積和
                int sumSize = degree + 1;
                var sumLeft = new TTreeDpState[sumSize];
                sumLeft[0] = _identity;
                for (int i = 0; i < degree; i++)
                {
                    var child = _graph[root][i];
                    sumLeft[i + 1] = sumLeft[i].Multiply(_dp[root][child]);
                }

                var sumRight = new TTreeDpState[sumSize];
                sumRight[degree] = _identity;
                for (int i = degree - 1; i >= 0; i--)
                {
                    var child = _graph[root][i];
                    sumRight[i] = sumRight[i + 1].Multiply(_dp[root][child]);
                }
                _result[root] = sumLeft[degree].AddRoot();

                for (int i = 0; i < _graph[root].Count; i++)
                {
                    var child = _graph[root][i];
                    if (child == parent)
                        continue;
                    var dp = sumLeft[i].Multiply(sumRight[i + 1]);
                    Reroot(child, root, dp.AddRoot());
                }
            }
        }

        [System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Auto)]
        public struct Modular : IEquatable<Modular>, IComparable<Modular>
        {
            private const int _defaultMod = 1000000007;
            private readonly long _value;    // 0 <= value < Mod の値をとる
            public int Mod { get; }
            public int Value => (int)_value;

            public Modular(long value, int mod = _defaultMod)
            {
                if (mod < 2)
                {
                    throw new ArgumentOutOfRangeException(nameof(mod), $"{nameof(mod)}は2以上の素数でなければなりません。");
                }
                Mod = mod;

                if (value >= 0 && value < mod)
                {
                    _value = value;
                }
                else
                {
                    _value = value % mod;
                    if (Value < 0)
                    {
                        _value += mod;
                    }
                }
            }

            private static void CheckModEquals(Modular a, Modular b)
            {
                if (a.Mod != b.Mod)
                {
                    throw new ArgumentException($"{nameof(a)}, {nameof(b)}", $"両者の法（{nameof(Mod)}）は等しくなければなりません。");
                }
            }

            public static Modular operator +(Modular a, Modular b)
            {
                CheckModEquals(a, b);

                var result = a._value + b._value;
                if (result > a.Mod)
                {
                    result -= a.Mod;    // 剰余演算を避ける
                }
                return new Modular(result, a.Mod);
            }

            public static Modular operator -(Modular a, Modular b)
            {
                CheckModEquals(a, b);

                var result = a._value - b._value;
                if (result < 0)
                {
                    result += a.Mod;    // 剰余演算を避ける
                }
                return new Modular(result, a.Mod);
            }

            public static Modular operator *(Modular a, Modular b)
            {
                CheckModEquals(a, b);
                return new Modular(a._value * b._value, a.Mod);
            }

            public static Modular operator /(Modular a, Modular b)  // こいつは値渡しの方が速いっぽい
            {
                CheckModEquals(a, b);
                return a * Pow(b, a.Mod - 2);
            }

            // 需要は不明だけど一応
            public static bool operator ==(Modular left, Modular right) => left.Equals(right);
            public static bool operator !=(Modular left, Modular right) => !(left == right);
            public static bool operator <(Modular left, Modular right) => left.CompareTo(right) < 0;
            public static bool operator <=(Modular left, Modular right) => left.CompareTo(right) <= 0;
            public static bool operator >(Modular left, Modular right) => left.CompareTo(right) > 0;
            public static bool operator >=(Modular left, Modular right) => left.CompareTo(right) >= 0;

            public static explicit operator int(Modular a) => a.Value;
            public static explicit operator long(Modular a) => a._value;

            public static Modular Pow(Modular a, int n)
            {
                switch (n)
                {
                    case 0:
                        return new Modular(1, a.Mod);
                    case 1:
                        return a;
                    default:
                        var p = Pow(a, n / 2);
                        return p * p * Pow(a, n % 2);
                }
            }

            private static Dictionary<int, List<int>> FactorialCache { get; } = new Dictionary<int, List<int>>();
            private static Dictionary<int, int[]> FactorialInverseCache { get; } = new Dictionary<int, int[]>();
            private static Dictionary<int, List<int>> PermutationCache { get; } = new Dictionary<int, List<int>>();
            const int maxFactorial = 1000000;

            public static Modular Factorial(int n, int mod = _defaultMod)
            {
                if (n < 0)
                {
                    throw new ArgumentOutOfRangeException(nameof(n), $"{nameof(n)}は0以上の整数でなければなりません。");
                }
                if (mod < 2)
                {
                    throw new ArgumentOutOfRangeException(nameof(mod), $"{nameof(mod)}は2以上の素数でなければなりません。");
                }

                if (!FactorialCache.ContainsKey(mod))
                {
                    FactorialCache.Add(mod, new List<int>() { 1 });
                }

                var cache = FactorialCache[mod];
                for (int i = cache.Count; i <= n; i++)  // Countが1（0!までキャッシュ済み）のとき1!～n!まで計算
                {
                    cache.Add((int)((long)cache[i - 1] * i % mod));
                }
                return new Modular(cache[n], mod);
            }

            public static Modular Permutation(int n, int r, int mod = _defaultMod)
            {
                CheckNR(n, r);

                if (!PermutationCache.ContainsKey(mod ^ n))
                {
                    PermutationCache.Add(mod ^ n, new List<int>() { 1 });
                }

                var cache = PermutationCache[mod ^ n];
                for (int i = cache.Count; i <= r; i++)  // Countが1（nP0までキャッシュ済み）のときnP1～nPrまで計算
                {
                    cache.Add((int)((long)cache[i - 1] * (n - (i - 1)) % mod));
                }
                return new Modular(cache[r], mod);
            }

            public static Modular Combination(int n, int r, int mod = _defaultMod)
            {
                if (!FactorialInverseCache.ContainsKey(mod))
                {
                    InitializeCombinationTable(maxFactorial, mod);
                }
                CheckNR(n, r);
                r = Math.Min(r, n - r);
                return n == r ? new Modular(1, mod) : new Modular(FactorialCache[mod][n], mod) * new Modular(FactorialInverseCache[mod][r], mod) * new Modular(FactorialInverseCache[mod][n - r], mod);
            }

            private static void InitializeCombinationTable(int max, int mod)
            {
                Factorial(max);
                FactorialInverseCache.Add(mod, new int[max + 1]);

                long fInv = (new Modular(1, mod) / Factorial(max, mod)).Value;
                FactorialInverseCache[mod][max] = (int)fInv;
                for (int i = max - 1; i >= 0; i--)
                {
                    fInv = (fInv * (i + 1)) % mod;
                    FactorialInverseCache[mod][i] = (int)fInv;
                }
            }

            public static Modular CombinationWithRepetition(int n, int r, int mod = _defaultMod) => Combination(n + r - 1, r, mod);

            private static void CheckNR(int n, int r)
            {
                if (n < 0)
                {
                    throw new ArgumentOutOfRangeException(nameof(n), $"{nameof(n)}は0以上の整数でなければなりません");
                }
                if (r < 0)
                {
                    throw new ArgumentOutOfRangeException(nameof(r), $"{nameof(r)}は0以上の整数でなければなりません。");
                }
                if (n < r)
                {
                    throw new ArgumentOutOfRangeException($"{nameof(n)},{nameof(r)}", $"{nameof(r)}は{nameof(n)}以下でなければなりません。");
                }
            }

            public override string ToString() => $"{_value} (mod {Mod})";

            public bool Equals(Modular other) => _value == other._value && Mod == other.Mod;

            public int CompareTo(Modular other) => Mod == other.Mod ? _value.CompareTo(other._value) : 0;

            public override int GetHashCode() => Value ^ Mod;
        }

    }
}


namespace Yorukatsu036
{
    class Program
    {
        static void Main(string[] args)
        {
            IAtCoderQuestion question = new QuestionF_RerootingClass();    // 問題に合わせて書き換え
            var answers = question.Solve(Console.In);
            var writer = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
            Console.SetOut(writer);
            foreach (var answer in answers)
            {
                Console.WriteLine(answer);
            }
            Console.Out.Flush();
        }
    }
}

#region Base Classes

namespace Yorukatsu036.Questions
{

    public interface IAtCoderQuestion
    {
        IEnumerable<object> Solve(string input);
        IEnumerable<object> Solve(TextReader inputStream);
    }

    public abstract class AtCoderQuestionBase : IAtCoderQuestion
    {
        public IEnumerable<object> Solve(string input)
        {
            var stream = new MemoryStream(Encoding.Unicode.GetBytes(input));
            var reader = new StreamReader(stream, Encoding.Unicode);

            return Solve(reader);
        }

        public abstract IEnumerable<object> Solve(TextReader inputStream);
    }

}

#endregion

#region Extensions

namespace Yorukatsu036.Extensions
{
    internal static class TextReaderExtensions
    {
        internal static int ReadInt(this TextReader reader) => int.Parse(ReadString(reader));
        internal static long ReadLong(this TextReader reader) => long.Parse(ReadString(reader));
        internal static double ReadDouble(this TextReader reader) => double.Parse(ReadString(reader));
        internal static string ReadString(this TextReader reader) => reader.ReadLine();

        internal static int[] ReadIntArray(this TextReader reader, char separator = ' ') => ReadStringArray(reader, separator).Select(int.Parse).ToArray();
        internal static long[] ReadLongArray(this TextReader reader, char separator = ' ') => ReadStringArray(reader, separator).Select(long.Parse).ToArray();
        internal static double[] ReadDoubleArray(this TextReader reader, char separator = ' ') => ReadStringArray(reader, separator).Select(double.Parse).ToArray();
        internal static string[] ReadStringArray(this TextReader reader, char separator = ' ') => reader.ReadLine().Split(separator);
    }
}

#endregion