#include "bits/stdc++.h"
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector<vi>;
using vvl = vector<vl>;
const int INF = 1 << 28;
ll MOD = 1000000007;
template <class T>
bool chmax(T &a, const T &b) {
    return (a < b) ? (a = b, 1) : 0;
}
template <class T>
bool chmin(T &a, const T &b) {
    return (b < a) ? (a = b, 1) : 0;
}

struct mint {
    ll v;
    ll mod;
    mint() : v(0) {}
    mint(signed v, ll mod = MOD) : v(v), mod(mod) {}
    mint(ll t, ll mod = MOD) : mod(mod) {
        v = t % mod;
        if (v < 0)
            v += mod;
    }

    mint pow(ll k) {
        mint res(1), tmp(v);
        while (k) {
            if (k & 1)
                res *= tmp;
            tmp *= tmp;
            k >>= 1;
        }
        return res;
    }

    static mint add_identity() { return mint(0); }
    static mint mul_identity() { return mint(1); }

    mint inv() { return pow(mod - 2); }

    mint &operator+=(mint a) {
        v += a.v;
        if (v >= mod)
            v -= mod;
        return *this;
    }
    mint &operator-=(mint a) {
        v += mod - a.v;
        if (v >= mod)
            v -= mod;
        return *this;
    }
    mint &operator*=(mint a) {
        v = 1LL * v * a.v % mod;
        return *this;
    }
    mint &operator/=(mint a) { return (*this) *= a.inv(); }

    mint operator+(mint a) const { return mint(v) += a; };
    mint operator-(mint a) const { return mint(v) -= a; };
    mint operator*(mint a) const { return mint(v) *= a; };
    mint operator/(mint a) const { return mint(v) /= a; };

    mint operator-() const { return v ? mint(MOD - v) : mint(v); }

    bool operator==(const mint a) const { return v == a.v; }
    bool operator!=(const mint a) const { return v != a.v; }
    bool operator<(const mint a) const { return v < a.v; }
};

void dfs1(int curr, int par, vvl const &edge, vector<mint> &dp) {
    dp[curr] = 1;
    for (auto &to : edge[curr]) {
        if (to == par)
            continue;
        dfs1(to, curr, edge, dp);
        dp[curr] *= (dp[to] + 1);
    }
}

void dfs2(int curr, int par, vvl const &edge, vector<mint> &dp, vector<mint> &ret) {
    ret[curr] = 1;
    for (auto &t : edge[curr]) {
        ret[curr] *= dp[t] + 1;
    }
    int n = edge[curr].size();
    vector<mint> left(n, 1);
    vector<mint> right(n, 1);
    for (int i = 0; i < n; ++i) {
        left[i] = right[i] = dp[edge[curr][i]] + 1;
    }
    for (int i = 1; i < n; ++i) {
        left[i] *= left[i - 1];
    }
    for (int i = n - 2; i > 0; --i) {
        right[i] *= right[i + 1];
    }
    for (int i = 0; i < n; ++i) {
        if (edge[curr][i] == par)
            continue;
        dp[curr] = 1;
        if (i > 0)
            dp[curr] *= left[i - 1];
        if (i < n - 1)
            dp[curr] *= right[i + 1];
        dfs2(edge[curr][i], curr, edge, dp, ret);
    }
}

int main() {
    ll n;
    cin >> n >> MOD;
    vvl edge(n);
    for (int i = 0; i < n - 1; ++i) {
        int x, y;
        cin >> x >> y;
        --x;
        --y;
        edge[x].push_back(y);
        edge[y].push_back(x);
    }
    vector<mint> dp(n, 0);
    vector<mint> ret(n, 0);

    dfs1(0, -1, edge, dp);
    dfs2(0, -1, edge, dp, ret);
    for (int i = 0; i < n; ++i) {
        cout << ret[i].v << "\n";
    }
    return 0;
}

Measured memory_bytes: 24832