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)

//long long -> ll
//http://web.mit.edu/~ecprice/acm/acm08/notebook.html#file12
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
#include<bits/stdc++.h>
typedef long long ll;
#define x first
#define y second
#define all(aaa) (aaa).begin(),(aaa).end()
#define REP(i,sss,eee) for(long long i=sss;i<=eee;++i)
#define pb push_back
#define pii pair<long long,long long>
#define vi vector<long long>
#define speed ios_base::sync_with_stdio(0);cin.tie(0);
using namespace __gnu_pbds;
using namespace std;
template <class T>
T MAX(T a, T b){return ((a>b)?a:b);}
template <class T>
T MIN(T a, T b){return ((a<b)?a:b);}
template <class T>
T MOD(T a){return ((a>=0)?a:(-1*a));}
template <class T>
void SWAP(T &a,T &b){T temp=a;a=b;b=temp;}

/*****************default********************/
long long mod;
std::vector<std::vector<long long>> g;
std::vector<bool> vis;std::vector<long long> ways;std::vector<long long> par;
std::vector<long long> topo;
long long dfs(long long num)
{
	vis[num]=1;
	for(auto it:g[num])
	{
		if(!vis[it])
		{
			par[it]=num;
			ways[num]=((ways[num]*(1+dfs(it)))%mod);
		}
	}
	topo.pb(num);
	return ways[num];
}
long long pro(long long a,long long b){return ((((a*b)%mod)+mod)%mod);}
pii eu(long long a,long long b)
{
	if(a==0){return {0,1};}
	if(b==0){return {1,0};}
	long long q=(a/b),r=(a%b);
	pii t=eu(b,r);
	return {t.y,((t.x)-(q*(t.y)))};

}
long long inv(long long num)
{
	if(__gcd(num,mod)!=1){return 0;}
	pii ans=eu(num,mod);
	ans.x=((((ans.x)%mod)+mod)%mod);
	return ans.x;
}
std::vector<long long> dp;
long long solve()
{
	long long n;cin>>n>>mod;
	g.resize(n);vis.resize(n,0);ways.resize(n,1);par.resize(n);
	REP(i,0,n-2)
	{
		long long a,b;cin>>a>>b;--a;--b;
		g[a].pb(b);g[b].pb(a);
	}
	dfs(0);
	dp.resize(n,-1);
	reverse(topo.begin(),topo.end());
	dp[0]=ways[0];
	for(auto it:topo)
	{
		if(dp[it]!=-1){continue;}
		long long p=par[it];
		long long den=(1+ways[it]),num=dp[p];long long g=__gcd(mod,den);
		if(g!=1){
			den/=g;
			num/=g;
		}
		dp[it]=pro(ways[it],(pro(num,inv(den))+1));
	}
	for(auto it:dp){cout<<it<<"\n";}
}
signed main(){
	long long tt=1;
	//cin>>tt;
	while(tt--)
	{
		solve();
	}
	return 0;
}