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: Time Limit Exceeded)

//This code is solely the property of StarnyC
#include<bits/stdc++.h>
using namespace std;
#define ll          long long
#define ld          long double
#define int         long long
#define hell        1000000007
#define hellx       998244353
#define pb          push_back
#define mp          make_pair
#define reset(a,b)  memset(a,b,sizeof(a))
#define Go_Baby_Go  ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);cerr.tie(0);
#define deb(x)      cout << (#x) << " is " << (x) << endl;
#define onbits(n)    __builtin_popcount(n)
#define INF         numeric_limits<int>::max()
#define ff          first
#define ss          second
#define pii         pair<ll,ll>
#define sz(a)       (ll)((a).size())
#define all(c)      (c).begin(),(c).end()
#define forn(i,n)   for(int i=0;i<(int)n;i++)
#define endl		'\n'

ll gcd(ll a,ll b) {
	if(b==0)    return a;
	            return gcd(b,a%b);
}
 
ll binexp(ll x,ll n,ll m) {
    if(!n)          return 1;
    else if(n&1)    return (x%m)*binexp((x%m)*(x%m),n/2,m)%m;
    else if(!(n&1)) return binexp((x%m)*(x%m),n/2,m)%m;
                    return 0;
}
int ans[100005],m;
vector <int> adj[100005];
map <pii , int> dp;

int dfs(int par, int child)
{
	//cout << par << " " << child << endl;
	if( dp[ {par, child} ]  ) { 
		//cout << dp[ {par, child} ] << " calculated result  " << endl;
		return dp[ {par, child} ];
	}

	int temp = 1;
	for(auto i:adj[child]) {
		if(i==par) continue;
		else { temp *= dfs(child, i) ; temp %= m; }
	}

	temp++;
	dp[ {par, child} ] = temp;
	//cout << dp[ {par, child} ] << " first time calculating " << endl;
	return temp%m;
}

int32_t solve(ll _,ll _t)
{  
	int n;
	cin >> n >> m;
	for(int i = 0; i < n-1 ; i++) {
		int x , y;
		cin >> x >> y;
		adj[x].pb(y);
		adj[y].pb(x);
	}


	for(int i = 1; i <= n; i++) {
		ans[i] = 1;
		for(auto j:adj[i]) {
			ans[i] *= dfs(i,j);
			ans[i] %= m;
		}
		cout<<ans[i]%m<<endl;
	}

	


}
 
int32_t main()
{
 Go_Baby_Go
//cerr<< '\n' << "Time elapsed :" << clock() * 1000.0 / CLOCKS_PER_SEC << " ms" ;

ll _=1;
//cin>>_;
//cin.ignore();
ll _t=_;
while(_--)
solve(_,_t);
 
return 0;
 
}