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++14 (GCC 5.4.1) (judge verdict: Accepted)

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> 
#include <ext/pb_ds/tree_policy.hpp>
 
// Remove GCC optimizer for codechef
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
 
using namespace std;
using namespace __gnu_pbds;
 
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define pb push_back
#define endl "\n"
#define int long long
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
 
const int INF = 1e16;
const double PI = acos(-1);
const int N = 1e6 + 7;
int mod = 1e9 + 7;

const int maxn = 1e5 + 7;
vector<int> graph[maxn];

int dp[maxn], ans[maxn];

void dfs(int u, int p)
{
    dp[u] = 1;
    for(int &v: graph[u])
        if(v != p)
        {
            dfs(v, u);
            dp[u] *= (dp[v] + 1);
            dp[u] %= mod;
        }
}

void dfs2(int u, int p, int val)
{
    ans[u] = (val + 1);
    ans[u] %= mod;
    int cnt = 0, prf = 1;
    vector<int> pref, suff, tmp;
    for(int &v: graph[u])
        if(v != p)
        {
            ans[u] *= (dp[v] + 1);
            ans[u] %= mod;
            int t = (dp[v] + 1);
            t %= mod;
            prf *= t;
            prf %= mod;
            pref.pb(prf);
            tmp.pb(t);
            cnt++;
        }
    reverse(tmp.begin(), tmp.end());
    int s = 1;
    for(int &e: tmp)
    {
        s *= e;
        s %= mod;
        suff.pb(s);
    }
    reverse(suff.begin(), suff.end());
    int i = 0;
    for(int &v: graph[u])
        if(v != p)
        {
            int pf = (i > 0 ? pref[i-1] : 1);
            int sf = (i < cnt-1 ? suff[i+1] : 1);
            int t = pf * sf;
            t %= mod;
            t *= ((val + 1) % mod);
            t %= mod;
            dfs2(v, u, t);
            i++;
        }
}

int32_t main()
{
    IOS;
    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);

    int n;
    cin >> n >> mod;
    for(int i = 0; i < n - 1; i++)
    {
        int u, v;
        cin >> u >> v;
        graph[u].pb(v);
        graph[v].pb(u);
    }
    dfs(1, 1);
    dfs2(1, 1, 0);
    for(int i = 1; i <= n; i++)
        cout << ans[i] << endl;
    return 0;
}