#include <string>
#include <vector>
#include <algorithm>
#include <numeric>
#include <set>
#include <map>
#include <queue>
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <cctype>
#include <cassert>
#include <limits>
#include <functional>
#include <iomanip>
#include <complex>
#include <bitset>
#include <stack>
 
using namespace std;
 
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define rer(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i))
#define reu(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i))
#if defined(_MSC_VER) || __cplusplus > 199711L
#define aut(r,v) auto r = (v)
#else
#define aut(r,v) __typeof(v) r = (v)
#endif
#define each(it,o) for(aut(it, (o).begin()); it != (o).end(); ++ it)
#define ktya(x) sort(all(x))
#define maxs(x,y) (x = max(x,y))
#define mins(x,y) (x = min(x,y))
#define all(o) (o).begin(), (o).end()
#define pb(x) push_back(x)
#define mp(x,y) make_pair((x),(y))
#define mset(m,v) memset(m,v,sizeof(m))
#define INF 0x3f3f3f3f3f3f3f3fLL
#define SIZE 200105
#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
 
typedef vector<int> vi; typedef pair<int, int> pii; typedef vector<pair<int, int> > vpii; typedef long long ll;
typedef pair<int,ll> pill; typedef pair<ll,int> plli; 
typedef pair<double, int> pdi;
template<typename T, typename U> inline void amin(T &x, U y) { if(y < x) x = y; }
template<typename T, typename U> inline void amax(T &x, U y) { if(x < y) x = y; }
typedef complex<double> P;
ll MOD = 1000000007;
//ll MOD=998244353;
typedef ll Weight;
struct Edge {
	int src, dst;
	Weight weight;
};
bool operator < (const Edge &e, const Edge &f) {
	return e.weight != f.weight ? e.weight > f.weight : // !!INVERSE!!
	e.src != f.src ? e.src < f.src : e.dst < f.dst;
}
typedef vector<Edge> Edges;
ll X[114514];

vi G[114514];
vi G_kon[114514];
int N;
ll M;
ll dp[114514]={0};
ll dp_up[114514]={0};
bool used[114514];
void solve(int now){
	dp[now]=1;
	rep(i,G[now].size()){
		int nex=G[now][i];
		if(!used[nex]){
			used[nex]=true;
			G_kon[now].pb(nex);
			solve(nex);
			dp[now]=(dp[now]*(dp[nex]+1))%M;
		}
		
	}
}
void solve2(int oya){
	int sz = G_kon[oya].size();
	ll X[sz];
	ll Y[sz];
	//cout<<oya<<endl;
	for(int i=0;i<sz;i++){
		int ch = G_kon[oya][i];
		if(i==0){
			X[i]=dp[ch]+1;
		}else{
			X[i]=(X[i-1]*(dp[ch]+1))%M;
		}
	}
	for(int i=sz-1;i>=0;i--){
		int ch = G_kon[oya][i];
		if(i==sz-1){
			Y[i]=dp[ch]+1;
		}else{
			Y[i]=(Y[i+1]*(dp[ch]+1))%M;
		}
	}
	rep(i,sz){
		int ch = G_kon[oya][i];
		dp_up[ch]=dp_up[oya]+1;
		if(i>0){
			dp_up[ch]*=X[i-1];
			dp_up[ch]%=M;
		}
		if(i<sz-1){
			dp_up[ch]*=Y[i+1];
			dp_up[ch]%=M;
		}
		//cout<<ch<<" "<<dp_up[ch]<<endl;
		solve2(ch);
	}
}
int main(){
	cin>>N>>M;
	rep(i,N-1){
		int x,y;
		cin>>x>>y;--x;--y;
		G[x].pb(y);
		G[y].pb(x);
	}
	rep(i,N)used[i]=false;
	used[0]=true;
	solve(0);

	/*rep(i,N){
		rep(j,G_kon[i].size())cout<<G_kon[i][j]+1<<" ";
		cout<<endl;
	}*/
	
	dp_up[0]=0;
	solve2(0);
	//cout<<endl;
	rep(i,N){
		cout<<(dp[i]*(dp_up[i]+1))%M<<endl;
		//cout<<dp[i]<<" "<<(dp_up[i]+1)%M<<endl;
	}
}

Measured memory_bytes: 23296