#include <bits/stdc++.h>



template <typename Cost = int> class Edge{
public:
  int from,to;
  Cost cost;
  Edge() {}
  Edge(int to, Cost cost): to(to), cost(cost){}
  Edge(int from, int to, Cost cost): from(from), to(to), cost(cost){}

  Edge rev() const {return Edge(to,from,cost);}
  
  friend std::ostream& operator<<(std::ostream &os, const Edge &e){
    os << "(FROM: " << e.from << "," << "TO: " << e.to << "," << "COST: " << e.cost << ")";
    return os;
  }
};

template <typename T> using Graph = std::vector<std::vector<Edge<T>>>;
template <typename T> using Tree = std::vector<std::vector<Edge<T>>>;

template <typename C, typename T> void add_edge(C &g, int from, int to, T w){
  g[from].emplace_back(from, to, w);
}

template <typename C, typename T> void add_undirected(C &g, int a, int b, T w){
  add_edge(g, a, b, w);
  add_edge(g, b, a, w);
}


/**
 * @attention 使用前にstatic変数Mを設定する。
 */
class RuntimeModInt{
public:
  static std::uint32_t M;
  
  std::uint64_t val;
  RuntimeModInt(): val(0){}
  RuntimeModInt(std::int64_t n){
    if(n >= M) val = n % M;
    else if(n < 0) val = n % M + M;
    else val = n;
  }
  
  const auto operator+(const RuntimeModInt &a) const {return RuntimeModInt(val + a.val);}
  const auto operator-(const RuntimeModInt &a) const {return RuntimeModInt(val - a.val);}
  const auto operator*(const RuntimeModInt &a) const {return RuntimeModInt(val * a.val);}
  const auto operator/(const RuntimeModInt &a) const {return RuntimeModInt(val * a.inv().val);}
  
  const auto& operator=(const RuntimeModInt &a){val = a.val; return *this;}
  const auto& operator+=(const RuntimeModInt &a){if((val += a.val) >= M) val -= M; return *this;}
  const auto& operator-=(const RuntimeModInt &a){if(val < a.val) val += M; val -= a.val; return *this;}
  const auto& operator*=(const RuntimeModInt &a){(val *= a.val) %= M; return *this;}
  const auto& operator/=(const RuntimeModInt &a){(val *= a.inv().val) %= M; return *this;}

  const bool operator==(const RuntimeModInt &a) const {return val == a.val;}
  const bool operator!=(const RuntimeModInt &a) const {return val != a.val;}

  const static auto power(std::int64_t n, std::int64_t p){
    RuntimeModInt ret = 1, e = n;
    for(; p; e *= e, p >>= 1) if(p & 1) ret *= e;
    return ret;
  }

  const auto power(std::int64_t p) const {
    RuntimeModInt ret = 1, e = val;
    for(; p; e *= e, p >>= 1) if(p & 1) ret *= e;
    return ret;
  }
  
  const RuntimeModInt inv() const {
    std::int64_t a = val, b = M, u = 1, v = 0;

    while(b){
      std::int64_t t = a/b;
      a -= t*b; std::swap(a,b);
      u -= t*v; std::swap(u,v);
    }
    u %= M;
    if(u < 0) u += M;
    
    return u;
  }
};

auto operator-(const RuntimeModInt &a){return RuntimeModInt(-a.val);}

auto operator+(std::int64_t a, const RuntimeModInt &b){return RuntimeModInt(a) + b;}
auto operator-(std::int64_t a, const RuntimeModInt &b){return RuntimeModInt(a) - b;}
auto operator*(std::int64_t a, const RuntimeModInt &b){return RuntimeModInt(a) * b;}
auto operator/(std::int64_t a, const RuntimeModInt &b){return RuntimeModInt(a) / b;}

std::uint32_t RuntimeModInt::M;

std::istream& operator>>(std::istream &is, RuntimeModInt &a){is >> a.val; return is;}
std::ostream& operator<<(std::ostream &os, const RuntimeModInt &a){os << a.val; return os;}


using mint = RuntimeModInt;



template <typename T>
void rooting(Tree<T> &tree, int cur, int par = -1){
  if(par != -1){
    for(auto it = tree[cur].begin(); it != tree[cur].end(); ++it){
      if(it->to == par){
        tree[cur].erase(it);
        break;
      }
    }
  }

  for(auto &e : tree[cur]){
    rooting(tree, e.to, cur);
  }
}



template <typename F>
struct FixPoint : F{
  explicit constexpr FixPoint(F &&f) noexcept : F(std::forward<F>(f)){}

  template <typename... Args>
  constexpr decltype(auto) operator()(Args &&... args) const {
    return F::operator()(*this, std::forward<Args>(args)...);
  }
};

template <typename F>
static inline constexpr decltype(auto) make_fix_point(F &&f){
  return FixPoint<F>(std::forward<F>(f));
}



























int main(){
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);

  int N, M;
  std::cin >> N >> M;
  mint::M = M;
  
  Tree<int> tree(N);
  for(int i = 0; i < N-1; ++i){
    int x,y; std::cin >> x >> y;
    --x, --y;
    add_undirected(tree, x, y, 1);
  }

  rooting(tree, 0);


  std::vector<mint> dp1(N);
  std::vector<mint> dp2(N, 1);

  {
    auto rec =
      make_fix_point([&](auto &f, int cur) -> mint{
                       dp1[cur] = 1;

                       for(auto &e : tree[cur]){
                         dp1[cur] = dp1[cur] * (f(e.to) + 1); 
                       }

                       return dp1[cur];
                     });
    rec(0);
  }
  
  {
    auto rec =
      make_fix_point([&](auto &f, int cur, int par, mint left_acc, mint right_acc) -> void{
                       if(par != -1){
                         dp2[cur] = dp2[par] * left_acc * right_acc + 1;
                       }

                       int l = tree[cur].size();

                       std::vector<mint> left(l+1, 1), right(l+1, 1);

                       for(int i = 0; i < l-1; ++i){
                         left[i+1] = left[i] * (dp1[tree[cur][i].to] + 1);
                       }

                       for(int i = l-1; i >= 1; --i){
                         right[i-1] = right[i] * (dp1[tree[cur][i].to] + 1);
                       }

                       for(int i = 0; i < l; ++i){
                         auto &e = tree[cur][i];
                         f(e.to, cur, left[i], right[i]);
                       }
                     });
    rec(0, -1, 1, 1);
  }
  
  for(int i = 0; i < N; ++i){
    mint ans = dp2[i] * dp1[i];
    std::cout << ans << "\n";
  }
  
  return 0;
}


Measured memory_bytes: 19840