#include <bits/stdc++.h>
using namespace std;
using lint = long long int;
using pint = pair<int, int>;
using plint = pair<lint, lint>;
struct fast_ios { fast_ios(){ cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_;
#define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++)
#define IFOR(i, begin, end) for(int i=(end)-1,i##_begin_=(begin);i>=i##_begin_;i--)
#define REP(i, n) FOR(i,0,n)
#define IREP(i, n) IFOR(i,0,n)
template<typename T> istream &operator>>(istream &is, vector<T> &vec){ for (auto &v : vec) is >> v; return is; }
template<typename T> ostream &operator<<(ostream &os, const vector<T> &vec){ os << "["; for (auto v : vec) os << v << ","; os << "]"; return os; }
template<typename T> ostream &operator<<(ostream &os, const deque<T> &vec){ os << "deq["; for (auto v : vec) os << v << ","; os << "]"; return os; }
template<typename T> ostream &operator<<(ostream &os, const set<T> &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; }
template<typename T> ostream &operator<<(ostream &os, const unordered_set<T> &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; }
template<typename T> ostream &operator<<(ostream &os, const multiset<T> &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; }
template<typename T> ostream &operator<<(ostream &os, const unordered_multiset<T> &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; }
template<typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &pa){ os << "(" << pa.first << "," << pa.second << ")"; return os; }
template<typename TK, typename TV> ostream &operator<<(ostream &os, const map<TK, TV> &mp){ os << "{"; for (auto v : mp) os << v.first << "=>" << v.second << ","; os << "}"; return os; }
template<typename TK, typename TV> ostream &operator<<(ostream &os, const unordered_map<TK, TV> &mp){ os << "{"; for (auto v : mp) os << v.first << "=>" << v.second << ","; os << "}"; return os; }
template<typename T> void ndarray(vector<T> &vec, int len) { vec.resize(len); }
template<typename T, typename... Args> void ndarray(vector<T> &vec, int len, Args... args) { vec.resize(len); for (auto &v : vec) ndarray(v, args...); }
template<typename T> void Max(T &m, const T q) { if (m < q) m = q; }
template<typename T> void Min(T &m, const T q) { if (m > q) m = q; }
template<typename T1, typename T2> pair<T1, T2> operator+(pair<T1, T2> &l, pair<T1, T2> &r) { return make_pair(l.first + r.first, l.second + r.second); }
template<typename T1, typename T2> pair<T1, T2> operator-(pair<T1, T2> &l, pair<T1, T2> &r) { return make_pair(l.first - r.first, l.second - r.second); }
#define dbg(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ") " << __FILE__ << endl;

int N, M;
constexpr int Nmax = 100000;
vector<lint> to[Nmax];
vector<lint> chi[Nmax];
vector<lint> prod[Nmax][2];
vector<lint> res;
vector<lint> oya;

lint dfs0(int now, int prv)
{
    for (auto nxt : to[now]) if (nxt != prv)
    {
        chi[now].push_back(nxt);
        prod[now][0].push_back((dfs0(nxt, now) + 1) % M);
    }

    reverse(prod[now][0].begin(), prod[now][0].end());
    prod[now][1] = {1};
    for (auto v : prod[now][0]) prod[now][1].push_back(prod[now][1].back() * v % M);
    reverse(prod[now][1].begin(), prod[now][1].end());

    prod[now][0].push_back(1);
    reverse(prod[now][0].begin(), prod[now][0].end());
    REP(i, prod[now][0].size() - 1) (prod[now][0][i + 1] *= prod[now][0][i]) %= M;
    return prod[now][0].back();
}

void dfs1(int now, int prv)
{
    REP(i, chi[now].size()) if (chi[now][i] != prv)
    {
        int nxt = chi[now][i];
        oya[nxt] = (prod[now][0][i] * prod[now][1][i + 1] % M * oya[now] + 1) % M;
        res[nxt] = prod[nxt][0].back() * oya[nxt] % M;
        dfs1(nxt, now);
    }
}

int main()
{
    cin >> N >> M;
    REP(_, N - 1)
    {
        int x, y;
        cin >> x >> y;
        to[x - 1].push_back(y - 1);
        to[y - 1].push_back(x - 1);
    }

    dfs0(0, -1);
    res.resize(N);
    res[0] = prod[0][0].back();
    oya.resize(N);
    oya[0] = 1;
    dfs1(0, -1);
    for (auto v : res) printf("%lld\n", v);
}


Measured memory_bytes: 31360