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 mod 1000000007
#define ll long long int
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define boost ios_base::sync_with_stdio(false);cin.tie(NULL);
#define inf 9223372036854775807
#define mini 9223372036854775807
#include <ext/pb_ds/assoc_container.hpp> 
#include <ext/pb_ds/tree_policy.hpp> 
using namespace __gnu_pbds; 
#define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
vector<ll>v[100005];
ll dp[100005];
ll parent[100005]={0};
ll m;
ll dfs(ll i,ll k)
{
	parent[i]=k;
	ll j;
	for(j=0; j<v[i].size(); j++)
	{
		if(v[i][j]!=k)
		{
			ll z=dfs(v[i][j],i);
			dp[i]=((dp[i]%m)*((1+z)%m))%m;
		}
	}
	return dp[i];
}
void dfs2(ll i,ll k)
{
	//cout<<i<<" "<<k<<"-----------------------------------------"<<endl;
	ll j;
	ll left[v[i].size()+1];
	ll right[v[i].size()+1];
	for(j=0; j<=v[i].size(); j++)
	{
		left[j]=0;
		right[j]=0;
	}
	left[0]=(1+dp[v[i][0]])%m;
	right[v[i].size()-1]=(1+dp[v[i][v[i].size()-1]])%m;
	for(j=1; j<v[i].size(); j++)
		left[j]=((left[j-1]%m)*(1+dp[v[i][j]]%m))%m;
	for(j=v[i].size()-2; j>=0; j--)
		right[j]=((right[j+1]%m)*(1+dp[v[i][j]]%m))%m;
//	for(j=0; j<v[i].size(); j++)
//		cout<<left[j]<<" "<<right[j]<<endl;

	if(v[i].size()==1 && v[i][0]!=k)
	{
		dp[v[i][0]]=(dp[v[i][0]]*2)%m;
	//	cout<<i<<" "<<dp[v[i][0]]<<" "<<v[i][0]<<endl;
		dfs2(v[i][0],i);
	}
	else if(v[i].size()>1)
	{
		for(j=0; j<v[i].size(); j++)
		{
			if(v[i][j]==k)
				continue;
	//		cout<<i<<" "<<k<<" "<<j<<" "<<v[i][j]<<" "<<dp[v[i][j]]<<" ";
			if(j==0)
				dp[v[i][j]]=((dp[v[i][j]]%m)*((1+right[j+1])%m))%m;
			else if(j==v[i].size()-1)
				dp[v[i][j]]=((dp[v[i][j]]%m)*((1+left[j-1])%m))%m;
			else
				dp[v[i][j]]=((dp[v[i][j]]%m)*((1+(((left[j-1]%m)*(right[j+1]%m))%m))%m))%m;
	//		cout<<dp[v[i][j]]<<" "<<right[j+1]<<" "<<left[j-1]<<endl;
	//		cout<<v[i][j]<<" "<<i<<endl;
			dfs2(v[i][j],i);
		}
	}
}
int main()
{
	boost
	ll n;
	cin>>n>>m;
	ll i;
	for(i=0; i<100005; i++)
	{
		dp[i]=1;
	}
	for(i=1; i<n; i++)
	{
		ll a,b;
		cin>>a>>b;
		v[a].push_back(b);
		v[b].push_back(a);
	}
	if(n!=1)
	{
		ll ans=dfs(1,-1);
		dfs2(1,-1);
	}
	else
	{
		dp[1]=1%m;
	}
	ll sum=0;
	for(i=1; i<=n; i++)
	{
		cout<<dp[i]<<endl;
	}
	return 0;
}