#include <iostream>
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cstring>
#include <deque>
#include <functional>
#include <initializer_list>
#include <math.h>
#include <map>
#include <set>
#include <stack>
#include <tuple>
#include <queue>
#include <vector>

#define FOR(i, a, b) for(int (i) = (a); (i) < (b); ++(i))
#define rFOR(i, a, b) for(int (i) = (b); (i) >= (a); --(i))
#define REP(i, n) FOR(i, 0, n)
#define rREP(i, n) rFOR(i, 0, (n-1))
#define SORT(A) std::sort((A).begin(), (A).end())
#define ALL(A) (A).begin(), (A).end()
// 座標圧縮 (for vector) : ソートしてから使うのが一般的 ; SORT(A) => COORDINATE_COMPRESSION(A)
#define COORDINATE_COMPRESSION(A) (A).erase(unique((A).begin(),(A).end()),(A).end())

using lli = long long int;
using pii = std::pair<int, int>;

void VintOut(std::vector<int>& A){
    const int n = A.size();
    if(n == 0){putchar('\n'); return;}
    printf("%d", A[0]);
    for(int i = 1; i < n; ++i) printf(" %d", A[i]);
    putchar('\n');
}
void VintOut(std::vector<long long int>& A){
    const int n = A.size();
    if(n == 0){ putchar('\n'); return;}
    printf("%lld", A[0]);
    for(int i = 1; i < n; ++i) printf(" %lld", A[i]);
    putchar('\n');
}

template <typename T>
inline bool chmin(T& a, T b){
    if(b < a){ a = b; return true;}
    return false;
}

template <typename T>
inline bool chmax(T& a, T b){
    if(a < b){ a = b; return true;}
    return false;
}

inline bool bitUP(int state, int k){ return (state >> k) & 1; }
inline bool bitUP(long long int state, int k){ return (state >> k) & 1;}


// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


std::vector<std::vector<int>> Edge;
std::vector<int> Depth;
std::vector<int> ChildSize;
std::vector<lli> DP, EP;
std::vector<std::vector<lli>> S;
std::vector<std::vector<int>> Child;

int main(void){
    int n, m; scanf("%d%d", &n, &m);
    Edge.resize(n);
    for(int _ = 1; _ < n; ++_){
        int x, y; scanf("%d%d", &x, &y); x--; y--;
        Edge[x].push_back(y); Edge[y].push_back(x);
    }


    // BFS
    int root = 0; // 根
    Depth.assign(n, -1); ChildSize.assign(n, 0);
    std::queue<int> Q; Q.push(root); Depth[root] = 0;
    while(not Q.empty()){
        int v = Q.front(); Q.pop();
        for(int nv : Edge[v]) if(Depth[nv] == -1){
            Depth[nv] = Depth[v] + 1; ChildSize[v]++;
            Q.push(nv);
        }
    }


    
    DP.assign(n, 1); S.resize(n); Child.resize(n);
    // 葉から DFS
    std::stack<int> Stk;
    REP(i, n) if(ChildSize[i] == 0) Stk.push(i);
    while(not Stk.empty()){
        int v = Stk.top(); Stk.pop();

        for(int par : Edge[v]) if(Depth[par] < Depth[v]){

            (DP[par] *= (DP[v] + 1)) %= m;
            
            Child[par].push_back(v);
            S[par].push_back(DP[v] + 1);

            if((--ChildSize[par]) == 0) Stk.push(par);

            break;
        }
    }


    std::vector<lli> T; EP.assign(n, 1);
    Stk.push(root);
    while(not Stk.empty()){
        int v = Stk.top(); Stk.pop();

        const int nn = S[v].size();
        if(nn == 0) continue;

        T.resize(nn);  T[nn-1] = 1;
        for(int x = nn - 1; x - 1 >= 0; --x) T[x-1] = (T[x] * S[v][x]) % m;


        lli left = 1;
        for(int x = 0; x < nn; ++x){
            EP[Child[v][x]] = (left * T[x]) % m;
            (EP[Child[v][x]] *= EP[v])++; EP[Child[v][x]] %= m;
            Stk.push(Child[v][x]);
            (left *= S[v][x]) %= m;
        }
    }

    REP(i, n) printf("%lld\n", (DP[i] * EP[i]) % m);

    
    return 0;
}

Measured memory_bytes: 19968