#include <bits/stdc++.h>
#define rep(i,n)for(int i=0;i<(n);i++)
//~ #define MOD 1000000007
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3f
#define EPS (1e-10)
using namespace std;
typedef long long ll;
typedef pair<int,int>P;

int n;
int MOD;

vector<int>E[200000];
ll dp[200000],dp2[200000];
map<int,ll>dpa,dpb;

void dfs(int v,int p){
	for(int u:E[v]){
		if(u==p)continue;
		dfs(u,v);
	}
	dp[v]=1;
	for(int u:E[v]){
		if(u==p)continue;
		dpa[u]=dp[v];
		(dp[v]*=(dp[u]+1))%=MOD;
	}
	if(p==-1){
		{
			ll a=1;
			for(int i=0;i<E[v].size();i++){
				int u=E[v][i];
				dpa[u]=a;
				(a*=(dp[u]+1))%=MOD;
			}
		}
		{
			ll a=1;
			for(int i=E[v].size()-1;i>=0;i--){
				int u=E[v][i];
				dpb[u]=a;
				(a*=(dp[u]+1))%=MOD;
			}
		}
	}
}
void dfs2(int v,int p){
	if(p==-1)dp2[v]=dp[v];
	else{
		ll c=(dpa[v]*dpb[v]%MOD+1)%MOD;
		{
			ll a=1;
			for(int i=0;i<E[v].size();i++){
				int u=E[v][i];
				dpa[u]=a;
				if(u==p)(a*=c)%=MOD;
				else (a*=(dp[u]+1))%=MOD;
			}
		}
		{
			ll a=1;
			for(int i=E[v].size()-1;i>=0;i--){
				int u=E[v][i];
				dpb[u]=a;
				if(u==p)(a*=c)%=MOD;
				else (a*=(dp[u]+1))%=MOD;
			}
			dp2[v]=a;
		}
	}
	for(int u:E[v]){
		if(u==p)continue;
		dfs2(u,v);
	}
}
int main(){
	cin>>n>>MOD;
	rep(i,n-1){
		int a,b;scanf("%d%d",&a,&b);a--;b--;
		E[a].push_back(b);
		E[b].push_back(a);
	}
	dfs(0,-1);
	dfs2(0,-1);
	rep(i,n){
		printf("%lld\n",dp2[i]);
	}
}