#include <iostream>
#include <algorithm>
#include <utility>
#include <vector>
#include <numeric>
#include <tuple>

template <class T, class U>
inline bool chmin(T& lhs, const U& rhs) {
  if (lhs > rhs) {
    lhs = rhs;
    return true;
  }
  return false;
}

template <class T, class U>
inline bool chmax(T& lhs, const U& rhs) {
  if (lhs < rhs) {
    lhs = rhs;
    return true;
  }
  return false;
}

// [l, r) from l to r
struct range {
  struct itr {
    int i;
    constexpr itr(int i_): i(i_) { }
    constexpr void operator ++ () { ++i; }
    constexpr int operator * () const { return i; }
    constexpr bool operator != (itr x) const { return i != x.i; }
  };
  const itr l, r;
  constexpr range(int l_, int r_): l(l_), r(std::max<int>(l_, r_)) { }
  constexpr itr begin() const { return l; }
  constexpr itr end() const { return r; }
};

// [l, r) from r to l
struct revrange {
  struct itr {
    int i;
    constexpr itr(int i_): i(i_) { }
    constexpr void operator ++ () { --i; }
    constexpr int operator * () const { return i; }
    constexpr bool operator != (itr x) const { return i != x.i; }
  };
  const itr r, l;
  constexpr revrange(int l_, int r_): l(l_ - 1), r(std::max<int>(l_, r_) - 1) { }
  constexpr itr begin() const { return r; }
  constexpr itr end() const { return l; }
};

template <class T>
class runtime_modulo_int {
public:
  static int mod() {
    return T::value;
  }
private:
  long long value;
  void normalize() {
    value %= mod();
    if (value < 0) value += mod();
  }
public:
  runtime_modulo_int(long long value_ = 0): value(value_) { normalize(); }
  runtime_modulo_int operator - () const { return runtime_modulo_int(mod() - value); }
  runtime_modulo_int operator ~ () const { return power(mod() - 2); }
  long long operator () () const { return value; }
  runtime_modulo_int operator + (const runtime_modulo_int& rhs) const { return runtime_modulo_int(*this) += rhs; }
  runtime_modulo_int& operator += (const runtime_modulo_int& rhs) {
    if ((value += rhs.value) >= mod()) value -= mod();
    return (*this);
  }
  runtime_modulo_int operator - (const runtime_modulo_int& rhs) const { return runtime_modulo_int(*this) -= rhs; }
  runtime_modulo_int& operator -= (const runtime_modulo_int& rhs) {
    if ((value += mod() - rhs.value) >= mod()) value -= mod();
    return (*this);
  }
  runtime_modulo_int operator * (const runtime_modulo_int& rhs) const { return runtime_modulo_int(*this) *= rhs; }
  runtime_modulo_int& operator *= (const runtime_modulo_int& rhs) {
    (value *= rhs.value) %= mod();
    return (*this);
  }
  runtime_modulo_int operator / (const runtime_modulo_int& rhs) const { return runtime_modulo_int(*this) /= rhs; }
  runtime_modulo_int& operator /= (const runtime_modulo_int& rhs) {
    return (*this) *= ~rhs;
  }
  runtime_modulo_int power (unsigned long long pow) const {
    runtime_modulo_int result(1), mult(*this);
    while (pow > 0) {
      if (pow & 1) result *= mult;
      mult *= mult;
      pow >>= 1;
    }
    return result;
  }
  friend std::istream& operator >> (std::istream& stream, runtime_modulo_int& lhs) {
    stream >> lhs.value;
    lhs.normalize();
    return stream;
  }
  friend std::ostream& operator << (std::ostream& stream, const runtime_modulo_int& rhs) {
    return stream << rhs.value;
  }
};

struct runtime_mod { static int value; };
int runtime_mod::value;
int &mod = runtime_mod::value;
using modint = runtime_modulo_int<runtime_mod>;

template <class T>
struct fix_point: private T {
  explicit constexpr fix_point(T &&func): T(std::forward<T>(func)) { }
  template <class... Args>
  constexpr decltype(auto) operator () (Args &&... args) const {
    return T::operator()(*this, std::forward<Args>(args)...);
  }
};

template <class T>
constexpr decltype(auto) make_fix_point(T &&func) {
  return fix_point<T>{std::forward<T>(func)};
}

int main() {
  int N;
  std::cin >> N >> mod;
  std::vector<std::vector<int>> graph(N);
  for (int i: range(0, N - 1)) {
    int x, y;
    std::cin >> x >> y;
    --x; --y;
    graph[x].push_back(y);
    graph[y].push_back(x);
  }
  std::vector<std::vector<modint>> dp(N);
  for (int u: range(0, N)) {
    dp[u].assign(graph[u].size(), modint(0));
  }
  make_fix_point([&](auto &&dfs, int u, int p) -> modint {
    modint res(1);
    for (int i: range(0, graph[u].size())) {
      if (graph[u][i] != p) {
        dp[u][i] = dfs(graph[u][i], u);
        res *= dp[u][i];
      }
    }
    return res + 1;
  })(0, -1);
  make_fix_point([&](auto &&apply, int u, int p, modint m) -> void {
    unsigned size = graph[u].size();
    for (int i: range(0, size)) {
      if (graph[u][i] == p) {
        dp[u][i] = m;
      }
    }
    std::vector<modint> left(size + 1), right(size + 1);
    left[0] = right[0] = 1;
    for (int i: range(0, size)) {
      left[i + 1] = left[i] * dp[u][i];
      right[i + 1] = right[i] * dp[u][size - i - 1];
    }
    for (int i: range(0, size)) {
      if (graph[u][i] != p) {
        apply(graph[u][i], u, left[i] * right[size - i - 1] + 1);
      }
    }
  })(0, -1, 0);
  for (int u: range(0, N)) {
    modint ans(1);
    for (modint m: dp[u]) {
      ans *= m;
    }
    std::cout << ans << '\n';
  }
  return 0;
}


Measured memory_bytes: 22400