Problem:
Score : 100 points

Problem Statement

There is a tree with N vertices, numbered 1, 2, \ldots, N.
For each i (1 \leq i \leq N - 1), the i-th edge connects Vertex x_i and y_i.

Taro has decided to paint each vertex in white or black, so that any black vertex can be reached from any other black vertex by passing through only black vertices.

You are given a positive integer M.
For each v (1 \leq v \leq N), answer the following question:

Assuming that Vertex v has to be black, find the number of ways in which the vertices can be painted, modulo M.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

2 \leq M \leq 10^9

1 \leq x_i, y_i \leq N

The given graph is a tree.

Input

Input is given from Standard Input in the following format:

N M
x_1 y_1
x_2 y_2
:
x_{N - 1} y_{N - 1}

Output

Print N lines.
The v-th (1 \leq v \leq N) line should contain the answer to the following question:

Assuming that Vertex v has to be black, find the number of ways in which the vertices can be painted, modulo M.

Sample Input 1

3 100
1 2
2 3

Sample Output 1

3
4
3

There are seven ways to paint the vertices, as shown in the figure below.
Among them, there are three ways such that Vertex 1 is black, four ways such that Vertex 2 is black and three ways such that Vertex 3 is black.

Sample Input 2

4 100
1 2
1 3
1 4

Sample Output 2

8
5
5
5

Sample Input 3

1 100

Sample Output 3

1

Sample Input 4

10 2
8 5
10 8
6 5
1 5
4 8
2 10
3 6
9 2
1 7

Sample Output 4

0
0
1
1
1
0
1
0
1
1

Be sure to print the answers modulo M.

Solution in C# (Mono 4.6.2.0) (judge verdict: Time Limit Exceeded)

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;

namespace ReadWriteTemplate
{
    public static class Solver
    {
        private static void SolveCase()
        {
            int n = ReadInt();
            long m = ReadLong();
            var vertices = new Vertex[n];
            for (int i = 0; i < n; i++)
            {
                vertices[i] = new Vertex();
            }
            for (int i = 0; i < n - 1; i++)
            {
                int x = ReadInt() - 1;
                int y = ReadInt() - 1;
                vertices[x].Edges.Add(vertices[y]);
                vertices[y].Edges.Add(vertices[x]);
            }

            vertices[0].Dfs1(null, m);
            vertices[0].Dfs2(null, 1, m);

            for (int i = 0; i < n; i++)
            {
                Writer.WriteLine(vertices[i].Answer);
            }
        }

        public class Vertex
        {
            public List<Vertex> Edges = new List<Vertex>();

            public long Value;

            public long Answer;

            public int ChildrenCount;

            public int GroupSize;

            public int GroupsCount;

            public long[] ChildrenValues;

            public long[] GroupValues;

            public void Dfs1(Vertex parent, long mod)
            {
                ChildrenCount = Edges.Count - (parent == null ? 0 : 1);
                if (ChildrenCount > 0)
                {
                    GroupSize = (int) Math.Sqrt(ChildrenCount);
                    GroupsCount = (ChildrenCount + GroupSize - 1) / GroupSize;

                    ChildrenValues = new long[ChildrenCount];
                    GroupValues = Enumerable.Repeat(1L, GroupsCount).ToArray();

                    int index = 0;
                    foreach (Vertex vertex in Edges)
                    {
                        if (vertex != parent)
                        {
                            vertex.Dfs1(this, mod);

                            ChildrenValues[index] = (vertex.Value + 1) % mod;

                            int groupId = index / GroupSize;
                            GroupValues[groupId] = GroupValues[groupId] * (vertex.Value + 1) % mod;

                            index++;
                        }
                    }
                }

                Value = 1;
                for (int i = 0; i < GroupsCount; i++)
                {
                    Value = Value * GroupValues[i] % mod;
                }
            }

            public void Dfs2(Vertex parent, long upValue, long mod)
            {
                Answer = Value * upValue % mod;

                int index = 0;
                foreach (Vertex vertex in Edges)
                {
                    if (vertex != parent)
                    {
                        int groupId = index / GroupSize;

                        long vUp = upValue;
                        for (int i = 0; i < GroupsCount; i++)
                        {
                            if (i != groupId)
                            {
                                vUp = vUp * GroupValues[i] % mod;
                            }
                        }

                        for (int i = groupId * GroupSize; i < Math.Min((groupId + 1) * GroupSize, ChildrenCount); i++)
                        {
                            if (i != index)
                            {
                                vUp = vUp * ChildrenValues[i] % mod;
                            }
                        }

                        vUp++;
                        if (vUp >= mod)
                        {
                            vUp -= mod;
                        }

                        vertex.Dfs2(this, vUp, mod);

                        index++;
                    }
                }
            }
        }

        public static void Solve()
        {
#if DEBUG
            var sw = Stopwatch.StartNew();
#endif

            SolveCase();

            /*int T = ReadInt();
            for (int i = 0; i < T; i++)
            {
                // Writer.WriteLine("Case {0}:", i + 1);
                // Writer.Write("Case #{0}: ", i + 1);
                SolveCase();
            }*/

#if DEBUG
            sw.Stop();
            Console.WriteLine($"{sw.ElapsedMilliseconds} ms");
#endif
        }

        public static void Main()
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;

#if DEBUG
            // Reader = Console.In; Writer = Console.Out;
            Reader = File.OpenText("input.txt"); Writer = File.CreateText("output.txt");
#else
            Reader = Console.In; Writer = Console.Out;
#endif

            // Solve();
            Thread thread = new Thread(Solve, 64 * 1024 * 1024);
            thread.CurrentCulture = CultureInfo.InvariantCulture;
            thread.Start();
            thread.Join();

            Reader.Close();
            Writer.Close();
        }

        public static IOrderedEnumerable<TSource> OrderByWithShuffle<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
        {
            return source.Shuffle().OrderBy(keySelector);
        }

        public static T[] Shuffle<T>(this IEnumerable<T> source)
        {
            T[] result = source.ToArray();
            Random rnd = new Random();
            for (int i = result.Length - 1; i >= 1; i--)
            {
                int k = rnd.Next(i + 1);
                T tmp = result[k];
                result[k] = result[i];
                result[i] = tmp;
            }
            return result;
        }

        #region Read/Write

        private static TextReader Reader;

        private static TextWriter Writer;

        private static Queue<string> CurrentLineTokens = new Queue<string>();

        private static string[] ReadAndSplitLine()
        {
            return Reader.ReadLine().Split(new[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
        }

        public static string ReadToken()
        {
            while (CurrentLineTokens.Count == 0)
                CurrentLineTokens = new Queue<string>(ReadAndSplitLine());
            return CurrentLineTokens.Dequeue();
        }

        public static string ReadLine()
        {
            return Reader.ReadLine();
        }

        public static int ReadInt()
        {
            return int.Parse(ReadToken());
        }

        public static long ReadLong()
        {
            return long.Parse(ReadToken());
        }

        public static double ReadDouble()
        {
            return double.Parse(ReadToken(), CultureInfo.InvariantCulture);
        }

        public static int[] ReadIntArray()
        {
            return ReadAndSplitLine().Select(int.Parse).ToArray();
        }

        public static long[] ReadLongArray()
        {
            return ReadAndSplitLine().Select(long.Parse).ToArray();
        }

        public static double[] ReadDoubleArray()
        {
            return ReadAndSplitLine().Select(s => double.Parse(s, CultureInfo.InvariantCulture)).ToArray();
        }

        public static int[][] ReadIntMatrix(int numberOfRows)
        {
            int[][] matrix = new int[numberOfRows][];
            for (int i = 0; i < numberOfRows; i++)
                matrix[i] = ReadIntArray();
            return matrix;
        }

        public static string[] ReadLines(int quantity)
        {
            string[] lines = new string[quantity];
            for (int i = 0; i < quantity; i++)
                lines[i] = Reader.ReadLine().Trim();
            return lines;
        }

        public static void WriteArray<T>(IEnumerable<T> array)
        {
            Writer.WriteLine(string.Join(" ", array));
        }

        #endregion
    }
}