// #pragma comment(linker, "/stack:200000000")
// #pragma GCC optimize("Ofast,no-stack-protector")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
// #pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>

#ifdef PERVEEVM_LOCAL
    #define debug(x) std::cerr << (#x) << ":\t" << (x) << std::endl
#else
    #define debug(x) 238;
#endif

#define fastIO std::ios_base::sync_with_stdio(false); std::cin.tie(0); std::cout.tie(0)
#define NAME "File"

using ll = long long;
using ld = long double;

#ifdef PERVEEVM_LOCAL
    std::mt19937 rnd(238);
#else
    std::mt19937 rnd(std::chrono::high_resolution_clock::now().time_since_epoch().count());
#endif

const double PI = atan2(0.0, -1.0);
const int INF = 0x3f3f3f3f;
const ll LINF = (ll)2e18;
int MOD = 998244353;

int add(int a, int b, int mod = MOD) {
    if (a + b >= mod) {
        return a + b - mod;
    }
    return a + b;
}

int sub(int a, int b, int mod = MOD) {
    if (a - b < 0) {
        return a - b + mod;
    }
    return a - b;
}

int mul(int a, int b, int mod = MOD) {
    return (int)(1ll * a * b % mod);
}

int binpow(int a, int n, int mod = MOD) {
    if (n == 0) {
        return 1;
    }
    if (n % 2 == 0) {
        return binpow(mul(a, a, mod), n / 2, mod);
    } else {
        return mul(binpow(a, n - 1, mod), a, mod);
    }
}

int rev(int a, int mod = MOD) {
    return binpow(a, mod - 2, mod);
}

const int N = 100100;

std::vector<int> g[N];
int ans[N];
int dp[N];

void calcDP(int v, int p = -1) {
    dp[v] = 1;

    for (auto to : g[v]) {
        if (to != p) {
            calcDP(to, v);
            dp[v] = mul(dp[v], add(dp[to], 1));
        }
    }
}

void calcANS(int v, int p = -1, int up = 0) {
    ans[v] = add(up, 1);

    std::vector<int> go;
    for (auto to : g[v]) {
        if (to != p) {
            go.push_back(to);
            ans[v] = mul(ans[v], add(dp[to], 1));
        }
    }

    std::vector<int> pref(go.size()), suff(go.size());

    for (int i = 0; i < (int)go.size(); ++i) {
        int to = go[i];
        pref[i] = add(dp[to], 1);

        if (i != 0) {
            pref[i] = mul(pref[i], pref[i - 1]);
        }
    }

    for (int i = (int)go.size() - 1; i >= 0; --i) {
        int to = go[i];
        suff[i] = add(dp[to], 1);

        if (i != (int)go.size() - 1) {
            suff[i] = mul(suff[i], suff[i + 1]);
        } 
    }

    for (int i = 0; i < (int)go.size(); ++i) {
        int curUp = add(up, 1);
        if (i != 0) {
            curUp = mul(curUp, pref[i - 1]);
        }
        if (i != (int)go.size() - 1) {
            curUp = mul(curUp, suff[i + 1]);
        }

        calcANS(go[i], v, curUp);

    }
}

void run() {
    int n;
    scanf("%d%d", &n, &MOD);

    for (int i = 0; i < n - 1; ++i) {
        int from, to;
        scanf("%d%d", &from, &to);

        g[from - 1].push_back(to - 1);
        g[to - 1].push_back(from - 1);
    }

    calcDP(0);
    calcANS(0);

    for (int i = 0; i < n; ++i) {
        printf("%d\n", ans[i]);
    }
}

int main(void) {
    // freopen(NAME".in", "r", stdin);
    // freopen(NAME".out", "w", stdout);

    auto start = std::chrono::high_resolution_clock::now();
    run();
    auto end = std::chrono::high_resolution_clock::now();

    #ifdef PERVEEVM_LOCAL
        std::cerr << "Execution time: "
                  << std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count()
                  << " ms" << std::endl;
    #endif

    return 0;
}


Measured memory_bytes: 22144