// Author : Syed Mujtaba
// Seek knowledge from the Cradle to the Grave !!
// https://www.linkedin.com/in/syed-mujtaba

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> 
#include <ext/pb_ds/tree_policy.hpp> 
using namespace __gnu_pbds;
using namespace std;
#define int             long long
#define ld              long double
#define ll              long long
#define pb              push_back
#define fi              first
#define se              second
// #define mod             1000000007  //998244353// 1e9 + 7
#define inf             1000000007 // 1e9 + 7
#define MAX             10000007   // 1e7 + 7
#define test            int t; cin>>t; while(t--)
#define eps             1e-6
#define fast_io         ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define tree            tree <int, null_type, less <int>, rb_tree_tag, tree_order_statistics_node_update>
#define N 300005
#define mt make_tuple
// int fact[1000007]={0};
// int spf[(int)(1e6)];
// bool prime[(int)((1e7) + 20)];
// int gcd(int a,int b){if(b==0)return a; return gcd(b,a%b);}
// void sp(int n){spf[1]=1;for(int i=2;i<n;i++)spf[i]=i;for(int i=4;i<n;i+=2)spf[i]=2;for(int i=3;i*i<n;i++){if(spf[i]==i){for(int j=i*i;j<n;j+=i)if(spf[j]==j)spf[j]=i;}}}
// void sieve(int n){memset(prime,true,sizeof(prime) );for(int p=2;p*p<=n;p++){if(prime[p]==true)for(int i=p*p;i<=n;i+=p)prime[i]=false;}prime[0]=false;prime[1]=false;}
// int xyp(int x,int y){int res=1;while(y>0){if(y%2==1) res=(res*x)%mod;x=(x*x)%mod;y/=2;}return res%mod;}
// int inv(int x){return xyp(x,mod - 2);}
// void init() {fact[0]=1;fact[1]=1;for(int i=2;i<1000007;i++)fact[i]=(fact[i-1]*i)%mod;}
// int ncr(int n,int r) {int res=1; res=fact[n]; res=(res*(xyp(fact[r],mod-2)))%mod; res=(res*(xyp(fact[n-r],mod-2)))%mod; return res;}
// int npr(int n,int r) {int res=1; res=fact[n]; res=(res*(xyp(fact[n-r],mod-2)))%mod; return res; }
// bool isp(int a){if(a == 2)return true;if(a == 1)return false;for(int x= 2; x <= sqrt(a) + 1; x++)if(a % x == 0)return false;return true;}
int mod;
int add(int a, int b){return (a%mod + b%mod + mod)%mod;}
int sub(int a, int b){return (a%mod - b%mod + mod)%mod;}
int mul(int a, int b){return ((a%mod)*(b%mod) + mod)%mod;}
// freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
// mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
int gcd(int a, int b, int &x, int &y) // ax + by = gcd(a, b) // returns a single solution to ax + by = gcd(a, b) and if gcd(a, b) == 1 then x is modular inverse of a
{
	if(a == 0)
	{
		x = 0;
		y = 1;
		return b;
	}
	int x1, y1;
	int d = gcd(b%a, a, x1, y1);
	x = y1 - (b / a)*x1;
	y = x1;
	return d;
}
int modinv(int a, int b)
{
	int x, y;
	if(gcd(a, b, x, y) != 1)
		return -1; // Mod Inv Does not Exist
	return (x%b + b)%b;
}
vector < vector < int > > adj;
int dp[200005];
int ans[200005];
int n, m;
int dfs1(int u, int p)
{
	int F = 1;
	dp[u] = 1;
	for(auto z : adj[u])
	{
		if(z != p)
		{
			F = 0;
			dp[u] = mul(dp[u], add(1, dfs1(z, u)));
		}
	}
	return dp[u];
}
void dfs2(int u, int p)
{
	ans[u] = dp[u];
	for(auto z : adj[u])
	{
		if(z == p)
			continue;
		int temp2 = dp[u];
		dp[u] = mul(dp[u], modinv (add(1, dp[z]),   mod));
		int temp = dp[z];
		dp[z] = mul(dp[z], add(1, dp[u]));
		dfs2(z, u);
		dp[z] = temp;
		dp[u] = temp2;
	}
}
signed main()
{
	fast_io	
	cin >> n >> m;
	mod = m;
	adj.resize(n + 1);
	for(int i = 0; i < n - 1; i++)
	{ 	
		int x, y;
		cin >> x >> y;
		adj[x].pb(y);
		adj[y].pb(x);
	}
	dfs1(1, -1); // simple dfs to calculate for root node.
	dfs2(1, -1); // re-rooting
	for(int i = 1; i <= n; i++)
		cout << ans[i] << "\n";
	cout << endl;
}