#include <bits/stdc++.h>
#ifdef ONLINE_JUDGE
#define endl "\n"
#endif
using namespace std;
typedef unsigned long long int lu;
typedef long long int ll;
typedef long double ld;
typedef vector<ll> v;
typedef vector<lu> vu;
typedef vector<v> vv;
typedef vector<vu> vvu;
typedef vector<ld> vld;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef pair<ll, ll> pll;
typedef vector<set<ll>> vsll;
typedef set<pair<ll, ll>> spll;
typedef vector<spll> vspll;
typedef vector<pll> vpll;
typedef pair<lu, lu> puu;
typedef vector<puu> vpuu;
const ll MOD = 1e9 + 7;
const ld PI = 2 * acos(0.0);
const v dx = {1, -1, 0, 0};
const v dy = {0, 0, 1, -1};
#define round(x, y) ((x + y - 1) / y)
#define ce(x, y) ((x + y - 1) / y)
#define amax(x, y) \
    if (y > x)     \
        x = y;
#define amin(x, y) \
    if (y < x)     \
        x = y;
#define lcm(x, y) ((x) * (y) / __gcd(x, y))
#define sz(x) (ll) x.size()
#define sq(x) ((x) * (x))
#define cb(x) ((x) * (x) * (x))
#define yes cout << "YES\n";
#define no cout << "NO\n";
#define yesno(f) \
    if (f)       \
        yes else no;
#define noo \
    { no return; }

#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()

template <typename T = ll>
vector<T> ga(ll n, bool oneIndexed = false) {
    vector<T> a = vector<T>(n + oneIndexed);

    for (ll i = 0; i < n; i++) {
        T p;
        cin >> p;
        a[i + oneIndexed] = p;
    }

    return move(a);
}

template <typename T, typename A>
void pa(vector<T, A> const &a, ll begin = 0, ll end = -1) {
    if (end == -1)
        end = sz(a) - 1;
    for (ll i = begin; i <= end; i++) {
        cout << a[i] << " ";
    }
    cout << endl;
}

#define MAX (ll)1e5 + 10
ll n, M;

// number of ways of painting the subtree rooted at i; 0 =black,1=white
v dp(MAX, 0);

void dfs(vv &adjList, ll curr, ll prev) {
    dp[curr] = 1;

    for (auto neigh : adjList[curr]) {
        if (neigh == prev)
            continue;
        dfs(adjList, neigh, curr);
        dp[curr] *= (dp[neigh] + 1);
        dp[curr] %= M;
    }
}

v answer(MAX, 0);

// find answers while changing the root
void dfs2(vv &adjList, ll leftpart, ll curr, ll prev) {
    answer[curr] = (leftpart * dp[curr]) % M;

    v bacchas;

    v vals;

    for (auto neigh : adjList[curr]) {
        if (neigh == prev)
            continue;

        bacchas.push_back(neigh);
        vals.push_back((dp[neigh] + 1) % M);
    }

    v prefixProd(vals), suffixProd(vals);

    for (ll i = 1; i < sz(bacchas); i++) {
        prefixProd[i] *= prefixProd[i - 1];
        prefixProd[i] %= M;
    }

    for (ll i = sz(bacchas) - 2; i >= 0; i--) {
        suffixProd[i] *= suffixProd[i + 1];
        suffixProd[i] %= M;
    }

    for (ll i = 0; i < sz(bacchas); i++) {
        ll lp = leftpart;

        if (i > 0) {
            lp *= prefixProd[i - 1];
            lp %= M;
        }
        if (i < sz(bacchas) - 1) {
            lp *= suffixProd[i + 1];
            lp %= M;
        }
        lp++;
        lp %= M;

        dfs2(adjList, lp, bacchas[i], curr);
    }
}

void solve() {
    cin >> n >> M;
    ll m = n - 1;

    vv adjList(n + 1, v(0));
    for (ll i = 0; i < m; i++) {
        ll a, b;
        cin >> a >> b;
        adjList[a].push_back(b);
        adjList[b].push_back(a);
    }
    dfs(adjList, 1, -1);
    dfs2(adjList, 1, 1, -1);
    for (ll i = 1; i <= n; i++) {
        cout << answer[i] << endl;
    }
}

int main(void) {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    solve();
    return 0;
}

Measured memory_bytes: 27648