#include <bits/stdc++.h>
using namespace std;

#define int long long
#define ff first
#define ss second
#define endl "\n"
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x.size())
int powmod(int a,int l, int md){a%=md; int res=1;while(l){if(l&1)res=res*a%md;l/=2;a=a*a%md;}return res;}
int binpow(int a,int l){int res=1;while(l){if(l&1)res=res*a;l/=2;a=a*a;}return res;}
int invmod(int a, int md){return powmod(a,md-2,md);}
typedef long long ll; typedef unsigned long long ull; typedef long double ld;
typedef vector<int> vi; typedef pair<int, int> ii; typedef vector< ii > vii;
#define pb push_back
int __set(int b, int i) {return b|(1LL<<i);} //set ith bit
int __unset(int b, int i) {return b&(~(1UL<<i));}
int __check(int b, int i) {return b&(1LL<<i);} //returns 0 if ith bit is 0
int mulmod(int a, int b, int md) {return (((a%md)*(b%md))%md+md)%md;}
int addmod(int a, int b, int md) {return ((a%md+b%md)%md+md)%md;}
int submod(int a, int b, int md) {return (((a%md-b%md)%md)+md)%md;}
int divmod(int a, int b, int md) {return mulmod(a, powmod(b, md-2, md), md);} //if md is prime;
const ll inf = 0xFFFFFFFFFFFFFFFL; //very large number
// priority_queue<ii, vector<ii>, greater<ii> > pq; //for min priority_queue
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int MOD=1;
class SegmentTree {
private:
	int n; vii st; vi lz; vi a;
	void merge(int node) {
		st[node].ss=st[2*node].ss*st[2*node+1].ss;
		st[node].ss%=MOD;
	}
	void build(int node, int lo, int hi) {
		if(lo==hi) {
			st[node]={lo,a[lo]};
			return;
		}
		build(2*node,lo,(lo+hi)/2);
		build(2*node+1,(lo+hi)/2+1,hi);
		merge(node);
	}
	void propagate(int node, int l, int r) {
		if(l!=r) {
			lz[2*node] += lz[node];
			lz[2*node] %=MOD;
			lz[2*node+1] += lz[node];
			lz[2*node+1]%=MOD;
		}
		st[node].ss += (r-l+1)*(lz[node]);
		st[node].ss%=MOD;
		lz[node]=0;
	}
	void update(int node, int l, int r, int lo, int hi, int val) {
		if(lz[node]!=0)
			propagate(node,l,r);
		if(hi < l || lo > r)
			return;
		if(hi >= r && lo <= l) {
			lz[node]+=val;
			lz[node]%=MOD;
			propagate(node,l,r);
			return;
		}
		update(2*node,l,(l+r)/2,lo,hi,val);
		update(2*node+1,(l+r)/2+1,r,lo,hi,val);
		merge(node);
	}
	ii rmq(int node, int l, int r, int lo, int hi) {
		// cout<<l<<" "<<r<<" "<<lo<<" "<<hi<<endl;
		if(lz[node]!=0)
			propagate(node,l,r);
		if(hi < l || lo > r)
			return {-1, 1};
		if(hi >= r && lo <= l)
			return st[node];
		ii d1 = rmq(2*node, l, (l+r)/2, lo, hi);
		ii d2 = rmq(2*node+1, (l+r)/2+1, r, lo, hi);
		return {-1, (d1.ss*d2.ss)%MOD};
	}
public:
	SegmentTree(vi& __a) {
		n = __a.size();
		a=__a;
		st.assign(4*n,{0,0});
		lz.assign(4*n,0);
		build(1,0,n-1);
	}
	void Update(int l, int r, int val) {return update(1,0,n-1,l,r,val);}
	ii RMQ(int l, int r) {return rmq(1,0,n-1,l,r);}
};

int n,seen[100010],dp[100010],parent[100010],dp1[100010],ans[100010];
vector<vi> g(100010);
vi id(100010); vi lorder;

void dfs(int u) {
	seen[u]=1;
	dp[u]=1;
	for(int v: g[u]) {
		if(!seen[v]) {
			dfs(v);
			dp[u]*=(dp[v]+1);
			dp[u]%=MOD;
		}
	}
}

void dfs1(int u, SegmentTree& st) {
	seen[u]=1;
	int lo=-1;
	for(int i = 0; i < sz(g[u]); i++) {
		int v=g[u][i];
		if(!seen[v]) {
			if(lo==-1) lo=id[v];
			int hi=lo+sz(g[u])-1;
			if(u!=0) hi--;
			int mid=id[v];
			int oldu=dp[u],oldv=dp[v];
			dp[u]=1;
			if(parent[u]!=u) dp[u] *= (dp[parent[u]]+1);
			if(lo<=mid-1) dp[u] *= st.RMQ(lo,mid-1).ss;
			dp[u]%=MOD;
			if(hi>=mid+1) dp[u] *= st.RMQ(mid+1,hi).ss;
			dp[u] %=MOD;
			dp[u]+=MOD;
			dp[u]%=MOD;
			dp[v]=(dp[u]+1)*dp[v];
			dp[v]%=MOD;
			dp[v]+=MOD;
			dp[v]%=MOD;
			st.Update(id[u],id[u],((dp[u]-oldu)%MOD+MOD)%MOD);
			st.Update(id[v],id[v],((dp[v]-oldv)%MOD+MOD)%MOD);
			ans[v]=dp[v];
			dfs1(v,st);
			st.Update(id[u],id[u],((oldu-dp[u])%MOD+MOD)%MOD);
			st.Update(id[v],id[v],((oldv-dp[v])%MOD+MOD)%MOD);
			dp[u]=oldu,dp[v]=oldv;
		}
	}
}
signed main(void)
{
	IOS;
	int n,m; cin>>n>>m;
	MOD=m;
	for(int i = 0; i < n-1; i++) {
		int u,v; cin>>u>>v;
		g[u-1].pb(v-1);
		g[v-1].pb(u-1);
	}
	queue<int> q;
	q.push(0);
	lorder.pb(0);
	id[0]=0;
	seen[0]=1;
	while(!q.empty()) {
		int u = q.front(); q.pop();
		for(int v: g[u]) {
			if(!seen[v]) {
				seen[v]=1;
				lorder.pb(v);
				id[lorder.back()]=sz(lorder)-1;
				parent[v]=u;
				q.push(v);
			}
		}
	}
	memset(seen,0,sizeof(seen));
	dfs(0);
	for(int i = 0; i < n; i++) {
		dp1[i]=dp[lorder[i]];
	}
	vi tmp(n);
	for(int i = 0; i < n; i++)
		tmp[i]=dp1[i]+1;
	SegmentTree st(tmp);

	memset(seen,0,sizeof(seen));
	ans[0]=dp[0];
	dfs1(0,st);
	for(int i = 0; i  < n; i++)
		cout<<ans[i]<<endl;
	return 0;
}

Measured memory_bytes: 35700