#include <bits/stdc++.h>

#define pb push_back
#define mp make_pair
#define inside sl<=l&&r<=sr
#define outside r<sl||sr<l
#define INF 1000000009
//#define mod 1000000007
using namespace std;
typedef long long ll;

ll n, mod;
list<int> adj[100005];
ll dpust[100005];
ll dpalt[100005];
ll dp3[100005];

ll phi;

ll par[100005];

ll us(ll a, ll b){
	ll ans = 1;
	while (b){
		if (b & 1){
			ans *= a;
			ans %= mod;
		}
		a *= a;
		a %= mod;
		b >>= 1;
	}
	return ans;
}

ll bol(ll a, ll b){
	a %= mod;
	b %= mod;
	if (a == 0 && b == 0) return 0;
	ll gcd = __gcd(a, b);
	a /= gcd;
	b /= gcd;
	assert(__gcd(b, mod) == 1);
	ll ans = a*us(b, phi-1);
	ans %= mod;
	return ans;
}
 
ll calphi(ll a){
	ll ans = a;
	for (int i = 2; i <= sqrt(a); i++){
		if (a % i == 0){
			ans /= i;
			ans *= (i-1);
			while (a % i == 0) a /= i;
		}
	}
	if (a > 1){
		ans /= a;
		ans *= a-1;
	}
	return ans;
}

void dfs1(int node, int ata){
	par[node] = ata;
	for (int ch: adj[node]){
		if (ch == ata) continue;
		dfs1(ch, node);
	}
	
	ll ans = 1;
	vector<ll> grp;
	vector<ll> grpi;
	for (int ch: adj[node]){
		if (ch == ata) continue;
		grp.pb((1+dpalt[ch])%mod);
		grpi.pb(ch);
		ans *= 1 + dpalt[ch];
		ans %= mod;
	}

	int size = grp.size();
	if (size == 1){
		dp3[grpi[0]] = 1;
	} else if (size > 1){
		//cout << "size: " << size << endl;
		//cout << "grp: "; for (int i = 0; i < size; i++) cout << grp[i] << " "; cout << endl;
		
		vector<ll> pr(size);
		vector<ll> sf(size);
		pr[0] = grp[0];
		for (int i = 1; i < size; i++) pr[i] = (pr[i-1]*grp[i])%mod;
		//cout << "pr: "; for (int i = 0; i < size; i++) cout << pr[i] << " "; cout << endl;
		
		sf[size-1] = grp[size-1];
		for (int i = size-2; i >= 0; i--) sf[i] = (sf[i+1]*grp[i])%mod;
		//cout << "sf: "; for (int i = 0; i < size; i++) cout << sf[i] << " "; cout << endl;
		
		for (int i = 1; i < size-1; i++){
			dp3[grpi[i]] = (pr[i-1]*sf[i+1])%mod;
			//cout << "dp3[" << grpi[i] << "] = " << dp3[grpi[i]] << endl; 
		}
		dp3[grpi[0]] = sf[1];
		dp3[grpi[size-1]] = pr[size-2];
		//cout << "dp3[" << grpi[0] << "] = " << grpi[0] << endl;
		//cout << "dp3[" << grpi[size-1] << "] = " << grp[grpi[size-1]] << endl;
	}
	dpalt[node] = ans; 
}

void dfs2(int node, int ata){
	//cout << "dfs2(" << node << ", " << ata << ")" << endl;
 	//cout << "dfs2(" << node << ", " << ata << ")" << endl;
	if (ata == -1){
		dpust[node] = 1;
	} else {
		/*ll ans = dpust[ata];
		for (int ch: adj[ata]){
			if (ch == node) continue;
			if (ch == par[ata]) continue;
			ans *= 1+dpalt[ch];
			ans %= mod;
		}*/
		// ans = dpust[ata]*dpalt[ata]/(1+dpalt[node]) +1
		//cout << "kullan: " << "dp3[" << node << "]" << endl;
		dpust[node] = (dpust[ata]*dp3[node] + 1)%mod;
	}
	for (int ch: adj[node]){
		if (ch == ata) continue;
		dfs2(ch, node);
	}
}

int main(){
	//freopen("stl.gir", "r", stdin);
	
	cin >> n >> mod;
	for (int i = 0; i < n-1; i++){
		int u, v;
		cin >> u >> v;
		u--; v--;
		adj[u].pb(v);
		adj[v].pb(u);
	}
	
	//phi = calphi(mod);
	
	dfs1(0, -1);
	dfs2(0, -1);
	
	//for (int i = 0; i < n; i++) cout << dpalt[i] << " "; cout << endl;
	//for (int i = 0; i < n; i++) cout << dpust[i] << " "; cout << endl;
	
	for (int i = 0; i < n; i++){
		cout << (dpalt[i]*dpust[i])%mod << endl;
	}
	
}

Measured memory_bytes: 22272