#include <bits/stdc++.h>

using namespace std;
using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
template<class T> using V = vector<T>;
template<class T> using VV = V<V<T>>;
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); }

template <class T> ostream& operator<<(ostream& os, const V<T>& v) {
	os << "[";
	for (auto d : v) os << d << ", ";
	return os << "]";
}

template <class T, class U>
ostream& operator<<(ostream& os, const pair<T, U>& p) {
	return os << "P(" << p.first << ", " << p.second << ")";
}

class GModInt {
public:
	static int Mod;	int x; GModInt() : x(0) { }GModInt(signed sig) { if ((x = sig % Mod + Mod) >= Mod) x -= Mod; }GModInt(signed long long sig) { if ((x = sig % Mod + Mod) >= Mod) x -= Mod; }int get() const { return x; }	GModInt &operator+=(GModInt that) { if ((x += that.x) >= Mod) x -= Mod; return *this; }	GModInt &operator-=(GModInt that) { if ((x += Mod - that.x) >= Mod) x -= Mod; return *this; }GModInt &operator*=(GModInt that) { x = (unsigned long long)x * that.x % Mod; return *this; }GModInt &operator/=(GModInt that) { return *this *= that.inverse(); }GModInt operator+(GModInt that) const { return GModInt(*this) += that; }GModInt operator-(GModInt that) const { return GModInt(*this) -= that; }GModInt operator*(GModInt that) const { return GModInt(*this) *= that; }GModInt operator/(GModInt that) const { return GModInt(*this) /= that; }GModInt inverse() const { long long a = x, b = Mod, u = 1, v = 0; while (b) { long long t = a / b; a -= t * b; std::swap(a, b); u -= t * v; std::swap(u, v); }return GModInt(u); }bool operator==(GModInt that) const { return x == that.x; }bool operator!=(GModInt that) const { return x != that.x; }GModInt operator-() const { GModInt t; t.x = x == 0 ? 0 : Mod - x; return t; }
};
int GModInt::Mod = 0;
typedef GModInt mint;


template <class N, class E> struct AllTree {
	int n;
	const VV<E>& g;
	V<N> sm;
	VV<N> dp; // tree
	void dfs1(int p, int b) {
		sm[p] = N();
		for (auto e : g[p]) {
			int d = e.to;
			if (d == b) continue;
			dfs1(d, p);
			sm[p] = sm[p] + sm[d].to_subs(p, e);
		}
		sm[p] = sm[p].join(p);
	}
	void dfs2(int p, int b, N top) {
		int deg = int(g[p].size());
		dp[p] = V<N>(deg + 1);
		dp[p][0] = N();
		for (int i = 0; i < deg; i++) {
			int d = g[p][i].to;
			dp[p][i + 1] = dp[p][i] + (d == b ? top : sm[d]).to_subs(p, g[p][i]);
		}
		N rnode = N();
		dp[p].back() = dp[p].back().join(p);
		for (int i = deg - 1; i >= 0; i--) {
			dp[p][i] = (dp[p][i] + rnode).join(p);
			int d = g[p][i].to;
			if (d != b) dfs2(d, p, dp[p][i]);
			rnode = rnode + (d == b ? top : sm[d]).to_subs(p, g[p][i]);
		}
	}
	AllTree(const VV<E>& _g) : n(int(_g.size())), g(_g), sm(n), dp(n) {
		dfs1(0, -1);
		dfs2(0, -1, N());
	}
};


//TODO : add sample, calc radius of tree
struct Node {
	//Educational DP Contest V - Subtree
	mint sm = mint(1);
	template <class E> Node to_subs(int, const E&) const {
		//tree -> subtrees
		return { sm + 1 };
	}
	Node operator+(const Node& r) const {
		//subtrees + subtrees
		return { sm * r.sm };
	}
	Node join(int) const {
		//subtrees -> tree
		return *this;
	}
};

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);

	int n, M;
	cin >> n >> M;
	GModInt::Mod = M;
	struct E { int to; };
	VV<E> g(n);
	for (int i = 0; i < n - 1; i++) {
		int a, b;
		cin >> a >> b; a--; b--;
		g[a].push_back(E{ b });
		g[b].push_back(E{ a });
	}
	AllTree<Node, E>tree(g);
	auto all_tree = tree.dp;

	for (int i = 0; i < n; i++) {
		cout << all_tree[i].back().sm.get() << "\n";
	}
	return 0;
}//


Measured memory_bytes: 23040