// #define _GLIBCXX_DEBUG // for STL debug (optional)
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
#include <bitset>
using namespace std;

#define debug(...) fprintf(stderr, __VA_ARGS__)
#define int long long int
 
template<typename T> void chmax(T &a, T b) {a = max(a, b);}
template<typename T> void chmin(T &a, T b) {a = min(a, b);}
template<typename T> void chadd(T &a, T b) {a = a + b;}
 
typedef pair<int, int> pii;
typedef long long ll;
 
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const ll INF = 1001001001001001LL;
const ll MOD = 1000000007LL;

int N, M, dp[100010][2], ans[100010];
vector<int> G[100010];

const int WHITE = 0;
const int BLACK = 1;

int solve(int cur, int col, int par=-1) {
    int &val = dp[cur][col];
    if(val >= 0) return val;

    val = 1;
    for(auto to : G[cur]) {
        if(to == par) continue;
        int white = solve(to, WHITE, cur);
        int black = solve(to, BLACK, cur);

        if(col == WHITE) {
            (val *= white) %= M;
        }
        if(col == BLACK) {
            (val *= (white + black)) %= M;
        }
    }
    return val;
}

/*
void dump_array(int cur) {
    fprintf(stderr, "# result! (root = %lld):\n", cur);
    for(int i=0; i<N; i++) {
        for(int j=0; j<2; j++) {
            fprintf(stderr, "dp[%lld][%lld] = %lld\n", i, j, dp[i][j]);
        }
    }

}
*/

void rotate(int cur, int par=-1) {
    // dump_array(cur);
    ans[cur] = dp[cur][BLACK];

    int par_w = 1, par_b = 1;
    vector<int> info_w, info_b, nxt;
    for(auto to : G[cur]) {
        if(to == par) {
            par_w = dp[to][WHITE];
            par_b = (dp[to][BLACK] + dp[to][WHITE]) % M;
            // fprintf(stderr, "cur = %lld, par = %lld, white = %lld, black = %lld\n", cur, par, par_w, par_b);
            continue;
        }
        info_w.push_back(dp[to][WHITE]);
        info_b.push_back((dp[to][BLACK] + dp[to][WHITE]) % M);
        nxt.push_back(to);
    }

    vector<int> pre_w(1, 1), suf_w(1, 1), pre_b(1, 1), suf_b(1, 1);
    int L = info_w.size();
    for(int i=0; i<L; i++) {
        if(i == 0) {
            pre_w.push_back(info_w[i]);
            pre_b.push_back(info_b[i]);
        }
        else {
            pre_w.push_back(pre_w.back() * info_w[i] % M);
            pre_b.push_back(pre_b.back() * info_b[i] % M);
        }
    }
    for(int i=L-1; i>=0; i--) {
        if(i == L-1) {
            suf_w.push_back(info_w[i]);
            suf_b.push_back(info_b[i]);
        }
        else {
            suf_w.push_back(suf_w.back() * info_w[i] % M);
            suf_b.push_back(suf_b.back() * info_b[i] % M);
        }
    }
    reverse(suf_w.begin(), suf_w.end());
    reverse(suf_b.begin(), suf_b.end());

    int orig_w = dp[cur][WHITE], orig_b = dp[cur][BLACK];
    for(int i=0; i<L; i++) {
        int to = nxt[i];
        
        dp[cur][WHITE] = pre_w[i] * suf_w[i+1] % M * par_w % M;
        dp[cur][BLACK] = pre_b[i] * suf_b[i+1] % M * par_b % M;
        // fprintf(stderr, "cur = %lld, to = %lld, white = %lld, black = %lld\n", cur, to, dp[cur][WHITE], dp[cur][BLACK]);
        
        int orig_child_w = dp[to][WHITE], orig_child_b = dp[to][BLACK];
        (dp[to][WHITE] *= dp[cur][WHITE]) %= M;
        (dp[to][BLACK] *= dp[cur][WHITE] + dp[cur][BLACK]) %= M;
        rotate(to, cur);

        dp[to][WHITE] = orig_child_w;
        dp[to][BLACK] = orig_child_b;
    }
    dp[cur][WHITE] = orig_w;
    dp[cur][BLACK] = orig_b;
}

signed main() {
    cin >> N >> M;
    for(int i=0; i<N-1; i++) {
        int x, y; cin >> x >> y;
        x--; y--;
        G[x].push_back(y);
        G[y].push_back(x);
    }

    fill(dp[0], dp[N], -1);
    solve(0, 0); solve(0, 1);
    rotate(0);

    for(int i=0; i<N; i++) {
        cout << ans[i] % M << endl;
    }
    return 0;
}


Measured memory_bytes: 50432