#include <bits/stdc++.h>
#pragma GCC diagnostic ignored "-Wunused-result"
#define int long long
using namespace std;

#define fin(i,n) for (int i = 0; i < n; i++)
#define fin2(i,a,b) for (int i = a; i < b; i++)
#define ford(i,n) for (int i = n-1; i >= 0; i--)
#define ford2(i,a,b) for (int i = a-1; i >= b; i--)

#define mp make_pair
#define mt make_tuple
#define pb push_back
#define PI 3.1415926535897932384626
#define EXP1 2.718281828459045
#define BIG 1000000000000000000

#define s1(a) cin >> a;
#define s2(a,b) cin >> a >> b;
#define s3(a,b,c) cin >> a >> b >> c;
#define s4(a,b,c,d) cin >> a >> b >> c >> d;
#define s5(a,b,c,d,e) cin >> a >> b >> c >> d >> e;
#define s6(a,b,c,d,e,f) cin >> a >> b >> c >> d >> e >> f;

#define p1(a) cout << a << '\n';
#define p2(a,b) cout << a << ' ' << b << '\n';
#define p3(a,b,c) cout << a << ' ' << b << ' ' << c << '\n';
#define p4(a,b,c,d) cout << a << ' ' << b << ' ' << c << ' ' << d << '\n';
#define p5(a,b,c,d,e) cout << a << ' ' << b << ' ' << c << ' ' << d << ' ' << e << '\n';
#define p6(a,b,c,d,e,f) cout << a << ' ' << b << ' ' << c << ' ' << d << ' ' << e << ' ' << f << '\n';

#define chmin(x, v) x = min(x, v)
#define chmax(x, v) x = max(x, v)

#define OK p1("OK")
#define OK1 p1("OK1")
#define OK2 p1("OK2")

#define ll int
#define vl vector<ll>
#define vvl vector<vl>
#define vvvl vector<vvl>

#define dll pair<ll, ll>
#define vdll vector<dll>
#define vvdll vector<vdll>

#define vb vector<bool>
#define vvb vector<vb>
#define vd vector<double>
#define vvd vector<vd>
#define vc vector<char>
#define vvc vector<vc>

#define all(v) v.begin(), v.end()
#define vlen(v) (int)(v.size())
#define pb push_back
#define fi first
#define se second

void solve();
signed main()
{
    #ifdef LOCAL_HUGO
    freopen("in.txt", "r", stdin);
    #else
    ios::sync_with_stdio(false);
    cin.tie(0);
    #endif
    solve();
    return 0;
}

int n, mod;
const int borne = (int)(1e5) + 5;
vl adj[borne];
int par[borne];
int sub[borne];
int up[borne];
vl pref[borne];
vl suff[borne];
int rev[borne];

bool subok[borne];
bool upok[borne];

void constructSub(int nod)
{
    if (subok[nod]) return;
    subok[nod] = true;
    sub[nod] = 1;
    for (int enf : adj[nod]) if (enf != par[nod]) {
        constructSub(enf);
        sub[nod] *= (1 + sub[enf]);
        sub[nod] %= mod;
    }
}

void constructPrefix(int nod)
{
    int k = adj[nod].size();
    pref[nod] = vl(k,1);
    suff[nod] = vl(k, 1);
    fin(i, k) if (adj[nod][i] != par[nod]) {
        pref[nod][i] = suff[nod][i] = (1 + sub[adj[nod][i]]);
    }
    fin2(i, 1, k) pref[nod][i] = (pref[nod][i] * pref[nod][i-1]) % mod;
    ford(i, k-1) suff[nod][i] = (suff[nod][i] * suff[nod][i+1]) % mod;
}

int sumExcept(int nod, int exceptChild)
{
    int ind = rev[exceptChild];
    int a = (ind > 0 ? pref[nod][ind-1] : 1);
    int b = ((ind < (int)(suff[nod].size()) - 1) ? suff[nod][ind+1] : 1);
    return (a*b) % mod;
}

void constructUp(int nod)
{
    if (upok[nod]) return;
    upok[nod] = true;
    up[nod] = 1;
    if (par[nod] != 0) {
        constructUp(par[nod]);
        up[nod] = (1 + up[par[nod]]*sumExcept(par[nod], nod));
        up[nod] %= mod;
    }
}

void dfs(int nod)
{
    int i = 0;
    for (int enf : adj[nod]) { if (enf != par[nod]) {
        par[enf] = nod;
        rev[enf] = i;
        dfs(enf);
    } ++i; }
}

void solve()
{
    s2(n, mod);
    fin(i, n-1) {
        int x, y;
        s2(x, y);
        adj[x].pb(y);
        adj[y].pb(x);
    }
    dfs(1);
    fin2(i,1,n+1) constructSub(i);
    fin2(i,1,n+1) constructPrefix(i);
    fin2(i,1,n+1) constructUp(i);

    fin2(i,1,n+1) {
        p1((((sub[i]*up[i]) % mod)+mod)%mod);
    }
    return;
}


Measured memory_bytes: 24320