#include <bits/stdc++.h>
using namespace std;
using ll = long long;

constexpr int inf = 0x3f3f3f3f;
constexpr ll linf = 0x3f3f3f3f3f3f3f3fLL;

class anymodint
{
public:
    anymodint(ll value = 0) : _value(value >= 0 ? value % mod() : (value % mod()) + mod()) {}

    static void setmod(ll value) { mod() = value; }

    anymodint operator+() const { return *this; }
    anymodint operator-() const { return anymodint(-_value); }

    anymodint &operator+=(const anymodint &rhs) {
        if ((_value += rhs._value) >= mod()) _value -= mod();
        return *this;
    }
    anymodint &operator-=(const anymodint &rhs) {
        if ((_value += mod() - rhs._value) >= mod()) _value -= mod();
        return *this;
    }
    anymodint &operator*=(const anymodint &rhs) {
        (_value *= rhs._value) %= mod();
        return *this;
    }
    anymodint &operator/=(const anymodint &rhs) {
        *this *= rhs.inv();
        return *this;
    }
    anymodint operator+(const anymodint &rhs) const { return anymodint(*this) += rhs; }
    anymodint operator-(const anymodint &rhs) const { return anymodint(*this) -= rhs; }
    anymodint operator*(const anymodint &rhs) const { return anymodint(*this) *= rhs; }
    anymodint operator/(const anymodint &rhs) const { return anymodint(*this) /= rhs; }

    anymodint operator++() { *this += 1; return *this; }
    anymodint operator--() { *this -= 1; return *this; }
    anymodint operator++(int) { anymodint tmp = *this; ++*this; return tmp; }
    anymodint operator--(int) { anymodint tmp = *this; --*this; return tmp; }

    bool operator==(const anymodint &rhs) const { return _value == rhs._value; }
    bool operator!=(const anymodint &rhs) const { return _value != rhs._value; }

    anymodint inv() const {
        ll a = _value, b = mod(), u = 1LL, v = 0LL;
        while (b > 0) {
            ll tmp = a / b;
            a -= tmp * b; swap(a, b);
            u -= tmp * v; swap(u, v);
        }
        return anymodint(u);
    }

    anymodint pow(ll n) const {
        anymodint e = *this, res = 1;
        while (n) {
            if (n & 1) res *= e;
            e *= e, n >>= 1;
        }
        return res;
    }

    friend ostream &operator<<(ostream &os, const anymodint &m) {
        os << m._value;
        return os;
    }

    friend istream &operator>>(istream &is, anymodint &m) {
        ll in;
        is >> in;
        m = anymodint(in);
        return is;
    }

private:
    ll _value;
    static ll &mod() { static ll _mod = linf; return _mod; }
};
using mint = anymodint;

int n, m;
vector<int> g[100000];

mint dp1[100000];
mint dp2[100000];

void dfs1(int i, int par = -1)
{
    for (auto j: g[i]) {
        if (j == par) continue;
        dfs1(j, i);
        dp1[i] *= (dp1[j] + 1);
    }
}

void dfs2(int i, int par = -1)
{
    if (par == -1) dp2[i] = 1;
    int size = g[i].size();
    vector<mint> l, r;
    l.emplace_back(1), r.emplace_back(1);
    // 左から累積和
    for (int j = 0; j < size; j++) {
        if (g[i][j] == par) continue;
        l.emplace_back((dp1[g[i][j]] + 1) * l.back());
    }
    l.pop_back();
    reverse(l.begin(), l.end());
    // 右から累積和
    for (int j = size - 1; j >= 0; j--) {
        if (g[i][j] == par) continue;
        r.emplace_back((dp1[g[i][j]] + 1) * r.back());
    }
    r.pop_back();

    for (auto j: g[i]) {
        if (j == par) continue;
        dp2[j] = dp2[i];
        dp2[j] *= l.back() * r.back();
        l.pop_back(), r.pop_back();
        dp2[j]++;
        dfs2(j, i);
    }

}

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

    cin >> n >> m;
    for (int i = 0; i < n - 1; i++) {
        int x, y; cin >> x >> y; x--, y--;
        g[x].emplace_back(y), g[y].emplace_back(x);
    }

    mint::setmod(m);
    fill(dp1, dp1 + n, 1);
    dfs1(0);
    dfs2(0);

    for (int i = 0;i < n; i++) {
        cout << dp1[i] * dp2[i] << '\n';
    }
    return 0;
}


Measured memory_bytes: 25472