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 fi first
#define se second
#define pb push_back
#define mod(n,k) ( ( ((n) % (k)) + (k) ) % (k))
#define forn(i,a,b) for(int i = a; i < b; i++)
#define forr(i,a,b) for(int i = a; i >= b; i--)
#define all(x) (x).begin(), (x).end()

typedef long long ll;
typedef long double ld;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;

const int maxn = 100000+10;
ll dp1[maxn],dp2[maxn],dp3[maxn],N,M;
vi graph[maxn];

ll extEuclides(ll a,ll b,ll &x, ll &y,ll mod = 0){
        if(!b){
                x = 1, y = 0;
                return a;
        }
        ll gcd = extEuclides(b,a%b,x,y,mod);
        x = !mod ? x-y*(a/b) : (mod+x-(y*(a/b))%mod)%mod;
        swap(x,y);
        return gcd;
}
ll modInverse(ll a,ll m){
	ll x,y;
	ll g = extEuclides(a,m,x,y,m);
	if(g != 1)return -1;
	return (x%m+m)%m;
}
ll modDivide(ll a,ll b,ll m){
	a %= m;
	ll inv = modInverse(b,m);
	if(inv == -1)return -1;
	return (inv*a)%m;
}
void dfs(int u,int p){
	dp1[u] = 1;
	vi nodes;
	for(auto &v : graph[u]){
		if(v == p)continue;
		nodes.pb(v);
		dfs(v,u);
	}
	if(nodes.empty())return;
	dp2[0] = dp1[nodes[0]]+1;
	for(int i = 1; i < nodes.size(); i++){
		dp2[i] = dp2[i-1];
		dp2[i] += ((dp2[i-1]%M)*(dp1[nodes[i]]%M))%M;
		dp2[i] %= M;
	}
	dp1[u] = dp2[nodes.size()-1];
}
void dfs2(int u,int p, ll arriba){
	ll sum = 1;
	for(auto &v : graph[u]){
		if(v == p)continue;
		sum *= ll(dp1[v]+1);
		sum %= M;
	}
	for(auto &v : graph[u]){
		if(v == p)continue;
		//sum = modDivide(sum,dp1[v]+1,M);
		//if(sum < 0)sum += M;
		sum /= (dp1[v]+1);
		dp3[v] = ((sum%M)*(arriba%M))%M; dp3[v]++;
		dp3[v] %= M;
		sum *= (dp1[v]+1);
		dfs2(v,u,dp3[v]);
	}
}
int main(){
	ios_base::sync_with_stdio(0); cin.tie(0);
	cin >> N >> M;
	forn(i,0,N-1){
		int u,v; cin >> u >> v; u--; v--;
		graph[u].pb(v);
		graph[v].pb(u);
	}
	dfs(0,0);
	dfs2(0,0,1);
	/*forn(i,0,N){
		cout << i+1 << ' ' << dp1[i] << ' ' << dp3[i] << ":D\n";
	}*/
	dp3[0] = 1;
	forn(i,0,N){
		ll res = ((dp1[i]%M)*(dp3[i]%M))%M;
		if(res < 0)res += M;
		cout << res << '\n';
	}
	return 0;
}
/*
__builtin_mul_overflow(x,y,&x)
-fsplit-stack
*/