#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> P;
typedef pair<int,int> Pi;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define fi first
#define se second

template<class T>
inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return true;
    }
    return false;
}
template<class T>
inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return true;
    }
    return false;
}
template<typename T> ostream& operator<<(ostream& s,const complex<T>& d) {return s<<"("<<d.real()<<", "<<d.imag()<< ")";}
template<typename T1, typename T2> ostream& operator<<(ostream& s,const pair<T1,T2>& d) {return s<<"("<<d.first<<", "<<d.second<<")";}
template<typename T> ostream& operator<<(ostream& s, const vector<T>& d){
	int len=d.size();
	rep(i,len){
		s<<d[i];if(i<len-1) s<<" ";
	}
	return s;
}
template<typename T> ostream& operator<<(ostream& s,const vector<vector<T>>& d){
	int len=d.size();
	rep(i,len){
		s<<d[i]<<endl;
	}
	return s;
}
template<typename T> ostream& operator<<(ostream& s,const set<T>& v){
	s<<"{ ";
	for(auto itr=v.begin();itr!=v.end();++itr) {
		if (itr!=v.begin()) {s<< ", ";}
		s<<(*itr);
	}
	s<<" }";
	return s;
}
template<typename T1, typename T2> ostream& operator<<(ostream& s,const map<T1,T2>& m){
	s<<"{"<<endl;
	for(auto itr=m.begin();itr!=m.end();++itr){
		s<<" "<<(*itr).first<<" : "<<(*itr).second<<endl;
	}
	s<<"}"<<endl;
	return s;
}

int mod;
const ll inf=1e15;
const int INF=1e9;

struct mint {
    ll x;
    mint(ll x=0):x((x%mod+mod)%mod){}
    mint& operator+=(const mint a) {
        if ((x += a.x) >= mod) x -= mod;
        return *this;
    }
    mint& operator-=(const mint a) {
        if ((x += mod-a.x) >= mod) x -= mod;
        return *this;
    }
    mint& operator*=(const mint a) {
        (x *= a.x) %= mod;
        return *this;
    }
    mint operator+(const mint a) const {
        mint res(*this);
        return res+=a;
    }
    mint operator-(const mint a) const {
        mint res(*this);
        return res-=a;
    }
    mint operator*(const mint a) const {
        mint res(*this);
        return res*=a;
    }
    mint pow(ll t) const {
        if (!t) return 1;
        mint a = pow(t>>1);
        a *= a;
        if (t&1) a *= *this;
        return a;
    }
    // for prime mod
    mint inv() const {
        return pow(mod-2);
    }
    mint& operator/=(const mint a) {
        return (*this) *= a.inv();
    }
    mint operator/(const mint a) const {
        mint res(*this);
        return res/=a;
    }
};

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	int n;
	cin>>n>>mod;
	vector<vector<int>> g(n);
	rep(i,n-1){
		int x,y;
		cin>>x>>y;
		x--,y--;
		g[x].push_back(y);
		g[y].push_back(x);
	}
	vector<mint> dp(n),ans(n);
	function<mint(int,int)> dfs=[&](int v,int pre){
		mint ret=1;
		for(auto itr:g[v]){
			if(itr==pre) continue;
			ret=ret*(dfs(itr,v)+1);
		}
		return dp[v]=ret;
	};
	function<void(int,int)> dfs2=[&](int v,int pre){
		ans[v]=dp[v];
		vector<mint> pl(g[v].size()+1),pr(g[v].size()+1);
		mint now=1;
		pl[0]=1,pr[g[v].size()]=1;
		for(int i=0;i<g[v].size();i++){
			now*=(dp[g[v][i]]+1);
			pl[i+1]=now;
		}
		now=1;
		for(int i=g[v].size()-1;i>=0;i--){
			now*=(dp[g[v][i]]+1);
			pr[i]=now;
		}
		for(int i=0;i<g[v].size();i++){
			if(g[v][i]==pre) continue;
			mint prod=pl[i]*pr[i+1];
			mint now1=dp[v];
			mint now2=dp[g[v][i]];
			dp[v]=prod;
			dp[g[v][i]]=dp[g[v][i]]*(prod+1);

			dfs2(g[v][i],v);	
			dp[v]=now1;
			dp[g[v][i]]=now2;
		}
	};
	dfs(0,-1);
	dfs2(0,-1);
	rep(i,n){
		cout<<ans[i].x<<endl;
	}
}

Measured memory_bytes: 22016