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>
#define ll long long
#define PB push_back
#define MP make_pair
#define ff first
#define ss second

using namespace std;
ll dp[100001]={0},ans[100001]={0},M,dpup[100001]={0};



ll dfs(ll pt,vector < pair< ll,pair<ll,ll> > > adj[],ll old){
	
ll v=1;
vector <ll> vs;
ll zes=0;
ll xas=adj[pt].size();
 for(ll i=0;i<xas;i++){
 	ll ne=adj[pt][i].ff;
	if(ne==old)continue;
	ll sa=dfs(ne,adj,pt);
	v=(v*sa)%M;
 	vs.PB(sa);
 //	cout<<sa<<endl;
 	adj[pt][i].ss.ff=v;
 	}

 ll ze=1;
 ll xa=adj[pt].size();
 ll za=vs.size()-1;
 
for(ll z=xa-1;z>=0;z--){
	ll ne=adj[pt][z].ff;
	if(ne==old)continue;
	
	ze=(ze*vs[za])%M;
	//cout<<ze<<pt<<z<<endl;
	za--;
 	adj[pt][z].ss.ss=ze;}
 	
 dp[pt]=v;
 return (v+1)%M;
}

ll dfs2(ll pt,vector <pair <ll , pair<ll,ll > > > adj[],ll old){

if(old==-1){ans[pt]=dp[pt];}
else{	
    // cout<<dpup[old]<<" "<<dpup[pt]<<endl;
	 dpup[pt]=(dpup[old]*dpup[pt]+1)%M;
	ll an=(dp[pt]*(dpup[pt]))%M;
		ans[pt]=an;
}	


    ll ze=-1;
    ll x=adj[pt].size();
	for(ll i=0;i<x;i++){
		ll ne=adj[pt][i].ff;
		if(ne==old)continue;
        if(ze!=-1){
			ll zx=adj[pt][ze].ff;
			
			dpup[ne]=(adj[pt][ze].ss.ff)%M;
		    dpup[zx]=(dpup[zx]*adj[pt][i].ss.ss)%M;
             //cout<<adj[pt][i].ss.ss<<" "<<i<<" "<<pt<<endl;
        }
		    ze=i;}
		for(ll i=0;i<x;i++){
	    ll ne=adj[pt][i].ff;
		if(ne==old)continue;
    	dfs2(ne,adj,pt);   }
	
}
void run(){
	ll n,m;
	cin>>n>>M;
    vector <pair<ll,pair<ll,ll> > > adj[n+1];
	for(ll i=0;i<n-1;i++){
		ll x,y;
		cin>>x>>y;
		ll s=1;
		adj[x].PB(MP(y,MP(s,s)));
		adj[y].PB(MP(x,MP(s,s)));
		dpup[i+1]=1;
	}
	dpup[n]=1;
	dfs(1,adj,-1);

    dfs2(1,adj,-1);
	for(ll j=1;j<=n;j++){
		cout<<ans[j]<<endl;;
	}
}
int main() {
	ll t=1;
	//cin>>t;
	for(ll i=0;i<t;i++)run();
	
	// your code goes here
	return 0;
}