#include<bits/stdc++.h>
using namespace std;
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); 
#define pi 3.1415926535897
#define ll long long int
#define ld long double
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define mod 1000000007
#define mod1 998244353
#define inf 1000000000000000000
#define inf1 1000000000
#define endl '\n'
ll n,m;
vector<ll> adj[100005];
ll dp_d[100005];
ll dp_u[100005];
void dfs(ll v,ll p)
{
	ll ans=1;
	for(auto it:adj[v])
	{
		if(it!=p)
		{
			dfs(it,v);
			ans=(ans*(dp_d[it]+1))%m;
		}
	}
	dp_d[v]=ans;
	return;
}
void dfs1(ll v,ll p)
{
	ll a=dp_u[v];
	for(auto it:adj[v])
	{
		if(it!=p)
		{
			dp_u[it]=(dp_u[it]*a)%m;
			a=(a*(1+dp_d[it]))%m;
		}
	}
	reverse(adj[v].begin(),adj[v].end());
	a=1;
	for(auto it:adj[v])
	{
		if(it!=p)
		{
			dp_u[it]=(dp_u[it]*a)%m;
			a=(a*(1+dp_d[it]))%m;
		}
	}
	for(auto it:adj[v])
	{
		if(it!=p)
		{
			dp_u[it]++;
			dfs1(it,v);
		}
	}
	return;
}
int main()
{
	fastio;
	#ifdef APNA_IO
		freopen("input.txt" , "rt" ,stdin);
		freopen("output.txt" , "wt" ,stdout);
	#endif
	cin>>n>>m;
	for(ll i=1;i<n;i++)
	{
		ll a,b;
		cin>>a>>b;
		adj[a].pb(b);
		adj[b].pb(a);
	}
	for(ll i=1;i<=n;i++)
		dp_u[i]=1;
	dfs(1,-1);
	dfs1(1,-1);
	for(ll i=1;i<=n;i++)
		cout<<(dp_u[i]*dp_d[i])%m<<endl;
	return 0;
}