#include<cstdio>
#include<cmath>
#include<iostream>
#include<cstring>
#include<ctime>
#include<map>
#include<queue>
#include<cctype>
#include<cstdlib>
#include<algorithm>
#include<vector>
#include<stack>

using namespace std;

	#define beg 131072


	long long m;
	vector < int > conn[100900];
	vector < pair < int , int > > obrada;
	long long solius[100900];
	
	int depth[100900];
	
	long long segment[2*beg + 100];
	
	struct subtree{
		
		long long down , up;
		int idxl , idxr , idx;
		
	} nodes[100900];

	void cin_conn(){
		
		int x , y; cin >> x >> y;
		conn[x].push_back(y);
		conn[y].push_back(x);
		return;
		
	}
	
	void build_tree(int node){
		
		obrada.push_back(make_pair(depth[node],node));
		
		for(int i = 0; i < conn[node].size(); i++){
			
			if(depth[conn[node][i]] == 0){
				depth[conn[node][i]] = depth[node] + 1;
				build_tree(conn[node][i]);
			}
			
		}
		
		return;
		
	}
	
	long long getumn(int a , int b){
		
		a += beg;
		b += beg;
		
		long long umn = 1;
		
		while(a <= b){
			
			if(a%2 != 0){
				
				umn*=segment[a]; umn%=m;
				a++;
				
			}
			if(b%2 == 0){
				
				umn*=segment[b]; umn%=m;
				b--;
				
			}
			b/=2; a/=2;
			
		}
		
		return umn%m;
		
	}
	
	

int main(){
	
	ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);

	int n; 
	cin >> n >> m;
	
	for(int i = 0; i < n - 1; i++) cin_conn();
	for(int i = 1; i <= n; i++) nodes[i].down = 1;
	depth[1] = 1; build_tree(1);
	sort(obrada.begin(),obrada.end());
	
	
	for(int i = obrada.size()-1; i >= 0; i--){
		
		int dep = obrada[i].first;
		int nod = obrada[i].second;
		
		for(int j = 0; j < conn[nod].size(); j++){
			
			if(depth[conn[nod][j]] < dep) continue;
			
			nodes[nod].down *= nodes[conn[nod][j]].down + 1;
			nodes[nod].down %= m;
			
		}
	//	cout << dep << " " << nod << " " << nodes[nod].down << endl;
	//	system("pause");
	}
	
	int curr_idx = 2;
	nodes[1].idx = 1;
	nodes[1].up = 1;
	segment[beg + 1] = nodes[1].down;
	for(int i = 0; i < obrada.size(); i++){
		
		int dep = obrada[i].first;
		int nod = obrada[i].second;
		
		nodes[nod].idxl = curr_idx;
		
		for(int j = 0; j < conn[nod].size(); j++){
			
			if(depth[conn[nod][j]] < dep) continue;
			
			segment[beg + curr_idx] = nodes[conn[nod][j]].down + 1;
			nodes[conn[nod][j]].idx = curr_idx;
			curr_idx++;
			
		}
		
		nodes[nod].idxr = curr_idx - 1;
		
	//	cout << nodes[nod].idx <<endl << nodes[nod].idxl <<" " << nodes[nod].idxr << endl;
	//	system("pause");
		
	}
	
	for(int i = beg - 1; i > 0; i--) segment[i] = (segment[i*2]*segment[i*2+1])%m;
	
	for(int i = 0; i < obrada.size(); i++){
		
		int dep = obrada[i].first;
		int nod = obrada[i].second;

		//cout << nodes[nod].down << " " << nodes[nod].up << " " << nod << endl; system("pause");
		
		solius[nod] = nodes[nod].up * nodes[nod].down;
		solius[nod]%=m;
		
		for(int j = 0; j < conn[nod].size(); j++){
			
			if(depth[conn[nod][j]] < dep) continue;
			
			int ind = nodes[conn[nod][j]].idx;
			int nxt = conn[nod][j];
			nodes[nxt].up = ((getumn(nodes[nod].idxl , ind - 1) * getumn(ind + 1, nodes[nod].idxr))%m * nodes[nod].up + 1)%m;
			// greska je bila sto se jedan na up dodaje tek kad izracunam sve umnoske 
			
		}
		
	}
	
	for(int i = 1; i <= n; i++) cout << solius[i] << endl;
	
	
	

	return 0;
}


Measured memory_bytes: 18424