#include <bits/stdc++.h>

#include <utility>

using namespace std;

using ll = long long;
using P = pair<ll, ll>;
using Graph = vector<vector<ll>>;
#define rep(i, n) for(ll i=0;i<(ll)(n);i++)
#define rep2(i, m, n) for(ll i=m;i<(ll)(n);i++)
#define rrep(i, n, m) for(ll i=n;i>=(ll)(m);i--)
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int ddx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const int ddy[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const ll MOD = 1000000007;
const ll INF = 1000000000000000000L;
#ifdef __DEBUG

/**
 * For DEBUG
 * https://github.com/ta7uw/cpp-pyprint
 */
#include "cpp-pyprint/pyprint.h"

#endif

template<class Monoid>
class ReRooting {
public:
    ReRooting(int n, vector<pair<int, int>> &edges, Monoid identity,
              function<Monoid(Monoid, Monoid)> operation,
              function<Monoid(Monoid, int)> node_operation)
            : n(n), identity(identity), operation(move(operation)), node_operation(move(node_operation)) {
        adjacents.resize(n);
        adjacent_indeces.resize(n);
        results.resize(n, identity);

        for (auto &edge : edges) {
            adjacent_indeces[edge.first].push_back(adjacents[edge.second].size());
            adjacent_indeces[edge.second].push_back(adjacents[edge.first].size());
            adjacents[edge.first].push_back(edge.second);
            adjacents[edge.second].push_back(edge.first);
        }
        dp.resize(n);
        for (int i = 0; i < n; i++) {
            dp[i] = vector<Monoid>(adjacents[i].size());
        }
    }

    void execute() {
        if (n == 1) {
            results[0] = node_operation(identity, 0);
            return;
        }
        vector<int> parents(n);
        vector<int> pre_order(n);
        int index = 0;
        stack<int> stack;
        stack.push(0);
        parents[0] = -1;
        while (!stack.empty()) {
            int node = stack.top();
            stack.pop();
            pre_order[index++] = node;
            for (int i = 0; i < adjacents[node].size(); i++) {
                int adjacent = adjacents[node][i];
                if (adjacent == parents[node]) continue;
                stack.push(adjacent);
                parents[adjacent] = node;
            }
        }

        for (int i = n - 1; i >= 1; i--) {
            int node = pre_order[i];
            int parent = parents[node];
            Monoid accum = identity;
            int parent_index = -1;
            for (int j = 0; j < adjacents[node].size(); j++) {
                if (adjacents[node][j] == parent) {
                    parent_index = j;
                    continue;
                }
                accum = operation(accum, dp[node][j]);
            }
            dp[parent][adjacent_indeces[node][parent_index]] = node_operation(accum, node);
        }

        for (int i = 0; i < n; i++) {
            int node = pre_order[i];
            Monoid accum = identity;
            int m = adjacents[node].size();
            vector<Monoid> accums(m);
            accums[m - 1] = identity;
            for (int j = m - 1; j >= 1; j--) {
                accums[j - 1] = operation(dp[node][j], accums[j]);
            }
            for (int j = 0; j < m; j++) {
                dp[adjacents[node][j]][adjacent_indeces[node][j]] = node_operation(operation(accum, accums[j]), node);
                accum = operation(accum, dp[node][j]);
            }
            results[node] = node_operation(accum, node);
        }
    }

    Monoid query(int idx) {
        return results[idx];
    }

private:
    int n;
    vector<vector<int>> adjacents;
    vector<vector<int>> adjacent_indeces;
    vector<Monoid> results;
    vector<vector<Monoid>> dp;
    Monoid identity;
    function<Monoid(Monoid, Monoid)> operation;
    function<Monoid(Monoid, int)> node_operation;
};

void solve() {
    ll N, M;
    cin >> N >> M;
    vector<pair<int, int>> edges(N - 1);
    rep(i, N - 1) {
        ll x, y;
        cin >> x >> y;
        x--;
        y--;
        edges[i] = make_pair(x, y);
    }
    ReRooting<ll> reRooting(N, edges, 1,
                            [&](ll a, ll b) { return (a * b) % M; },
                            [&](ll a, int i) { return (a + 1) % M; });
    reRooting.execute();
    rep(i, N) {
        cout << (reRooting.query(i) - 1 + M) % M << '\n';
    }
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);
    solve();
    return 0;
}


Measured memory_bytes: 24812