#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
//#define P pair<ll,ll>
#define FOR(I,A,B) for(ll I = ll(A); I < ll(B); ++I)
#define FORR(I,A,B) for(ll I = ll((B)-1); I >= ll(A); --I)
#define TO(x,t,f) ((x)?(t):(f))
#define SORT(x) (sort(x.begin(),x.end())) // 0 2 2 3 4 5 8 9
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin()) //xi>=v  x is sorted
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin()) //xi>v  x is sorted
#define NUM(x,v) (POSU(x,v)-POSL(x,v))  //x is sorted
#define REV(x) (reverse(x.begin(),x.end())) //reverse
ll gcd(ll a,ll b){if(a%b==0)return b;return gcd(b,a%b);}
ll lcm(ll a,ll b){ll c=gcd(a,b);return ((a/c)*(b/c)*c);}
#define NEXTP(x) next_permutation(x.begin(),x.end())
const ll INF=ll(1e16)+ll(7);
const ll MOD=1000000007LL;
#define out(a) cout<<fixed<<setprecision((a))


template <typename T>
class Seg_Tree{
public: // 0-index
	vector<T> dat;
	T initial,M;
	int n;
	T unite(T a,T b){//
		return (a*b) % M;
	}
	Seg_Tree(int n0_=1,T initial_=1,T M_=1){
		initsize(n0_,initial_,M_);
	}
	void initsize(int n0,T initial_,T M_){
		M = M_;
		initial = initial_;
		int k=1;
		while(1){
			if(n0<=k){
				n=k;
				dat.resize(2*n-1);
				for(int i=0;i<2*n-1;i++)dat[i]=initial;
				break;
			}
			k*=2;
		}
	}

	//i banme wo x nisuru
	void update(int i,T x){
		i += n-1;
		dat[i] = x;
		while(i>0){
			i = (i-1) / 2;
			dat[i] = unite(dat[i*2+1],dat[i*2+2]);
		}
	}

	//[a,b)
	T query0(int a,int b,int k,int l,int r){
		if(r<=a || b<=l)return initial;
		if(a<=l && r<=b)return dat[k];
		else{
			T vl = query0(a,b,k*2+1,l,(l+r)/2);
			T vr = query0(a,b,k*2+2,(l+r)/2,r);
			return unite(vl,vr);
		}
	}

	//return [a,b)
	T query(int a,int b){
		return query0(a,b,0,0,n);
	}

	T get_except(int ind){
		T vl = query(0,ind);
		T vr = query(ind+1,n);
		return unite(vl,vr);
	}

	T get_all(){
		return query(0,n);
	}
};

vector<int> leaf(const vector<int> &u,const vector<int> &v){
	int N = u.size() + 1;
	vector<int> G(N,0),res;
	for(int i=0;i<N-1;i++){
		G[u[i]]++;
		G[v[i]]++;
	}
	for(int i=0;i<N;i++){
		if(G[i] == 1){
			res.push_back(i);
		}
	}
	return res;
}

vector< pair<int,int> > leaf_to_root(const vector<int> &u,const vector<int> &v,const int root){
	// return <parent,child>
	int yet = 1000000000;
	vector<int> d(u.size()+1,yet);
	vector< pair<int,int> > res;
	queue<int> Q;
	Q.push(root);
	d[root] = 0;
	vector< vector<int> > G(u.size()+1);
	for(int i=0;i<u.size();i++){
		G[u[i]].push_back(v[i]);
		G[v[i]].push_back(u[i]);
	}
	while( Q.size() ){
		int now = Q.front();
		Q.pop();
		for(auto nex:G[now]){
			if(d[nex]!=yet)continue;
			d[nex] = d[now] + 1;
			Q.push(nex);
			res.push_back( make_pair(now,nex) );
		}
	}
	reverse(res.begin(), res.end());
	return res;
}


int main(){


	int N,M;
	cin >> N >> M;
	vector<int> x(N-1),y(N-1);
	FOR(i,0,N-1) cin >> x[i] >> y[i];
	FOR(i,0,N-1) x[i]--,y[i]--;




	vector< vector<int> > G(N);
	for(int i=0;i<N-1;i++){
		G[x[i]].push_back(y[i]);
		G[y[i]].push_back(x[i]);
	}
	vector< map<int,int> > ma(N);
	for(int i=0;i<N;i++){
		for(int j=0;j<G[i].size();j++){
			ma[ i ][ G[i][j] ] = j;
		}
	}
	vector<ll> val(N,1);

	vector< Seg_Tree<ll> > dp(N);
	for(int i=0;i<N;i++){
		dp[i].initsize(G[i].size(),1,M);
	}

	auto route = leaf_to_root(x,y,0);
	for(auto a:route){
		int p = a.first;
		int c = a.second;
		( val[p] *= val[c]+1LL ) %= M;
		dp[p].update(ma[p][c],(val[c]+1LL)%M );
	}

	reverse(route.begin(),route.end());
	for(auto a:route){
		int c = a.second;
		int p = a.first;
		ll v = dp[p].get_except(ma[p][c]);
		dp[c].update(ma[c][p],(v+1LL)%M);
	}

	for(int i=0;i<N;i++){
		cout << dp[i].get_all() << endl;
	}


}

Measured memory_bytes: 39544