#include <bits/stdc++.h>

using namespace std;

#define SZ(x) (int)(x.size())

using ll = long long;
using ld = long double;
using P = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
const ld eps = 1e-9;
ll MOD = 1000000007;
const int INF = 1000000000;
const ll LINF = 1ll<<50;

template<typename T>
void printv(const vector<T>& s) {
  for(int i=0;i<(int)(s.size());++i) {
    cout << s[i];
    if(i == (int)(s.size())-1) cout << endl;
    else cout << " ";
  }
}

template<typename T1, typename T2>
ostream& operator<<(ostream &os, const pair<T1, T2> p) {
  os << p.first << ":" << p.second;
  return os;
}

class Combination
{
    std::vector<long long> fact, finv, inv;
    ll mod;
public:
    Combination() {}
    Combination(int sz, ll mod): mod(mod)
    {
        fact.resize(sz);
        finv.resize(sz);
        inv.resize(sz);
        fact[0] = 1;
        for (int i = 1; i < sz; ++i)
        {
            fact[i] = fact[i - 1] * i % mod;
        }
        inv[0] = 0;
        inv[1] = 1;
        for (int i = 2; i < sz; ++i)
        {
            inv[i] = mod - inv[mod % i] * (mod / i) % mod;
        }
        finv[0] = 1;
        for (int i = 1; i < sz; ++i)
        {
            finv[i] = finv[i - 1] * inv[i] % mod;
        }
    }

    long long C(long long n, long long k)
    {
        if (n < k)
            return 0;
        if (n < 0 || k < 0)
            return 0;
        return (fact[n] * finv[k] % mod) * finv[n - k] % mod;
    }

    long long P(long long n, long long k)
    {
        if (n < k)
            return 0;
        if (n < 0 || k < 0)
            return 0;
        return fact[n] * finv[n - k] % mod;
    }

    long long H(long long n, long long k)
    {
        return this->C(n + k - 1, k);
    }

    long long getfact(int i)
    {
        return fact[i];
    }

    long long getinv(int i)
    {
        return inv[i];
    }

    long long getfinv(int i)
    {
        return finv[i];
    }
};

Combination comb;

vll cntb, cntw;
vll ansb, answ;
void dfs1(int now, int prev, const vvi &tr) {
  for(int i=0;i<SZ(tr[now]);++i) {
    int nxt = tr[now][i];
    if(nxt == prev) continue;
    dfs1(nxt, now, tr);
  }
  for(int i=0;i<SZ(tr[now]);++i) {
    int nxt = tr[now][i];
    if(nxt == prev) continue;
    cntb[now] *= (cntb[nxt] + 1);
    cntb[now] %= MOD;
    cntw[now] += cntw[nxt] + cntb[nxt];
    cntw[now] %= MOD;
  }
}

int n;
void dfs2(int now, int prev, ll parb, ll parw, const vvi &tr) {
  //cout << now << ":" << prev << ":" << parb << ":" << parw << endl;
  ansb[now] = cntb[now] * (parb + 1) % MOD;
  answ[now] = (cntw[now] + (parb + parw)) % MOD;
  vll mull(SZ(tr[now])+2, 1), mulr(SZ(tr[now])+2, 1);
  for(int i=0;i<SZ(tr[now]);++i) {
    int nxt = tr[now][i];
    if(nxt == prev) mull[i+1] = mull[i];
    else mull[i+1] = mull[i] * (cntb[nxt]+1) % MOD;
  }
  for(int i=SZ(tr[now])-1;i>=0;--i) {
    int nxt = tr[now][i];
    if(nxt == prev) mulr[i+1] = mulr[i+2];
    else mulr[i+1] = mulr[i+2] * (cntb[nxt]+1) % MOD;
  }
  //printv(mull);
  //printv(mulr);

  for(int i=0;i<SZ(tr[now]);++i) {
    int nxt = tr[now][i];
    if(nxt == prev) continue;
    ll nxtparb = mull[i] * mulr[i+2] % MOD * (parb + 1) % MOD;
    ll nxtparw = cntw[now] + (parb + parw) + (2*MOD - (cntb[nxt] + cntw[nxt]));
    nxtparw %= MOD;
    dfs2(nxt, now, nxtparb, nxtparw, tr);
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);

  cin >> n >> MOD;
  vvi tr(n);
  for(int i=0;i<n-1;++i) {
    int x, y; cin >> x >> y;
    x--; y--;
    tr[x].push_back(y);
    tr[y].push_back(x);
  }

  comb = Combination(1000000, MOD);
  cntb.resize(n, 1);
  cntw.resize(n, 0);
  ansb.resize(n);
  answ.resize(n);
  dfs1(0, -1, tr);
  //printv(cntb);
  //printv(cntw);
  dfs2(0, -1, 0, 0, tr);
  for(int i=0;i<n;++i) {
    cout << ansb[i] << endl;
  }
}


Measured memory_bytes: 47872