#include <bits/stdc++.h>

using namespace std;

#define PB	push_back
#define ALL(x)	(x).begin(), (x).end()
#define ALLR(x)	(x).rbegin(), (x).rend()
#define MP	make_pair

#define debug(args...) { string _s = #args; replace(ALL(_s), ',', ' '); stringstream ss(_s); cerr << endl;_debug(ss, args); }
#define _rg auto it = next(v.begin()); it != v.end(); it++
#define _input ostream& operator <<(ostream& out

template<typename T, typename U>
_input, const pair<T, U>& p) {
	out << "(" << p.first << ", " << p.second << ")";
	return out;
}

#define cont(c) template<typename T> _input, const c<T>& v) { \
					if (v.empty()) return out; \
					out << "[" << *v.begin(); \
					for (_rg) out << ", " << *it; \
					out << "]"; return out; \
				}
				
#define cont2(c) template<typename T, typename U> _input, const c<T, U>& v) { \
					if (v.empty()) return out; \
					out << "[" << *v.begin(); \
					for (_rg) out << ", " << *it; \
					out << "]"; return out; \
				}

cont(vector) cont(set) cont(multiset) cont(queue) cont2(map)

template<typename T, size_t n, typename= enable_if_t<!is_same<T, char>::value>>
_input, const T(&v)[n]) {
	if (n == 0) return out;
	out << "[" << v[0];
	for (int i = 1; i < (int)n; i++) out << ", " << v[i];
	out  << "]"; return out;
}

template<typename T, typename... Ts>
void _debug(stringstream& ss, const T &arg) {
	string name; ss >> name;
	cerr << name << " = " << arg << endl;
}

template<typename T, typename... Ts>
void _debug(stringstream& ss, const T& arg, const Ts&... args) {
	string name; ss >> name; 
	cerr << name << " = " << arg << ", ";
	if (is_class<T>::value || is_array<T>::value) cerr << endl;
	_debug(ss, args...);
}

#define FAST_IO ios::sync_with_stdio(false); cin.tie(0)

#define int long long int

typedef pair<int, int> ii;
typedef pair<pair<int, int>, int> iii;
typedef vector<int> vi;
typedef vector<iii> viii;
typedef vector<vi> vvi;
typedef vector<ii> vii;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef unsigned long long ull;

const int INF = LONG_LONG_MAX>>1;
const int NINF = LONG_LONG_MIN/2;
const double PI = 3.141592653589793238;

//MODULAR ARITHMATIC
int mmod = 1000000007;

int add(int a, int b, int m=mmod) {
	return (a%m + b%m)%m;
}

int sub(int a, int b, int m=mmod) {
    return (a%m - b%m + m)%m;
}

int mul(int a, int b, int m=mmod) {
	return ((a%m) * (b%m) % m);
}

int pwr(int a, int b, int m=mmod) {
    a %= m;
    int r = 1;
    while (b) {
		if (b&1) r = r*a%m;
		b >>= 1;
		a = a*a%m;
    }
    return r;
}

int inv(int a, int m=mmod) {
	return pwr(a, m-2, m);
}

#define all_white first
#define sth_is_black second

const int N = 100009;
vi adj[N], child[N];
ii down[N], up[N];
vii downs[N], pref[N], suf[N];

ii merge(const ii& a, const ii& b) {
	pair<int,int> p;
    p.all_white = mul(a.all_white, b.all_white);
    p.sth_is_black = add(
        mul(a.sth_is_black, add(b.all_white, b.sth_is_black)),
        mul(a.all_white, b.sth_is_black)
    );
    return p;
}

ii process(ii p) {
	return {p.all_white, add(p.sth_is_black, p.all_white)};
}

void dfs1(int v, int par) {
	ii p = {1, 0};
	for (int u: adj[v]) {
		if (u != par) {
			child[v].PB(u);
			dfs1(u, v);
			p = merge(p, down[u]);
			downs[v].PB(down[u]);
		}
	}
	down[v] = process(p);
}

void dfs2(int v, int par) {
	for (int i = 0; i < (int)child[v].size(); i++) {
		ii p{1, 0};
		if (i != 0) {
			p = merge(p, pref[v][i-1]);
		}
		if (i != (int) downs[v].size() - 1) {
			p = merge(p, suf[v][i+1]);
		}
		if (v != 1) {
			p = merge(p, up[v]);
		}
		int u = child[v][i];
		up[u] = process(p);
		dfs2(u, v);
	}
}

int32_t main() {
	FAST_IO;
	
	int n, m;
	cin >> n >> m;
	mmod = m;
	for (int i = 0; i < n-1; i++) {
		int x, y;
		cin >> x >> y;
	
		adj[x].PB(y);
		adj[y].PB(x);
	}
	
	dfs1(1, -1);
	
	for (int v = 1; v <= n; v++) {
		int k = downs[v].size();
		assert(k == (int) child[v].size());
		ii p{1, 0};
		for (int i = 0; i < k; i++) {
			p = merge(p, downs[v][i]);
			pref[v].PB(p);
		}
		p = {1, 0};
		for (int i = k-1; i >= 0; i--) {
			p = merge(p, downs[v][i]);
			suf[v].PB(p);
		}
		reverse(ALL(suf[v]));
	}
	dfs2(1, -1);
	for (int v = 1; v <= n; v++) {
		ii p{1, 0};
		if (v != 1) {
			p = merge(p, up[v]);
		}
		if (!child[v].empty()) {
			p = merge(p, pref[v].back());
		}
		p = process(p);
		cout << p.sth_is_black << endl;
	}
}

// g++ -Wall -Wextra -Wshadow -fsanitize=undefined -std=c++17


Measured memory_bytes: 37632