#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
#ifdef ONLINE_JUDGE
#define filename(_ST_)			   \
	freopen(_ST_ ".in", "r", stdin); \
	freopen(_ST_ ".out", "w", stdout);
#else
#define filename(_ST_)
#endif
template <typename T>
inline void read(T &x) {
	char ch = 0;
	x = 0;
	bool sign = false;
	while (ch < '0' || '9' < ch) sign |= ch == '-', ch = getchar();
	while ('0' <= ch && ch <= '9') x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar();
	x = sign ? -x : x;
}
template <typename T>
inline void print(T x) {
	static char outp[64];
	if (!x) {
		putchar('0');
		return;
	}
	if (x < 0)
		putchar('-'), x = -x;
	int tot = 0;
	while (x) outp[tot++] = x % 10 + '0', x /= 10;
	while (tot) putchar(outp[--tot]);
}
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef unsigned long long ull;
#define fst first
#define sed second
#define pb push_back
#define mp make_pair
#define rt register int
#define all(it) it.begin(), it.end()
#define rep(it, f, e) for (rt it = f; it <= e; ++it)
#define per(it, f, e) for (rt it = f; it >= e; --it)
struct Tree {
	struct Node {
		int stdl, stdr;
		ll mul;
		Node() : mul(1) {}
	};
	int n, p;
	vector<Node> tree;
	Tree() : n(0), p(0) {}
	Tree(int x, int y) {
		n = x + 1;
		p = y;
		tree.resize(n << 2);
		buildtree(1, 1, n);
	}
	inline void buildtree(int root, int l, int r) {
		tree[root].stdl = l;
		tree[root].stdr = r;
		if (l == r) return;
		int mid = (l + r) >> 1;
		buildtree(root << 1, l, mid);
		buildtree(root << 1 | 1, mid + 1, r);
	}
	inline void pushdown(int root) {
		tree[root << 1].mul = (tree[root << 1].mul * tree[root].mul) % p;
		tree[root << 1 | 1].mul = (tree[root << 1 | 1].mul * tree[root].mul) % p;
		tree[root].mul = 1;
	}
	inline void ride(int root, int l, int r, ll k) {
		if (r < tree[root].stdl || l > tree[root].stdr) return;
		if (l <= tree[root].stdl && tree[root].stdr <= r) {
			tree[root].mul = (tree[root].mul * k) % p;
			return;
		}
		pushdown(root);
		ride(root << 1, l, r, k);
		ride(root << 1 | 1, l, r, k);
	}
	inline ll query(int root, int l, int r) {
		if (r < tree[root].stdl || l > tree[root].stdr) return 0;
		if (l <= tree[root].stdl && tree[root].stdr <= r) return tree[root].mul;
		pushdown(root);
		return (query(root << 1, l, r) + query(root << 1 | 1, l, r)) % p;
	}
};
const int MAXN = 1e5 + 7;
int n, m, dp[MAXN], res[MAXN];
vector<int> edge[MAXN];
Tree fval[MAXN];
inline void dfs(int w, int pre) {
	dp[w] = 1;
	fval[w] = Tree(edge[w].size(), m);
	rep (j, 0, (int)edge[w].size()-1) {
		int i = edge[w][j];
		if (i == pre) continue;
		dfs(i, w);
		dp[w] = (dp[i] + 1ll) * dp[w] % m;
		if (j) fval[w].ride(1, 1, j, dp[i]+1);
		if (j + 1 != (int)edge[w].size() ) fval[w].ride(1, j + 2, edge[w].size(), dp[i]+1);
	}
	rep (j, 0, (int)edge[w].size()-1) {
		int i = edge[w][j];
		if (i == pre) continue;
		res[i] = fval[w].query(1, j+1, j+1);
	}
}
inline void solve(int w, int pre) {
	if (w != 1) {
		dp[w] = (res[w] + 1) * 1ll * dp[w] % m;
		for (auto i : edge[w]) {
			res[i] = (res[w] + 1) * 1ll * res[i] % m;
		}
	}
	for (auto i : edge[w]) {
		if (i == pre) continue;
		solve(i, w);
	}
}
int main() {
	read(n);
	read(m);
	rep (i, 2, n) {
		int u, v;
		read(u);
		read(v);
		edge[u].pb(v);
		edge[v].pb(u);
	}
	dfs(1, 0);
	solve(1, 0);
	rep (i, 1, n) {
		print(dp[i]);
		putchar('\n');
	}
	return 0;
}

Measured memory_bytes: 45312