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: Wrong Answer)

using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using SB = System.Text.StringBuilder;
//using System.Text.RegularExpressions;
//using System.Globalization;
//using System.Diagnostics;
using static System.Console;
using System.Numerics;
using static System.Math;
using pair = Pair<long, long>;

class Program
{
    static void Main()
    {
        SetOut(new StreamWriter(OpenStandardOutput()) { AutoFlush = false });
        new Program().solve();
        Out.Flush();
    }
    readonly Scanner cin = new Scanner();
    readonly int[] dd = { 0, 1, 0, -1, 0 }; //→↓←↑
    readonly int mod = 1000000007;
    readonly int dom = 998244353;
    bool chmax<T>(ref T a, T b) where T : IComparable<T> { if (a.CompareTo(b) < 0) { a = b; return true; } return false; }
    bool chmin<T>(ref T a, T b) where T : IComparable<T> { if (b.CompareTo(a) < 0) { a = b; return true; } return false; }

    List<int>[] G;
    long[] dp;
    int M;
    long[] ans;
    long calc(int u, int p)
    {
        var ret = 1L;
        foreach (var v in G[u])
        {
            if (v == p)
            {
                continue;
            }            
            ret *= calc(v, u) + 1;
            ret %= M;
        }
        return dp[u] = ret;
    }
    void zenhoui(int u, int p)
    {
        var ret = 1L;
        foreach (var v in G[u])
        {
            ret *= dp[v] + 1;
            ret %= M;
        }
        ans[u] = ret;

        int N = G[u].Count;
        var L = new long[N];
        var R = new long[N];
        L[0] = dp[G[u][0]] + 1;
        for (int i = 1; i < N; i++)
        {
            L[i] = L[i - 1] * (dp[G[u][i]] + 1);
            L[i] %= M;
        }
        R[N - 1] = dp[G[u][N - 1]] + 1;
        for (int i = N - 2; i >= 0; i--)
        {
            R[i] = R[i + 1] * (dp[G[u][i]] + 1);
            R[i] %= M;
        }

        for (int i = 0; i < N; i++)
        {
            if (G[u][i] == p)
            {
                continue;
            }
            var tmp = 1L;
            if (0 <= i - 1)
            {
                tmp *= L[i - 1];
            }
            if (i + 1 < N)
            {
                tmp *= R[i + 1];
            }
            dp[u] = tmp % mod;
            zenhoui(G[u][i], u);
        }
    }
    void solve()
    {
        int N = cin.nextint;
        if (N == 1)
        {
            WriteLine(1);
            return;
        }
        M = cin.nextint;
        G = new List<int>[N];
        for (int i = 0; i < N; i++)
        {
            G[i] = new List<int>();
        }
        for (int i = 0; i < N - 1; i++)
        {
            int a = cin.nextint - 1;
            int b = cin.nextint - 1;
            G[a].Add(b);
            G[b].Add(a);
        }
        dp = new long[N];
        calc(0, -1);
        ans = new long[N];
        zenhoui(0, -1);
        foreach (var item in ans)
        {
            WriteLine(item);
        }  
    }

    long[] A, B;
    //a^b(mod p) solution works in O(logb).
    long powmod(long a, long b, long p)
    {
        if (b == 0) return 1;
        if (b % 2 == 0)
        {
            long d = powmod(a, b / 2, p);
            return d * d % p;
        }
        else
        {
            return a * powmod(a, b - 1, p) % p;
        }
    }
    void table(int n)
    {
        A = new long[n + 1];
        B = new long[n + 1];
        A[0] = 1;
        for (int i = 0; i < n; i++)
        {
            A[i + 1] = A[i] * (i + 1) % mod;
        }
        B[n] = powmod(A[n], mod - 2, mod);
        //modが合成数の場合
        //extgcd(A[n], mod, out long x, out long y);
        //B[n] = ((x % mod) + mod) % mod;
        for (int i = 0; i < n; i++)
        {
            B[n - i - 1] = B[n - i] * (n - i) % mod;
        }
    }
    long combi(int n, int r)
    {
        return A[n] * B[r] % mod * B[n - r] % mod;
    }
}


static class Ex
{
    public static void join<T>(this IEnumerable<T> values, string sep = " ") => WriteLine(string.Join(sep, values));
    public static string concat<T>(this IEnumerable<T> values) => string.Concat(values);
    public static string reverse(this string s) { var t = s.ToCharArray(); Array.Reverse(t); return t.concat(); }

    public static int lower_bound<T>(this IList<T> arr, T val) where T : IComparable<T>
    {
        int low = 0, high = arr.Count;
        int mid;
        while (low < high)
        {
            mid = ((high - low) >> 1) + low;
            if (arr[mid].CompareTo(val) < 0) low = mid + 1;
            else high = mid;
        }
        return low;
    }
    public static int upper_bound<T>(this IList<T> arr, T val) where T : IComparable<T>
    {
        int low = 0, high = arr.Count;
        int mid;
        while (low < high)
        {
            mid = ((high - low) >> 1) + low;
            if (arr[mid].CompareTo(val) <= 0) low = mid + 1;
            else high = mid;
        }
        return low;
    }
}

class Pair<T, U> : IComparable<Pair<T, U>> where T : IComparable<T> where U : IComparable<U>
{
    public T f; public U s;
    public Pair(T f, U s) { this.f = f; this.s = s; }
    public int CompareTo(Pair<T, U> a) => f.CompareTo(a.f) != 0 ? f.CompareTo(a.f) : s.CompareTo(a.s);
    public override string ToString() => $"{f} {s}";
}

class Scanner
{
    string[] s; int i;
    readonly char[] cs = new char[] { ' ' };
    public Scanner() { s = new string[0]; i = 0; }
    public string[] scan => ReadLine().Split();
    public int[] scanint => Array.ConvertAll(scan, int.Parse);
    public long[] scanlong => Array.ConvertAll(scan, long.Parse);
    public double[] scandouble => Array.ConvertAll(scan, double.Parse);
    public string next
    {
        get
        {
            if (i < s.Length) return s[i++];
            string st = ReadLine();
            while (st == "") st = ReadLine();
            s = st.Split(cs, StringSplitOptions.RemoveEmptyEntries);
            i = 0;
            return next;
        }
    }
    public int nextint => int.Parse(next);
    public long nextlong => long.Parse(next);
    public double nextdouble => double.Parse(next);
}