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

#include "bits/stdc++.h"
using namespace std;
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define ll long long int
#define int ll
//#define slld(t) scanf("%lld",&t)
//#define sd(t) scanf("%d",&t)
//#define pd(t) printf("%d\n",t)
//#define plld(t) printf("%lld\n",t)
#define vec vector < int >
#define vecp vector < pair < int , int > >
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define debug(x)   cerr<<#x<<": "<<x<<endl
#define pii pair < int , int >
#define pll pair < ll , ll > 
#define debug(x)   cerr<<#x<<": "<<x<<endl
// #define mod  1000000007
#define N 102
#define MAX 100005
#define mod1 1000000007
#define mod2 998244353
#define bitcnt __builtin_popcount
#define PI acos(-1)
//#define endl "\n"
const int inf = (int)1e18 + 10;
vector < int > v[MAX] , pre[MAX] , suff[MAX];
int up[MAX] , down[MAX];
int mod;
void dfs(int x , int p){
	down[x] = 1 ;
	vec t;
	pre[x].pb(1);
	pre[x].pb(1);
	suff[x].pb(1);
    suff[x].pb(1);
  for(int i = 0 ; i < v[x].size() ; i++){
  	int y = v[x][i];
  	if(y == p){
  		t.pb(1);
  		continue;
  	}
  	dfs(y , x);
  	down[x] *= (down[y] + 1);
  	down[x] %= mod;
  	t.pb(down[y] + 1);
    
  }
  if(v[x].size() == 0)
  	down[x] = 1;
  down[x] %= mod;
   // cout << x << " "<< down[x] << endl;
  for(int i = 1 ; i <= t.size() ; i++ ){	
  	pre[x][i] = pre[x][i-1] * t[i-1];
   
  }
  for(int i = t.size() ; i > 0 ; i-- ){
  	suff[x][i] = suff[x][i+1] * t[i-1];
  }  
}


void dfs2(int x , int p){
	for(int i = 0 ; i <  v[x].size() ; i++){
		int y = v[x][i];
		if(y == p) continue;
		up[y] = pre[x][i] * suff[x][i+2] ;
		up[y] %= mod;
		up[y]*= (up[x] + 1);
		up[y] %= mod;
		// cout << y << " "<< up[y] << endl;
		dfs2(y , x);
	}
}


signed main(){
  fast;
  int n , x , y;
  cin >> n >> mod;
  for(int i = 1 ; i < n ; i++){
  	cin >> x >> y;
  	v[x].pb(y);
  	v[y].pb(x);
  	pre[x].pb(1);
  	pre[y].pb(1);
  	suff[x].pb(1);
  	suff[y].pb(1);
  }



  dfs(1 , 0);
  up[1] = 0;
  dfs2(1 , 0);

  for(int i = 1 ; i <= n ; i++)
  	cout << ((up[i] + 1) * down[i] ) % mod << endl;
  


}