/*
 * Author    : raj1307 - Raj Singh
 * Date      : 22.07.2020
 */
 
#pragma GCC optimize("Ofast")
// #pragma GCC optimize("unroll-loops")
// #define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
using namespace std;

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
 
#define fio ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define fr(i, a, b) for(int i = (a); i < (b); ++i)
#define fb(i, b, a) for(int i = (b); i > (a); --i)
#define fill(x,y) memset(x,y,sizeof(x))
#define rr return 
#define mp make_pair
#define pb push_back
#define sz(x) ((int)x.size())
#define int long long
//#define ll long long
#define ld long double
#define S second
#define F first
#define endl "\n"
#define Endl "\n"
#define all(v) (v).begin(), (v).end()
typedef pair<int, int> pii;
int max(int a,int b){if(a>b) return a; else return b;}
int min(int a,int b){if(a>b) return b; else return a;}
int gcd(int a,int b){ return (b == 0) ? a : gcd(b, a % b);}
inline int lcm(int a,int b){ return a / gcd(a, b) * b;}
inline int normalize(int x,int mod){ x %= mod; if (x < 0) x += mod; return x;}
 
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
string abc="abcdefghijklmnopqrstuvwxyz";
const double PI=acosl(-1);
const int mod = 1000*1000*1000+7;
//const int mod = 998244353;
const int inf = 1e18 +5;
 
//a and b are assumed to be taken modulo p
inline int add(int a, int b, int p = mod){ int c = a + b; if(c >= p) c -= p; return c; }
inline int sub(int a, int b, int p = mod){ int c = a - b; if(c < 0) c += p; return c; }
inline int mul(int a, int b, int p = mod){ return (a * 1ll * b) % p; }

// template<typename T>
// using ordered_set = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>; 
//1. order_of_key(k) : number of elements strictly lesser than k
//2. find_by_order(k) : k-th element in the set
//To make it an ordered_multiset, use pairs of (value, time_of_insertion)
//to distinguish values which are similar.
 
int floor1(int n,int k){
    if(n%k == 0 || n >= 0)return n/k;
    return (n/k)-1;
}
 
int ceil1(int n,int k){
    return floor1(n+k-1,k);
}
 
int powm(int a, int b) {
	int res=1;
	while(b) {
		if(b&1)
			res=(res*a)%mod;
		a=(a*a)%mod;
		b>>=1;
	}
	return res;
}
 
 
const int N = 1e5 + 5;

int n,m;
int a[N];

vector<int>g[N],pre[N],suf[N];
int dp[N];
int ans[N];



void dfs1(int u, int par){
	dp[u]=1;
	for(int i:g[u]){
		if(i!=par){
			dfs1(i,u);
			dp[u]=mul(dp[u],dp[i]+1,m);
		}
	}
	pre[u].resize(g[u].size());
	suf[u].resize(g[u].size());

	fr(i,0,sz(g[u])){
		pre[u][i]=mul((i? pre[u][i-1]:1),(g[u][i]==par)? 1: (dp[g[u][i]]+1),m);
	}
	fb(i,sz(g[u])-1,-1){
		suf[u][i]=mul( ((i<sz(g[u])-1)? suf[u][i+1]: 1),(g[u][i]==par)? 1: (dp[g[u][i]]+1),m);
	}


}

void dfs2(int u, int par){

	if(par==-1)
		ans[u]=1;
	
	fr(i,0,sz(g[u])){

		if(g[u][i]!=par){

			int it=g[u][i];

			ans[it]= add( mul( ans[u], mul( i?pre[u][i-1]:1,(i==sz(g[u])-1)?1:suf[u][i+1], m) ,m), 1, m);

			dfs2(it,u);

		}
	}
}





void solve(){  
    	

	


	cin>>n>>m;
	fr(i,0,n-1){
		int u,v;
		cin>>u>>v;
		g[u].pb(v);
		g[v].pb(u);
	}


	dfs1(1,-1);
	dfs2(1,-1);

	


	fr(i,1,n+1) cout<<mul(ans[i],dp[i],m)<<endl;





   	











	
	








	

}
 
 
 
 
signed main() {
	fio;
	#ifndef ONLINE_JUDGE 
    	freopen("input.txt", "r", stdin); 
    	freopen("output.txt", "w", stdout); 
    	//freopen("error.txt", "w", stderr); 
	#endif 
	cout<<fixed<<setprecision(10);
	int t=1;
	//cin>>t;  // UNCOMMENT FOR TEST-CASES
	//precompute();
	while(t--) {
		solve();
	}	
	return 0;
}
 


Measured memory_bytes: 27216