#include <algorithm>
#include <array>
#include <cassert>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define FOR(i,a,b) for (int i=(a),for_##i##_max=(b);i<=(for_##i##_max);++i)
#define RFOR(i,a,b) for (int i=(a),rfor_##i##_min=(b);i>=(rfor_##i##_min);--i)
#define REP(i,n) for (int i=0,rep_##i##_len=(n);i<(rep_##i##_len);++i)
#define RREP(i,n) for (int i=(n)-1;i>=0;--i)

namespace { // Basics

template<typename T>
void chmin(T &x, T y) {
  x = std::min(x, y);
}

template<typename T>
void chmax(T &x, T y) {
  x = std::max(x, y);
}

template<typename T>
__attribute__((unused))
const auto minimum = static_cast<T const &(*)(T const &, T const &)>(std::min);

template<typename T>
__attribute__((unused))
const auto maximum = static_cast<T const &(*)(T const &, T const &)>(std::max);

template<typename T>
int cmp3(T lhs, T rhs) { return (lhs == rhs) ? 0 : (lhs > rhs) ? 1 : -1; }

template<typename T>
__attribute__((unused))
T constexpr infty = std::numeric_limits<T>::max / 3;
template<>
__attribute__((unused))
auto constexpr infty<int> = 1'100'100'100;
template<>
__attribute__((unused))
auto constexpr infty<long long int> = 100'500'400'300'200'100LL;

using lli = long long int;
using ld = long double;

} // namespace
namespace { // Graph Basics

template<typename WeightT>
struct edge {
  int from, to;
  WeightT weight;
  edge(int s, int d, WeightT w): from(s), to(d), weight(w) {}
};

template<typename WeightT>
bool
operator< (edge<WeightT> const &lhs, edge<WeightT> const &rhs) {
  if (lhs.weight != rhs.weight) {
    return lhs.weight < rhs.weight;
  } else if (lhs.from != rhs.from) {
    return lhs.from < rhs.from;
  }
  return lhs.to < rhs.to;
}

template<typename WeightT>
using edges = std::vector<edge<WeightT> >;

template<typename WeightT>
using adjacency_list = std::vector<edges<WeightT> >;

template<typename WeightT>
void
add_undir_edge(adjacency_list<WeightT> &G, int u, int v, WeightT w) {
  G[u].emplace_back(u, v, w);
  G[v].emplace_back(v, u, w);
}

template<typename WeightT>
adjacency_list<WeightT>
inverse_graph(adjacency_list<WeightT> const &G) {
  size_t const n = G.size();
  adjacency_list<WeightT> F(n);
  for (auto &&es: G) for (auto &&e: es) {
    F[e.to].emplace_back(e.to, e.from, e.weight);
  }
  return F;
}

template <typename WeightT>
edges<WeightT>
all_edges(adjacency_list<WeightT> const &G) {
  auto const E = std::accumulate(begin(G), end(G), 0,
      [](auto x, auto vec) { return x + vec.size(); });
  edges<WeightT> es;
  es.reserve(E);
  for (auto &&vec: G) {
    std::copy(begin(vec), end(vec), std::back_inserter(es));
  }
  return es;
}

} // namespace
namespace { // rooted_tree

struct rooted_tree {
  size_t size;
  std::vector<size_t> topo;
  std::vector<size_t> par;
  std::vector<std::vector<size_t>> children;
};

template <typename WeightT>
rooted_tree make_rooted_tree(adjacency_list<WeightT> const &G, size_t root) {
  size_t N = G.size();
  std::vector<size_t> topo, par(N);
  par[root] = N + 1;
  std::vector<std::vector<size_t>> ch(N);

  std::vector<bool> used(N, false);
  std::stack<size_t> stk;
  stk.push(root); used[root] = true;
  while (!stk.empty()) {
    size_t const u = stk.top(); stk.pop();
    topo.push_back(u);
    for (auto &&e : G[u]) {
      if (used[e.to]) { continue; }
      stk.push(e.to); used[e.to] = true;
      par[e.to] = u;
      ch[u].push_back(e.to);
    }
  }
  return { N, topo, par, ch };
}

} // namespace
namespace { // segment-tree

template<typename T, typename FnT>
class segment_tree {
  public:
    explicit segment_tree(FnT const &app, T const &un, size_t size):
      segment_tree(app, un, std::vector<T>(size, un)) {
    }
    explicit segment_tree(FnT const &app, T const &un, std::vector<T> const &v):
      append(app), unit(un), N(1 << log2(v.size())), dat(2*N-1, un) {
      std::copy(begin(v), end(v), begin(dat)+N-1);
      for (size_t k = N - 1; k > 0; --k) {
        dat[k-1] = append(dat[2*k-1], dat[2*k]);
      }
    }
    void update(size_t k, T x) {
      k += N - 1;
      dat[k] = x;
      while (k > 0) {
        k = (k - 1) / 2;
        dat[k] = append(dat[2*k+1], dat[2*k+2]);
      }
    }
    T at(size_t k) const {
      return dat[k+N-1];
    }
    T query(size_t a, size_t b, size_t k, size_t l, size_t r) const {
      if (r <= a || b <= l) { return unit; }
      if (a <= l && r <= b) { return dat[k]; }
      const auto mid = (l + r) / 2;
      const auto vl = query(a, b, 2*k+1, l, mid);
      const auto vr = query(a, b, 2*k+2, mid, r);
      return append(vl, vr);
    }
    T query(size_t a, size_t b) const {
      return query(a, std::min(b, N), 0, 0, N);
    }
    T query() const {
      return query(0, N, 0, 0, N);
    }
    std::vector<T> const &data() const { return dat; }
  private:
    FnT const append;
    T const unit;
    size_t const N;
    std::vector<T> dat;

    static size_t log2(size_t n) {
      size_t p = 1;
      while ((1 << p) < n) { ++p; }
      return p;
    }
};

template<typename T, typename FnT>
segment_tree<T, typename std::decay<FnT>::type>
make_segment_tree(FnT const &app, T const &un, size_t size) {
  return segment_tree<T, typename std::decay<FnT>::type>(app, un, size);
}

template<typename T, typename FnT>
segment_tree<T, typename std::decay<FnT>::type>
make_segment_tree(FnT const &app, T const &un, std::vector<T> const &v) {
  return segment_tree<T, typename std::decay<FnT>::type>(app, un, v);
}

} // namespace

using namespace std;

using Graph = adjacency_list<nullptr_t>;

lli M;
lli mult_modm(lli a, lli b) {
  return (a * b) % M;
}
using Product = segment_tree<lli, decltype(&mult_modm)>;

void solve(int N, Graph const &G) {
  auto const tree = make_rooted_tree(G, 0);
  vector<Product> dr;
  REP(u, N) {
    dr.push_back(make_segment_tree(mult_modm, 1LL, 1+tree.children[u].size()));
  }
  RREP(i, tree.size) {
    size_t u = tree.topo[i];
    REP(j, tree.children[u].size()) {
      size_t v = tree.children[u][j];
      dr[u].update(j, dr[v].query() + 1);
    }
  }

  vector<lli> dpar(N);
  dpar[0] = 0;
  REP(i, N-1) {
    size_t u = tree.topo[i];
    REP(j, tree.children[u].size()) {
      size_t v = tree.children[u][j];
      lli lhs = dr[u].query(0, j), rhs = dr[u].query(j+1, tree.children[u].size());
      dpar[v] = mult_modm(dpar[u]+1, mult_modm(lhs, rhs));
    }
  }
  REP(u, N) {
    cout << mult_modm(dr[u].query(), dpar[u]+1) << endl;
  }
}

int main() {
  int N; cin >> N >> M;
  Graph G(N);
  REP(i, N-1) {
    int X, Y; cin >> X >> Y;
    add_undir_edge(G, --X, --Y, nullptr);
  }
  solve(N, G);
  return 0;
}


Measured memory_bytes: 27820