#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
#define int long long
#define gmax(x,y) x=max(x,y)
#define gmin(x,y) x=min(x,y)
#define F first
#define S second
#define P pair
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define rep(i,a,b) for(int i=a;i<b;i++)
#define V vector
#define RE return
#define ALL(a) a.begin(),a.end()
#define MP make_pair
#define PB emplace_back
#define PF push_front
#define FILL(a,b) memset(a,b,sizeof(a))
#define lwb lower_bound
#define upb upper_bound
using namespace std;
int mod,fa[100005],in[100005],out[100005],cnt;
V<int> v[100005]; 
void dfsin(int p){
	in[p]=1;
	for(auto u:v[p]){
		if(u!=fa[p]){
			fa[u]=p;
			dfsin(u);
			in[p]=(in[p]*(in[u]+1))%mod;
		}
	}
}
int qz[100005],hz[100005],cur[100005];
void dfsout(int p){
	cnt=0;
	for(auto u:v[p])if(u!=fa[p])cur[cnt++]=u;
	qz[0]=out[p];
	rep(i,0,cnt)qz[i+1]=(qz[i]*(1+in[cur[i]]))%mod;
	hz[cnt]=1;
	for(int i=cnt-1;i>=0;i--)hz[i]=(hz[i+1]*(1+in[cur[i]]))%mod;
	rep(i,0,cnt)out[cur[i]]=(1+hz[i+1]*qz[i])%mod;
	for(auto u:v[p])if(u!=fa[p])dfsout(u);
}
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	int n,x,y;
	cin>>n>>mod;
	rep(i,1,n){
		cin>>x>>y;
		v[x].PB(y);
		v[y].PB(x);
	}
	dfsin(1);
	out[1]=1;dfsout(1);
	FOR(i,1,n)cout<<in[i]*out[i]%mod<<'\n';
	RE 0;
}


