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

#include<iostream>
#include<vector>
using namespace std;
#define int long long int
void dfs(int src,vector<int> adj[],int p,bool isleaf[])
{
	int count=0;
	for(int x:adj[src])
	{
		if(x!=p)
		{
			dfs(x,adj,src,isleaf);
			count++;
		}
	}
	if(count==0)
	{
		isleaf[src]=true;
	}
}
void dfs(int src,vector<int> adj[],int p,int in[],bool isleaf[],int m)
{
	if(isleaf[src])
	{
		in[src]=1;
		return ;
	}
	else
	{
		for(int x:adj[src])
		{
			if(x!=p)
			{
				dfs(x,adj,src,in,isleaf,m);
			}
		}
		for(int x:adj[src])
		{
			if(x!=p)
			{
				in[src]*=(in[x]+1);
				in[src]%=m;
			}
		}
	}
}
void dfs(int src,vector<int> adj[],int p,int prev,int in[],int out[],bool isleaf[],int m)
{
	out[src]*=(prev+1);
	out[src]%=m;
	int next=prev+1;  // prev is for black only
	next%=m;
	if(isleaf[src])
	  return ;
	vector<int> vec;
	for(int x:adj[src])
	{
		if(x!=p)
		{
			vec.push_back(x);
		}
		
	}
	int n=vec.size();
	int prefix[n],suffix[n];
	prefix[0]=in[vec[0]]+1;
	int j;
	for(j=1;j<n;j++)
	{
		prefix[j]=prefix[j-1]*(in[vec[j]]+1);
		prefix[j]%=m;
	}
	suffix[n-1]=(in[vec[n-1]]+1);
	for(j=n-2;j>=0;j--)
	{
		suffix[j]=suffix[j+1]*(in[vec[j]]+1);
		suffix[j]%=m;
	}
	for(j=0;j<n;j++)
	{
		int here=1;
		if(j>=1)
		{
			here*=prefix[j-1];
		}
		if(j+1<n)
		{
			here*=suffix[j+1];
		}
		here%=m;
		int final=here*next;
		int x=vec[j];
		dfs(x,adj,src,final,in,out,isleaf,m);
	}
}
int32_t main()
{
	int n;
	cin>>n;
	int m;
	cin>>m;
	vector<int> adj[n+2];
	int j;
	for(j=1;j<n;j++)
	{
		int u,v;
		cin>>u>>v;
		adj[u].push_back(v);
		adj[v].push_back(u);
	}
	bool isleaf[n+2]={false};
	dfs(1,adj,-1,isleaf);
	int in[n+2],out[n+2];
	for(j=0;j<=n+1;j++)
	{
		in[j]=1;
		out[j]=1;
	}
	dfs(1,adj,-1,in,isleaf,m);
	dfs(1,adj,-1,0,in,out,isleaf,m);
	int answer[n+2];
	for(j=1;j<=n;j++)
	{
		answer[j]=in[j]*out[j];
		answer[j]%=m;
	}
	for(j=1;j<=n;j++)
	{
		cout<<answer[j]<<endl;
	}
	cout<<endl;
}