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()
        {
            /*long mod = 1000000007;
            long d = 2;
            for (int i = 0; i < 8000000; i++)
            {
                d = d * d % mod;
            }
            return;*/

            /*Writer.WriteLine("100000 1000000007");
            for (int i = 0; i < 50000; i++)
            {
                Writer.WriteLine($"1 {i + 2}");
            }
            for (int i = 50000; i < 100000 - 1; i++)
            {
                Writer.WriteLine($"2 {i + 2}");
            }
            return;*/

            int n = ReadInt();
            long m = ReadLong();
            var vertices = new Vertex[n];
            for (int i = 0; i < n; i++)
            {
                vertices[i] = new Vertex(i);
            }
            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]);
            }

            ChildrenValues = new long[n][];
            OuterGroupValues = new long[n][];

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

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


        public static long[][] ChildrenValues;

        public static long[][] OuterGroupValues;

        public class Vertex
        {
            public Vertex(int id)
            {
                Id = id;
            }

            public int Id;

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

            public long Value;

            public long Answer;

            public int ChildrenCount;

            public int GroupSize;

            public int GroupsCount;

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

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

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

                            long multValue = vertex.Value + 1;

                            ChildrenValues[Id][index] = multValue % mod;

                            int groupId = index / GroupSize;
                            for (int i = 0; i < GroupsCount; i++)
                            {
                                if (i != groupId)
                                {
                                    OuterGroupValues[Id][i] = OuterGroupValues[Id][i] * multValue % mod;
                                }
                            }
                            // GroupValues[groupId] = GroupValues[groupId] * (vertex.Value + 1) % mod;

                            Value = Value * multValue % mod;

                            index++;
                        }
                    }
                }
            }

            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 * OuterGroupValues[Id][groupId] % mod;

                        int childMaxId = Math.Min((groupId + 1) * GroupSize, ChildrenCount);
                        for (int i = groupId * GroupSize; i < childMaxId; i++)
                        {
                            if (i != index)
                            {
                                vUp = vUp * ChildrenValues[Id][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
    }
}


Measured memory_bytes: 55784