#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define repi(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,a) repi(i,0,a)
#define all(a) (a).begin(), (a).end()

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using ll = long long;

struct mint {
  ll x, mod; // typedef long long ll;
  mint(ll x=0, ll mod=1000000007):x((x%mod+mod)%mod), mod(mod){}
  mint operator-() const { return mint(-x,mod);}
  mint& operator+=(const mint a) {
    if ((x += a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator-=(const mint a) {
    if ((x += mod-a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const {
    mint res(*this);
    return res+=a;
  }
  mint operator-(const mint a) const {
    mint res(*this);
    return res-=a;
  }
  mint operator*(const mint a) const {
    mint res(*this);
    return res*=a;
  }
  mint pow(ll t) const {
    if (!t) return mint(1,mod);
    mint a = pow(t>>1);
    a *= a;
    if (t&1) a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const {
    return pow(mod-2);
  }
  mint& operator/=(const mint a) {
    return (*this) *= a.inv();
  }
  mint operator/(const mint a) const {
    mint res(*this);
    return res/=a;
  }
};

struct combination {
  std::vector<mint> fact, ifact;
  ll mod;
  combination(int n,ll mod=1000000007):fact(n+1),ifact(n+1),mod(mod) {
    assert(n < mod);
    fact[0] = mint(1,mod);
    for (int i = 1; i <= n; ++i) fact[i] = fact[i-1]*i;
    ifact[n] = fact[n].inv();
    for (int i = n; i >= 1; --i) ifact[i-1] = ifact[i]*i;
  }
  mint operator()(int n, int k) {
    if (k < 0 || k > n) return mint(0,mod);
    return fact[n]*ifact[k]*ifact[n-k];
  }
};

ll N, M;
std::vector<ll> G[100010];
bool used[100010];
mint dp[100010];

void clear()
{
  rep( i, N+1 )
    used[i] = false;

  return;
}

void dfs1( ll v, ll p )
{
  used[v] = true;

  dp[v] = mint(1,M);

  for( auto u : G[v] ) if( u != p )
  {
    dfs1( u, v );

    dp[v] *= dp[u]+mint(1,M);
  }

  return;
}

void dfs2( ll v, ll p, mint par )
{
  used[v] = true;

  std::vector<mint> left( G[v].size()+1, mint( 1, M ) ), right( G[v].size()+1, mint( 1, M ) );

  rep( i, G[v].size() )
    left[i+1] = left[i]*(G[v][i]==p ? par+1 : dp[G[v][i]]+mint(1,M));

  for( int i = G[v].size()-1; i >= 1; --i )
    right[i-1] = right[i]*(G[v][i]==p ? par+1 : dp[G[v][i]]+mint(1,M));

  rep( i, G[v].size() ) if( G[v][i] != p )
    dfs2( G[v][i], v, left[i]*right[i] );

  dp[v] *= par+mint(1,M);

  return;
}

int main()
{
  std::cin >> N >> M;

  rep( i, N+1 ) rep( j, 3 )
    dp[i].mod = M;

  rep( i, N-1 )
  {
    ll x, y;
    std::cin >> x >> y;

    --x; --y;

    G[x].emplace_back( y );
    G[y].emplace_back( x );
  }

  clear();
  dfs1( 0, -1 );

  clear();
  dfs2( 0, -1, 0 );

  rep( i, N )
    std::cout << dp[i].x << std::endl;

  return 0;
}

Measured memory_bytes: 26880