#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

const int MAXN = 100005;
ll mod;
ll up[MAXN];
ll down[MAXN];
vector<ll> g[MAXN];
ll pr[MAXN];

void dfs_down(int v, int p)
{
    down[v] = 1;
    for (auto to : g[v])
    {
        if (to == p) continue;
        dfs_down(to, v);
        down[v] *= down[to] + 1;
        down[v] %= mod;
    }
}

void dfs_up(int v, int p, ll val)
{
    up[v] = val;
    ll pref = 1;
    for (int i = 0; i < g[v].size(); i++)
    {
        ll to = g[v][i];
        if (to == p) continue;
        pr[to] = pref; pr[to] %= mod;
        pref *= down[to] + 1; pref %= mod;
    }
    ll suf = 1;
    for (int i = g[v].size()-1; i >= 0; i--)
    {
        ll to = g[v][i];
        if (to == p) continue;
        ll nval = val;
        nval *= pr[to]; nval %= mod;
        nval *= suf; nval++; nval %= mod;
        dfs_up(to, v, nval);
        suf *= down[to] + 1; suf %= mod;
    }
}

int main()
{
    int n;
    cin >> n >> mod;
    for (int i = 0; i < n-1; i++)
    {
        int u, v;
        cin >> u >> v;
        g[u].push_back(v);
        g[v].push_back(u);
    }

    dfs_down(1, 0);
    dfs_up(1, 0, 1);

    for (int i = 1; i <= n; i++)
        cout << (down[i] * up[i]) % mod << endl;

    return 0;
}
