#include <algorithm>
#include <iostream>
#include <vector>
#include <math.h>
#include <set>
#include <map>
#include <string>
#include <stack>
#include <queue>
#include <iomanip>
#include <numeric>
#include <tuple>
#include <bitset>
#include <complex>
#include <unistd.h>
#include <cassert>
#include <cctype>
#include <random>
#include <time.h>
#define _USE_MATH_DEFINES
#define _GLIBCXX_DEBUG
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> plglg;
typedef pair<double, ll> pdlg;
typedef tuple<int, int, int> tiii;
typedef tuple<ll, ll, ll> tlglglg;
typedef tuple<double, double, double> tddd;
typedef complex<double> xy_t;
typedef vector<ll> vll;
typedef vector< vector<ll> > matrix;
#define REP(i, x, y) for(ll i = (ll)x; i < (ll)y; i++)
#define DREP(i, x, y, d) for(ll i = (ll)x; i < (ll)y; i += (ll)d)
#define PER(i, x, y) for(ll i = (ll)x; i > (ll)y; i--)
#define DPER(i, x, y, d) for(ll i = (ll)x; i > (ll)y; i -= (ll)d)
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
double pi = 3.141592653589793;
ll mod = 1000000007;
int intmax = 2147483647;
int intmin = -2147483648;
ll llmax = 9223372036854775807;
ll llmin = -9223372036854775807;
int iinf = intmax / 8;
ll inf = llmax / 8;
double eps = 1e-12;

ll modadd(ll x, ll y) {
    return ((x + y) % mod + mod) % mod;
}

ll multiply(ll x, ll y) {
    return (x % mod) * (y % mod) % mod;
}

struct edge {
    ll to, cost;
};

int V;
vector<edge> G[1000000];
ll d[1000000];

void addedge(int st, int ed, ll co) {
    edge e;
    e.to = ed;
    e.cost = co;
    G[st].push_back(e);
}

ll parent[100010];
ll parnum[100010];
ll dp[100010];
vector<ll> to[100010];
vector<ll> chi[100010];
vector<ll> preprod[100010];
vector<ll> sufprod[100010];

ll dpsolve(ll vertex, ll par) {
    ll ans = 1;
    parent[vertex] = par;
    REP(i, 0, G[vertex].size()) {
        edge e = G[vertex][i];
        if (e.to != par) {
            chi[vertex].push_back(e.to);
            ans = multiply(ans, modadd(dpsolve(e.to, vertex), 1));
        }
    }
    return dp[vertex] = ans;
}

ll dpans[100010];
ll dpparnum[100010];

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll N, M;
    cin >> N >> M;
    mod = M;
    ll x[N - 1], y[N - 1];
    REP(i, 0, N - 1) {
        cin >> x[i] >> y[i];
        addedge(x[i] - 1, y[i] - 1, 1);
        addedge(y[i] - 1, x[i] - 1, 1);
    }
    dpsolve(0, -1);
    REP(i, 0, N) {
        preprod[i].resize(G[i].size());
        ll tmp = 1;
        REP(j, 0, G[i].size()) {
            edge e = G[i][j];
            if (e.to == parent[i]) {
                preprod[i][j] = tmp;
            } else {
                parnum[e.to] = j;
                tmp = multiply(tmp, modadd(dp[e.to], 1));
                preprod[i][j] = tmp;
            }
        }
        sufprod[i].resize(G[i].size());
        tmp = 1;
        PER(j, G[i].size() - 1, -1) {
            edge e = G[i][j];
            if (e.to == parent[i]) {
                sufprod[i][j] = tmp;
            } else {
                tmp = multiply(tmp, modadd(dp[e.to], 1));
                sufprod[i][j] = tmp;
            }
        }
    }
    dpans[0] = dp[0];
    dpparnum[0] = 1;
    queue<ll> que;
    REP(i, 0, G[0].size()) {
        que.push(G[0][i].to);
    }
    while(!que.empty()) {
        ll p = que.front();
        que.pop();
        ll tmp = 1;
        REP(i, 0, G[p].size()) {
            edge e = G[p][i];
            if (e.to == parent[p]) {
                ll num = dpparnum[e.to];
                if (parnum[p] != 0) {
                    num = multiply(num, preprod[e.to][parnum[p] - 1]);
                }
                if (parnum[p] != (ll)G[e.to].size() - 1) {
                    num = multiply(num, sufprod[e.to][parnum[p] + 1]);
                }
                dpparnum[p] = modadd(num, 1);
                tmp = multiply(tmp, modadd(num, 1));
            } else {
                que.push(e.to);
                tmp = multiply(tmp, modadd(dp[e.to], 1));
            }
        }
        dpans[p] = tmp;
    }
    // REP(i, 0, preprod[1].size()) {
    //     cout << preprod[1][i] << " ";
    // }
    // cout << endl;
    // REP(i, 0, N) {
    //     cout << parent[i] << " ";
    // }
    // cout << endl;
    // REP(i, 0, N) {
    //     cout << parnum[i] << " ";
    // }
    // cout << endl;
    // REP(i, 0, N) {
    //     cout << dpparnum[i] << " ";
    // }
    // cout << endl;
    // REP(i, 0, N) {
    //     cout << dp[i] << " ";
    // }
    // cout << endl;
    REP(i, 0, N) {
        cout << dpans[i] << endl;
    }
}


Measured memory_bytes: 61824