#include<bits/stdc++.h>
#include<stdio.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef unsigned long long int lu;
typedef vector< int > vi;
typedef vector < ll > vl;
typedef set <ll> sl;
typedef vector< vi > vvi;
typedef vector< vl > vvl;
typedef pair< int,int > ii;
typedef pair< ll,ll > il;
#define sz(a) ll((a).size())
#define test(t) ll t;cin>>t;while(t--)
#define pb push_back
#define all(c) (c).begin(),(c).end()
#define tr(c,i) for(__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define present(c,x) ((c).find(x) != (c).end())
#define cpresent(c,x) (find(all(c),x) != (c).end())
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define Vi vector
#define pr pair
#define mpr make_pair
#define fi first
#define se second
#define isnum(a) (a>=48 && a<=57)
#define isS(a) (a>='a' && a<='z')
#define isU(a) (a>='A' && a<='Z')
#define toS(a) (isU(a)?a+32:a)
#define toU(a) (isS(a)?a-32:a)
#define toC(a) a&15

set <ll> s[100005];
ll d[100005];

vector<char> prime (100000+1, true);
void sieve(ll n)
{	
	//O(nloglogn) time
	prime[0] = prime[1] = false;
	for (ll i=2; i<=n; ++i)
	{
		if (prime[i])
		{
			s[i].insert(i);			
			for (ll j=2*i; j<=n; j+=i)
			{
				prime[j] = false;
				s[j].insert(i);
			}
		}
	}
}
#define forn(i,e) for(int i = 0; i < e; i++)
#define rep(i,a,b) for(i=a;i<b;i++)
#define REP(i,a,b) for(i=a;i<=b;i++)
#define reprev(i,a,b) for(i=a;i>=b;i--)

#define leadzero(a) __builtin_clz(a) //count leading zeros
#define trailzero(a) __builtin_ctz(a) //count trailing zeros
#define bitcount(a) __builtin_popcount(a) // count set bits (add ll)
#define ln cout<<"\n"
#define sp cout<<" "
#define maxe(a) *max_element(a.begin(),a.end())
#define maxi(a) max_element(a.begin(),a.end())-a.begin()
#define mine(a) *min_element(a.begin(),a.end())
#define mini(a) min_element(a.begin(),a.end())-a.begin()
ll gcd(ll a, ll b) { if(b == 0) return a; return gcd(b, a % b); }
ll fastpowMOD(ll a, ll p,ll MOD){ if(p==0) return 1; ll z = fastpowMOD(a,p/2,MOD); z = (z*z)%MOD; if(p%2) z = (z*a)%MOD; return z; }
ll fastpow(ll a, ll p){ if(p==0) return 1; ll z = fastpow(a,p/2); z = (z*z); if(p%2) z = (z*a); return z; }

ll search1(ll a[], ll f,ll n)
{
	ll left=1;
	ll right=n+1;
	while(right-left>1)
	{
		ll mid = left + (right-left)/2;
		if(a[mid]<f)
		{
			left=mid;
		}
		else right=mid;
	}
	return a[left];
}
ll n,m;
vl v[100003];
vector<ll> downdp;
vector<ll> updp;
vector<ll> grey;

ll down(ll vertex,ll parent,ll gp)
{
	//if(downdp[vertex]!=-1)return downdp[vertex];
	downdp[vertex]=1;
	vl g;
	for(ll x : v[vertex])
	{
		if(x==parent)continue;
		//down(x,vertex,parent);
		//downdp[vertex]*=(1+downdp[x])%m;
		downdp[vertex]*=(1+down(x,vertex,parent))%m;
		downdp[vertex] = downdp[vertex]%m;
		g.pb(x);
	}
	if(!g.empty())
	{
		ll pre[g.size()];
		ll aft[g.size()];
		pre[0]=aft[g.size()-1]=1;
		for(ll i = 1;i<g.size();i++)
		{
			pre[i]=(pre[i-1]*(1+downdp[g[i-1]]))%m;
			aft[g.size()-i-1]=(aft[g.size()-i]*(1+downdp[g[g.size()-i]]))%m;
		}
		for(ll i=0;i<g.size();i++)
		{
			grey[g[i]]=(pre[i]*aft[i])%m;
		}
	}
	return downdp[vertex];
}
void up(ll vertex,ll parent,ll gp)
{
	//if(updp[vertex]!=-1)return;
	if(parent==0){updp[vertex]=1;}
	else
	{
		//updp[vertex]=(1+updp[parent]*grey[vertex])%m;
		updp[vertex]=1;
		ll prod=updp[parent];
		prod*=grey[vertex]%m;
		updp[vertex]+=prod%m;
		updp[vertex]%=m;
	}
	for(ll x : v[vertex])
	{
		if(x==parent)continue;
		up(x,vertex,parent);
	}
}

int main(){
	ll i,j;
	fastio;
	cin >> n >> m;
	for(i=0;i<n-1;i++)
	{
		ll x,y;
		cin >> x >> y;
		v[x].pb(y);
		v[y].pb(x);
	}
	downdp.resize(n+1,-1);
	updp.resize(n+1,-1);
	grey.resize(n+1,-1);
	down(1,0,0);
	up(1,0,0);
	/*for(i=1;i<=n;i++)
	{
		cout << downdp[i] << " " << updp[i] << " " << grey[i] << endl;
	}*/
	for(i=1;i<=n;i++)
	{
		cout << (downdp[i]*updp[i])%m << endl;
	}
	return 0;
}

Measured memory_bytes: 22784