#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)n; ++i)
#define rrep(i, n) for (int i = (int)n-1; i >= 0; --i)
using namespace std;
using ll = long long;
template<typename T>
inline bool chmax(T& a, const T& b) {
    if (a < b){
        a = b;
        return true;
    }
    return false;
}
template<typename T>
inline bool chmin(T& a, const T& b) {
    if (b < a) {
        a = b;
        return true;
    }
    return false;
}
template<typename T> vector<T> make_vec(size_t n) { return vector<T>(n); }
template<typename T, typename... Args> auto make_vec(size_t n, Args... args) {
    return vector<decltype(make_vec<T>(args...))>(n, make_vec<T>(args...));
}
template<typename T, typename U, typename... V>
typename enable_if<is_same<T, U>::value>::type fill_v(U& u, const V... v) { u = U(v...); }
template<typename T, typename U, typename... V>
typename enable_if<!is_same<T, U>::value>::type fill_v(U& u, const V... v) {
    for (auto& e : u) fill_v<T>(e, v...);
}

template<typename T>
struct rerooting {
    using F1 = function<T(T, int)>;
    using F2 = function<T(T, T)>;

    int n;
    F1 f, g; // f : pre-merge, g : post-merge
    F2 merge;
    T id;
    vector<vector<int>> graph;
    vector<vector<T>> dp;

    rerooting(int n, F1 f, F1 g, F2 merge, T id)
        : n(n), f(f), g(g), merge(merge), id(id), graph(n), dp(n) {}

    void add_edge(int u, int v) {
        graph[u].push_back(v);
        graph[v].push_back(u);
    }

    T dfs1(int v, int p = -1) {
        int deg = graph[v].size();
        dp[v].resize(deg);
        T dp_cum = id;
        for (int i = 0; i < deg; ++i) {
            if (graph[v][i] == p) continue;
            dp[v][i] = dfs1(graph[v][i], v);
            dp_cum = merge(dp_cum, f(dp[v][i], graph[v][i]));
        }
        return g(dp_cum, v);
    }

    void dfs2(int v, T dp_p, int p = -1) {
        int deg = graph[v].size();
        for (int i = 0; i < deg; ++i) {
            if (graph[v][i] == p) {
                dp[v][i] = dp_p;
                break;
            }
        }
        vector<T> dp_l(deg + 1, id), dp_r(deg + 1, id);
        for (int i = 0; i < deg; ++i) dp_l[i+1] = merge(dp_l[i], f(dp[v][i], graph[v][i]));
        for (int i = deg - 1; i >= 0; --i) dp_r[i] = merge(f(dp[v][i], graph[v][i]), dp_r[i+1]);
        for (int i = 0; i < deg; ++i) {
            if (graph[v][i] == p) continue;
            dfs2(graph[v][i], g(merge(dp_l[i], dp_r[i+1]), v), v);
        }
    }

    void build() {
        dfs1(0);
        dfs2(0, id);
    }

    T solve(int v) {
        T ans = id;
        int deg = graph[v].size();
        for (int i = 0; i < deg; ++i) {
            ans = merge(ans, f(dp[v][i], graph[v][i]));
        }
        return g(ans, v);
    }
};

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

    int n, m;
    cin >> n >> m;
    auto f = [&](ll a, int b) { (void) b; return (a + 1) % m; };
    auto g = [&](ll a, int b) { (void) b; return a; };
    auto merge = [&](ll a, ll b) { return (a * b) % m; };
    rerooting<ll> re(n, f, g, merge, 1);

    rep(i, n-1) {
        int u, v;
        cin >> u >> v;
        u--; v--;
        re.add_edge(u, v);
    }

    re.build();
    rep(i, n) cout << re.solve(i) << '\n';
}


Measured memory_bytes: 33376