#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 T, typename U, typename Merge, typename F, typename G>
struct Rerooting{
  int N;
  Tree<T> tree;
  U id;
  Merge merge;
  F f;
  G g;

  std::vector<int> p;
  
  std::vector<std::vector<U>> dp;
  std::vector<U> result;
  
  Rerooting(Tree<T> tree, U id, Merge merge, F f, G g):
    N(tree.size()), tree(tree), id(id), merge(merge), f(f), g(g), p(N, -1), dp(N), result(N, id)
  {
    rooting(0);
    rec1(0);
    rec2(0, -1, id);
    for(int i = 0; i < N; ++i){
      for(int j = 0; j < (int)tree[i].size(); ++j){
        result[i] = merge(result[i], f(dp[i][j], tree[i][j]));
      }
      
      result[i] = g(result[i], i);
    }
  }

  void rooting(int cur, int par = -1){
    p[cur] = par;

    dp[cur].assign((int)tree[cur].size(), id);

    for(auto &e : tree[cur]){
      if(e.to == par){
        std::swap(e, *tree[cur].rbegin());
        break;
      }
    }

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

  U rec1(int cur, int par = -1){
    U acc = id;
    
    for(int i = 0; i < (int)tree[cur].size(); ++i){
      auto &e = tree[cur][i];
      if(e.to == par) continue;
      dp[cur][i] = rec1(e.to, cur);
      acc = merge(acc, f(dp[cur][i], e));
    }

    return g(acc, cur);
  }

  void rec2(int cur, int par, U value){
    int l = tree[cur].size();

    if(tree[cur].size() and tree[cur].back().to == par){
      dp[cur].back() = value;
      l -= 1;
    }

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

    for(int i = 0; i < l-1; ++i){
      auto &e = tree[cur][i];
      left[i+1] = merge(left[i], f(dp[cur][i], e));
    }

    for(int i = l-1; i >= 1; --i){
      auto &e = tree[cur][i];
      right[i-1] = merge(right[i], f(dp[cur][i], e));
    }

    for(int i = 0; i < l; ++i){
      auto &e = tree[cur][i];

      auto v = merge(left[i], right[i]);
      if(par != -1) v = merge(v, f(value, tree[cur].back()));
      
      rec2(e.to, cur, g(v, cur));
    }
  }
};






















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);
  }

  auto merge = [](const auto &a, const auto &b){return a * b;};
  auto f = [](const auto &a, const auto &e){return a + 1;};
  auto g = [](const auto &a, int){return a;};
  
  Rerooting<int,mint,decltype(merge),decltype(f),decltype(g)> r(tree, 1, merge, f, g);

  for(auto x : r.result){
    std::cout << x << "\n";
  }
  
  return 0;
}


Measured memory_bytes: 33280