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 5.4.1) (judge verdict: Time Limit Exceeded)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ll long long
#define rep(i,l,r)for(ll i=(l);i<(r);i++)
#define repp(i,l,r,k)for(ll i=(l);i<(r);i+=(k))
#define INF ((1LL<<62)-(1LL<<31))
#define max(p,q)((p)>(q)?(p):(q))
#define min(p,q)((p)<(q)?(p):(q))
#define bit(n,m)(((n)>>(m))&1)
int upll(const void*a, const void*b){return*(ll*)a<*(ll*)b?-1:*(ll*)a>*(ll*)b?1:0;}
int downll(const void*a, const void*b){return*(ll*)a<*(ll*)b?1:*(ll*)a>*(ll*)b?-1:0;}
void sortup(ll*a,int n){qsort(a,n,sizeof(ll),upll);}
void sortdown(ll*a,int n){qsort(a,n,sizeof(ll),downll);}
ll pom(ll a,ll n,int m){ll x=1;for(a%=m;n;n/=2)n&1?x=x*a%m:0,a=a*a%m;return x;}
//#define MOD 998244353
#define invp(a,p)pom(a,p-2,p)

//全方位木DPとかいうやつ？
ll MOD;

//辺の情報を個別に持つタイプ
typedef struct edge{ll s,g;}E;
typedef struct graph{
	ll vcnt,ecnt;
	E  e[200010];//適宜変える
	ll id[100010];//適宜変える
}G;
G g;

int esort(const void*a,const void*b){
	E*p=(E*)a,*q=(E*)b;
	if((*p).s<(*q).s)return -1;
	if((*p).s>(*q).s)return  1;
	if((*p).g<(*q).g)return -1;
	return 1;
}

ll root[100010];
void dfs(int k){
	rep(i,g.id[k],g.id[k+1])if(g.e[i].g!=root[k]){
		root[g.e[i].g]=k;
		dfs(g.e[i].g);
	}
}

void readgraph(){
	//適宜変える
	ll n;
	scanf("%lld%lld",&n,&MOD);
	rep(i,0,n-1){
		ll x,y;
		scanf("%lld%lld",&x,&y);
		x--,y--;
		g.e[2*i].s=x;
		g.e[2*i].g=y;
		g.e[2*i+1].s=y;
		g.e[2*i+1].g=x;
	}
	g.vcnt=n;
	g.ecnt=2*n-2;
	qsort(g.e,g.ecnt,sizeof(E),esort);

	int p=0;
	rep(i,0,g.vcnt){
		while(p<g.ecnt&&g.e[p].s<i)p++;
		g.id[i]=p;
	}
	g.id[g.vcnt]=g.ecnt;//一応番兵
	dfs(0);
}

ll dp1[100010];//dp1[i]=0を根とする根付き木とみて、頂点iを黒く塗るときの部分木の塗り方
ll dp2[100010];//dp2[i]=iを根としたとき、サイズが1小さい部分木であって0を含むものに対する塗り方
int flag[100010];

ll calc(int k){
	ll ans=1;
	//塗り方はΠ(dp1[*]+1)通り(+1は塗らない分)
	rep(i,g.id[k],g.id[k+1])if(g.e[i].g!=root[k]){
		ans=ans*(calc(g.e[i].g)+1)%MOD;
	}
	return dp1[k]=ans;
}

ll sub(int v,int k){
	//自分の親vについて、自分を除くΠ(dp*[*]+1)を求める
	if(flag[k])return dp2[k];
	flag[k]=1;
	ll ans=1;
	rep(i,g.id[v],g.id[v+1]){
		if(g.e[i].g==root[v]){
			ans=ans*(sub(root[v],v)+1)%MOD;
		}else if(g.e[i].g!=k){
			ans=ans*(dp1[g.e[i].g]+1)%MOD;
		}
	}
	return dp2[k]=ans;
}


ll calc2(int k){
	//dp2[k]を求める
	//答えはこれのdp1[k]倍
	return dp1[k]*(sub(root[k],k)+1)%MOD;
}

int main(){
	readgraph();
	printf("%lld\n",calc(0));
	rep(i,1,g.vcnt)printf("%lld\n",calc2(i));
	return 0;
}