#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vl = vector<ll>;
template<class T> using vc = vector<T>;
template<class T> using vvc = vector<vector<T>>;

#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define repr(i, n) for (ll i = (n)-1; i >= 0; i--)
#define repe(i, l, r) for (ll i = (l); i < (r); i++)
#define reper(i, l, r) for (ll i = (r)-1; i >= (l); i--)
#define repa(i,n) for (auto& i: n)

template<class T> inline bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> inline bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
void init() {cin.tie(0);ios::sync_with_stdio(false);cout << fixed << setprecision(15);}

#ifdef DEBUG
template <class T, class N> void verr(const T& a, const N& n) { rep(i, n) cerr << a[i] << " "; cerr << "\n" << flush; }
ll dbgt = 1; void err() { cerr << "passed " << dbgt++ << "\n" << flush; }
template<class H, class... T> void err(H&& h,T&&... t){ cerr<< h << (sizeof...(t)?" ":"\n") << flush; if(sizeof...(t)>0) err(forward<T>(t)...); }
#endif

const ll INF = 4e18;
const ld EPS = 1e-11;
const ld PI = acos(-1.0L);
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
//--------------------------------------------------------------------------------//

template<typename T>
struct ReRooting{
    using Func = function<T(T, T)>;
    struct edge{
        ll to;
        T val;
        edge(){}
        edge(ll t_, T v_): to(t_), val(v_){}
    };
    vector<vector<edge>> G;     // グラフ
    vector<T> V;                // 各頂点を根としたときの値
    vector<map<ll, T>> dp;
    Func merge;                 // 部分木同士を1頂点でまとめる
    Func rooting;               // 辺、部分木毎の係数を加える
    T id;

    ReRooting(ll N_, Func m_, Func r_, T id_)
        : G(N_), V(N_), dp(N_), merge(m_), rooting(r_), id(id_){}

    void addedge(ll a, ll b, T c){
        G[a].emplace_back(b, c);
    }

    void build(){
        ll N = G.size();
        auto dfs = [&](auto&& dfs, ll now, ll par, bool flag) {
            if (dp[now].count(par)) return dp[now][par];

            if(par == -1 or flag){
                vector<T> A;
                for (ll i = 0; i < G[now].size(); i++){
                    ll to = G[now][i].to;
                    if (to == par) continue;

                    A.emplace_back(rooting(dfs(dfs, to, now, flag), G[now][i].val));
                }

                vector<T> R(A.size() + 1, id);  //右からの累積和
                for (ll i = A.size(); i > 0; i--) R[i - 1] = merge(R[i], A[i - 1]);

                if(par == -1){
                    T L = id;
                    for (ll i = 0; i < G[now].size(); i++){
                        ll to = G[now][i].to;

                        dp[now][to] = merge(L, R[i + 1]);  //now->toを除いた値
                        L = merge(L, A[i]);  //左から累積していく
                    }
                }

                return dp[now][par] = R[0];

            }
            else{
                dfs(dfs, now, -1, flag);
                return dfs(dfs, now, par, flag);
            }
        };

        dfs(dfs, 0, -1, true);

        for (ll i = 0; i < N; i++) dfs(dfs, i, -1, false), V[i] = dp[i][-1];
    }
};

int main() {
    init();
    ll N, M;
    cin >> N >> M;

    
    auto f1 = [&](ll a, ll b) { return a * b % M; };
    auto f2 = [&](ll a, ll b) { return a + 1; };
    ReRooting<ll> rr(N, f1, f2, 1);
    rep(i, N - 1){
        ll a, b;
        cin >> a >> b;
        rr.addedge(a - 1, b - 1, 1);
        rr.addedge(b - 1, a - 1, 1);
    }

    rr.build();

    rep(i, N) cout << rr.V[i] << '\n';
}

Measured memory_bytes: 51644