#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using ld = long double;

using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vb = vector<bool>;
using vs = vector<string>;
using vld = vector<ld>;
using vvld = vector<vld>;

typedef pair<ll, ll> P;

#define bit(n) (1LL << (n))

//#define int long long

#define all(v) v.begin(), v.end()

#define rep(i, n) for (ll i = 0; i < n; i++)
#define REP(i, n) for (ll i = 1; i < n; i++)

#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define FORm(i, m) for (auto i = m.begin(); i != m.end(); i++)

template <class T>
inline void chmax(T& a, T b) {
  a = std::max(a, b);
}
template <class T>
inline void chmin(T& a, T b) {
  a = std::min(a, b);
}

#define mod (ll)(1e9 + 7)
// #define mod (998244353ll)

const long long INF = 1LL << 60;

// #define mod (ll)(1e9 + 7)

ll ModVal = mod;

struct ModInt {
  ll x;

  ModInt(ll _x = 0) : x((_x % ModVal + ModVal) % ModVal) {
  }

  ModInt operator-() {
    return ModInt(-x);
  }
  ModInt& operator+=(const ModInt a) {
    x += a.x;
    if (x >= ModVal)
      x -= ModVal;
    return *this;
  }
  ModInt& operator-=(const ModInt a) {
    x = x + ModVal - a.x;
    if (x >= ModVal)
      x -= ModVal;
    return *this;
  }
  ModInt& operator*=(const ModInt a) {
    x *= a.x;
    x %= ModVal;
    return *this;
  }

  ll ext_gcd(ll a, ll b, ll& x, ll& y) {
    if (b == 0) {
      x = 1;
      y = 0;
      return a;
    }
    ll tmp = a / b;
    ll d = ext_gcd(b, a - b * tmp, y, x);
    y -= tmp * x;
    return d;
  }

  // 逆元
  ModInt inv() {
    ll u, v;
    ext_gcd(x, ModVal, u, v);
    return ModInt(u);
  }

  ModInt inv(const ModInt a) {
    ll u, v;
    ext_gcd(a.x, ModVal, u, v);
    return ModInt(u);
  }

  ModInt& operator/=(const ModInt a) {
    return (*this) *= inv(a);
  }

  ModInt operator+(const ModInt a) {
    ModInt retval(*this);
    return retval += a;
  }
  ModInt operator-(const ModInt a) {
    ModInt retval(*this);
    return retval -= a;
  }
  ModInt operator*(const ModInt a) {
    ModInt retval(*this);
    return retval *= a;
  }
  ModInt operator/(const ModInt a) {
    ModInt retval(*this);
    return retval /= a;
  }

  ModInt pow(ll n) {
    ModInt ans(1);
    while (n) {
      if (n & 1)
        ans = ans * x;
      *this = (*this) * (*this);
      n = n >> 1;
    }
    return ans;
  }

  const ll& value() {
    return this->x;
  }
};

ostream& operator<<(ostream& os, const ModInt& a) {
  os << a.x;
  return os;
}

using mint = ModInt;
using vm = vector<mint>;
using vvm = vector<vm>;
using vvvm = vector<vvm>;

ll n;
vvll g;
vm dp;
vm ans;

void dfs(int idx, int par) {
  dp[idx] = 1;
  for (auto e : g[idx]) {
    if (e == par)
      continue;

    dfs(e, idx);

    // idxが黒
    dp[idx] *= dp[e] + 1;
  }
}

void rerooting(int idx, int par, mint val) {
  ll n = g[idx].size();

  vm ele = vm(n, 1);
  vm mul_l = vm(n + 1, 1);
  vm mul_r = vm(n + 1, 1);

  rep(i, n) {
    if (g[idx][i] != par) {
      mul_l[i + 1] = mul_l[i] * (dp[g[idx][i]] + 1);
    } else {
      mul_l[i + 1] = mul_l[i];
    }
  }
  for (int i = n; i > 0; i--) {
    if (g[idx][i - 1] != par) {
      mul_r[i - 1] = mul_r[i] * (dp[g[idx][i - 1]] + 1);
    } else {
      mul_r[i - 1] = mul_r[i];
    }
  }

  rep(i, n) {
    int to = g[idx][i];
    if (to == par)
      continue;

    mint v = mul_l[i] * mul_r[i + 1];

    rerooting(to, idx, val * v + 1);
  }
  ans[idx] *= val;
}
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(20);

  cin >> n >> ModVal;
  g.resize(n);

  rep(i, n - 1) {
    ll x, y;
    cin >> x >> y;
    x--;
    y--;
    g[x].push_back(y);
    g[y].push_back(x);
  }

  dp = vm(n);

  dfs(0, 0);
  ans = dp;

  rerooting(0, 0, 1);

  rep(i, n) {
    cout << ans[i] << endl;
  }

  return 0;
}


Measured memory_bytes: 22144