/*input
10 2
8 5
10 8
6 5
1 5
4 8
2 10
3 6
9 2
1 7

*/
/*

**************************
*                        *
* AUTHOR :: knight_coder *
*                        *
**************************

*/
#include<bits/stdc++.h>
#define lld          long long int
#define pb          push_back
#define ppb         pop_back
#define	endl		'\n'
#define mii         map<lld,lld>
#define msi         map<string,lld>
#define mis         map<lld, string>
#define mpi         map<pair<lld ,lld >,lld >
#define pii         pair<lld,lld>
#define vi          vector<lld>
#define vpi 		vector<pii>
#define vs          vector<string>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define sz(x)       (lld)x.size()
#define hell        1000000007
#define rep(i,m,n)	for(lld i=m;i<n;i++)
#define lbnd        lower_bound
#define ubnd        upper_bound
#define bs          binary_search
#define mp          make_pair
#define what_is(x)  cerr << #x << " is " << x << endl;
#define pr(x) 		cout<< x << " ";
#define pre(x)		cout<< x << endl ;
using namespace std;
#define N  100005
lld n,m;
vi v[N];
vi vv[N];
bool vis[N];
lld dp[N][2],dp2[N][2];
lld hell2;
lld valll[N];
vi dist[N];
vi dist2[N];
vi dist3[N];
map <pii,lld> ordd;
void init()
{
	rep(i,0,N)
	{
		vis[i]=false;
		dp[i][0]=1;
		dp[i][1]=1;
		dp2[i][0]=1;
		dp2[i][1]=1;
		v[i].clear();
	}
}
inline void dfs(lld node,lld flag,lld par)
{
	lld ans=1;
	lld ans2=1;
	//cout << node << " " << par << endl;
	for(auto it:v[node])
	{
		if(it!=par)
		{
			if(!flag)
			{
				dfs(it,0,node);
				ans2=(ans2*(dp[it][0]+dp[it][1]))%hell2;
			}
		}
	}
	vv[par].pb(node);
	dp[node][1]=ans%hell2;
	dp[node][0]=(ans2)%hell2;
	dist[par].pb((dp[node][1]+dp[node][0])%hell2);
	dist2[par].pb(1);
	dist3[par].pb(1);
}
inline void dfs2(lld node,lld par,lld ppar)
{
	lld ans=1;
	lld ans2=1;
	if(par!=0)
	{
		// for(auto it:v[par])
		// {
		// 	if(it!=node && it!=ppar)
		// 	{
		// 		ans2=(ans2*(dp[it][1]+dp[it][0]))%hell2;
		// 	}
		// }
		if(ordd[{par,node}]>0)
		ans2=(dist2[par][ordd[{par,node}]-1])%hell2;
		//cout << ans2 << " " ;
		if(ordd[{par,node}]<(vv[par].size()-1))
		ans2*=dist3[par][ordd[{par,node}]+1];
		ans2%=hell2;
		//cout << ans2 << " ";
		ans2=(ans2*(dp2[par][0]))%hell2;
		//cout << ans2 << endl;
		dp2[node][0]=(ans+ans2)%hell2;
	}
	for(auto it:v[node])
	{
		if(it!=par)
		{
			dfs2(it,node,par);
		}
	}
}
void solve()
{ 
	init();
	cin >> n >> m;
	rep(i,1,n)
	{
 		lld x,y;
		cin >> x >> y;
		v[x].pb(y);
		v[y].pb(x);
	}
	hell2=m;
	dfs(1,0,0);
	rep(i,1,n+1)
	{
		lld cnt=0;
		rep(j,0,vv[i].size())
		{
			ordd[{i,vv[i][j]}]=cnt;
			cnt++;
		}
	}
	rep(i,1,n+1)
	{
		if(dist[i].size()==0)
			continue;
		if(dist[i].size()>0)
			dist2[i][0]=dist[i][0];
		for(lld j=1;j<dist[i].size();j++)
		{
			dist2[i][j]=dist2[i][j-1]*dist[i][j];
			dist2[i][j]%=hell2;
		}
		if(dist[i].size()>0)
			dist3[i][dist[i].size()-1]=dist[i][dist[i].size()-1];
		if(dist[i].size()>1)
		for(lld j=(dist[i].size()-2);j>=0;j--)
		{
			dist3[i][j]=dist3[i][j+1]*dist[i][j];
			dist3[i][j]%=hell2;
		}
	}
	// rep(i,1,n+1)
	// {
	// 	rep(j,0,dist[i].size())
	// 	{
	// 		cout << dist[i][j] << " " << dist2[i][j] << " " << dist3[i][j] << endl;
	// 	}
	// 	cout << endl;
	// }
	dp2[0][0]=1;
	dp2[0][1]=1;
	dfs2(1,0,-1);
	// rep(i,1,n+1)
	// {
	// 	//cout << dp[i][0] << " " << dp[i][1] << " " << dp2[i][0] <<" "<< dp2[i][1]<<endl;
	// }
	rep(i,1,n+1)
	{
		cout << (dp[i][0]*dp2[i][0])%hell2 << endl;
	}
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int tests=1;
	//cin>>tests;
	while(tests--)
	{
		solve();
	}
}

Measured memory_bytes: 41856