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: Runtime Error)

// https://atcoder.jp/contests/dp/tasks/dp_v
// V - Subtree

#include <iostream>
#include <algorithm>
#include <vector>
#include <cassert>

#include <stdio.h>

using namespace std;

vector< vector<int> > adj;
vector< vector<int> > adj_count;
vector< long long > ans;
vector< bool > visited;


int M;

long long dfs1(int u) {
  // cout << "v1" << u << endl;
  visited[u] = true;
  long long a = 1;
  int c = 0;
  for (auto v: adj[u]) {
    // cout << "ds " << u << " " << v << endl;
    if (!visited[v]) {
      auto r = dfs1(v) + 1;
      a = (a * r) % M;
      adj_count[u][c] = r % M;
    }
    c++;
  }

  return a;
}

void dfs2(int u, long long father) {
  // cout << "v2 " << u << " " << father << endl;
  visited[u] = true;


  int le = adj[u].size();

  if (le==0) {
    ans[u] = 1;
    return;
  }

  vector<long long> l( le, 1 );
  vector<long long> r( le, 1 );
  vector<long long> f( le, 1 );

  for(auto i=0; i<le; i++) {
    auto v = adj[u][i];
    if (visited[v]) adj_count[u][i] = father+1;
    // cout << "ac " << adj_count[u][i] << endl;

    if (i>0) 
      l[i] = (l[i-1] * adj_count[u][i] ) % M; 
    else
      l[i] = adj_count[u][0] % M;
  }

  r[le-1] = adj_count[u][le-1];
  if (le > 1) f[le-1] = l[le-2]; else f[le-1]= 1;

  for(auto i=le-2; i >=0; i--) {
    auto v = adj[u][i];
    r[i] = (r[i+1] * adj_count[u][i] ) % M;
    if (i > 0) f[i] = (l[i-1] * r[i+1] ) % M;
    else f[i] = r[i+1] % M;
  }


  assert(r[0] == l[le-1]);
  ans[u] = r[0];

  for(auto i=0; i<le; i++) {
    // cout << "fi" << i << " " << f[i] << endl;
    auto v = adj[u][i];
    if (!visited[v])  dfs2(v,  f[i]);
  }
}

int main() {
  int N;
  cin >> N >> M;

  adj.assign(N, {});
  adj_count.assign(N, {});
  ans.assign(N, 0);
  visited.assign(N, false);

  for(int i=0; i < N-1; i++) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    adj[u].push_back(v);
    adj_count[u].push_back(0);
    adj[v].push_back(u);
    adj_count[v].push_back(0);
  }

  dfs1(0);
  visited.assign(N, false);
  dfs2(0, 0);
  for(auto i=0; i<N; i++) cout << ans[i] << endl;
  return 0;
}