// Original C++ solution (slower):
#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector")

#include <bits/stdc++.h>

using namespace std;



#define df(b, e) ((b) > (e))

#define fore(i, b, e) for (auto i = (b) - df(b, e); i != e - df(b, e); i += 1 - 2 * df(b, e))

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

#define all(x) begin(x), end(x)

#define f first

#define s second

#define pb push_back

#define eb emplace_back

#define mp make_pair



typedef long long lli;

typedef long double ld;

typedef pair<int, int> ii;

typedef vector<int> vi;



#ifdef LOCAL

  #include "debug.h"

  const int N = 12;

#else

  #define debug(...)

  const int N = 2e5 + 5;

#endif

const int INF = 1e9 + 7;



mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());



vi g[N];

lli dp[N], ans[N], mod;

int n;



lli dfs (int u, int parent = 1) {

  auto &nans = dp[u] = 1;

  for (auto v: g[u]) {

    if (v != parent) { 

      (nans *= dfs(v, u) + 1) %= mod;

    }

  }

  return nans;

}



void solve (int u, int parent = 1) {

  debug(u, parent);

  vector<lli> list;

  for (auto v: g[u]) {

    list.eb(dp[v]);

  }

  debug(list);

  vector<lli> pre, suf;

  for (auto x: list) {

    pre.eb(((sz(pre) ? *pre.rbegin() : 1LL) * (x + 1)) % mod);

  }

  reverse(all(list));

  for (auto x: list) {

    suf.eb(((sz(suf) ? *suf.rbegin() : 1LL) * (x + 1)) % mod);

  }

  reverse(all(suf));

  ans[u] = (sz(list) == 0 ? 1: suf[0]);

  debug(pre);

  debug(suf);

  auto get = [&] (int k) -> lli {

    return ((k == 0 ? 1: pre[k - 1]) * (k == sz(list) - 1 ? 1: suf[k + 1])) % mod;

  };

  int cnt = 0;

  for (auto v: g[u]) {

    if (v != parent) {

      dp[u] = get(cnt);

      solve(v, u);

    }

    cnt++;

  }

}



int main() {

  cin.tie(0) -> sync_with_stdio(0), cout.tie(0);

  int u, v;

  cin >> n >> mod;

  fore (i, 0, n - 1) {

    cin >> u >> v;

    g[u].eb(v);

    g[v].eb(u);

  }

  dfs(1);

  solve(1);

  fore (i, 1, n + 1)

    cout << ans[i] << '\n';

  return 0;

}

// Optimized solution to the same problem (faster):
#include<bits/stdc++.h>

#define REP(x,y,z) for(int x=y;x<=z;x++)

#define MSET(x,y) memset(x,y,sizeof(x))

#define M 100005

using namespace std;

using LL = long long;

int n,m;

LL down[M], ans[M];

vector<int> e[M];

void dfs1(int cur,int fa) {

    down[cur] = 1;

    for (int i: e[cur]) if (i!=fa) {

        dfs1(i, cur);

        down[cur] = down[cur] * (down[i]+1) % m;

    }

}

void dfs2(int cur,int fa,LL up) {

    ans[cur] = down[cur] * (up+1) % m;

    

    int sz = (int)e[cur].size();

    vector<LL> fr, bk;

    fr = vector<LL> (sz);

    bk = vector<LL> (sz);

    REP(i,0,sz-1)               fr[i] = (i>0 ? fr[i-1] : 1LL)    * (e[cur][i]==fa ? (up+1) : (down[e[cur][i]]+1)) % m;

    for (int i=sz-1; i>=0; i--) bk[i] = (i<sz-1 ? bk[i+1] : 1LL) * (e[cur][i]==fa ? (up+1) : (down[e[cur][i]]+1)) % m;

    

    REP(i,0,sz-1) if (e[cur][i]!=fa) {

        LL nup = 1LL;

        if (i>0) nup = nup * fr[i-1] % m;

        if (i<sz-1) nup = nup * bk[i+1] % m;

        dfs2(e[cur][i], cur, nup);

    }

}

int main()

{

    scanf("%d %d", &n, &m);

    REP(i,1,n-1) {

        int x, y;

        scanf("%d %d", &x, &y);

        e[x].push_back(y);

        e[y].push_back(x);

    }



    dfs1(1, -1);

    dfs2(1, -1, 0);

    REP(i,1,n) printf("%lld\n", ans[i]);

    return 0;

}


// Fastest known solution for this problem: