#include<iostream>
#include<vector>
#include<stack>
using namespace std;

// Finite field Z/pZ
class ModInt{
private:
  long long val;
  static const int default_mod = 1e9 + 7;
  static int mod;
public:
  ModInt(long long val)
  {
    this->val = val % getMod();
    if(this->val < 0){
      this->val += getMod();
    }
  }
  ModInt()
    : ModInt(0)
  {
  }
  static void setMod(int mod){
    if(ModInt::mod != 0){
      throw "The modulo is already set.";
    }
    ModInt::mod = mod;
  }
  inline static int getMod(){
    if(mod == 0){
      mod = default_mod;
    }
    return mod;
  }
  inline long long getVal() const{
    return val;
  }

  
  ModInt& operator+=(const ModInt&);
  ModInt& operator-=(const ModInt&);
  ModInt& operator*=(const ModInt&);
  ModInt& operator/=(const ModInt&);

  ModInt pow(long long exponent) const{
    exponent %= getMod() - 1;
    long long pow_val = this->val;
    long long ret_val = 1;
    while(exponent > 0){
      if(exponent % 2 == 1){
        ret_val = (ret_val * pow_val) % getMod();
      }
      pow_val = (pow_val * pow_val) % getMod();
      exponent /= 2;
    }
    return ModInt(ret_val);
  }
  ModInt inv() const{
    return this->pow(getMod() - 2);
  }
  friend ostream& operator<< (ostream& os, const ModInt& modint){
    os << modint.val;
    return os;
  }
  friend istream& operator>>(istream& is, ModInt& modint){
    long long val;
    is >> val;
    modint.val = val % modint.getMod();
    return is;
  }
};

int ModInt::mod = 0;

const ModInt operator+(const ModInt& lhs, const ModInt& rhs){
  return ModInt(lhs.getVal() + rhs.getVal());
}
const ModInt operator-(const ModInt& lhs, const ModInt& rhs){
  return ModInt(lhs.getVal() - rhs.getVal());
}
const ModInt operator*(const ModInt& lhs, const ModInt& rhs){
  return ModInt(lhs.getVal() * rhs.getVal());
}
const ModInt operator/(const ModInt& lhs, const ModInt& rhs){
  return lhs.getVal() * rhs.inv();
}


ModInt& ModInt::operator+=(const ModInt& rhs){
  (*this) = (*this) + rhs;
  return *this;
}
ModInt& ModInt::operator-=(const ModInt& rhs){
  (*this) = (*this) - rhs;
  return *this;
}
ModInt& ModInt::operator*=(const ModInt& rhs){
  (*this) = (*this) * rhs;
  return *this;
}
ModInt& ModInt::operator/=(const ModInt& rhs){
  (*this) = (*this) / rhs;
  return *this;
}

class ModIntComb{
private:
  vector<ModInt> facts;
  vector<ModInt> fact_invs;
  int n_max;
public:
  ModIntComb(int n_max): n_max(n_max){
    facts.push_back(1);
    for(int i = 1; i <= n_max; i++){
      facts.push_back(facts[i - 1] * i);
    }
    fact_invs = vector<ModInt>(n_max + 1);
    fact_invs[n_max] = facts[n_max].inv();
    for(int i = n_max - 1; i >= 0; i--){
      fact_invs[i] = fact_invs[i + 1] * (i + 1);
    }
  }
  int getNMax(){
    return n_max;
  }
  ModInt fact(int n){
    return facts[n];
  }
  ModInt fact_inv(int n){
    return fact_invs[n];
  }
  ModInt comb(int n, int k){
    if(k < 0 || k > n){
      return 0;
    }
    return facts[n] * fact_invs[k] * fact_invs[n - k];
  }
};

vector<vector<int>> adj;
vector<ModInt> dp, dp2;

// calc dp
ModInt dfs(int now, int prev){
  ModInt ret = 1;
  for(auto next: adj[now]){
    if(next != prev){
      ret *= (dfs(next, now) + 1);
    }
  }
  return dp[now] = ret;
}

ModInt dfs2(int now, int prev, int prev_prev, const vector<ModInt>& parent_cumprods, const vector<ModInt>& parent_cumprods_rev, int parent_cumprods_idx){
  //cout << "now: " << now << ", prev: " << prev << ", parent_cumprods_idx: " << parent_cumprods_idx << endl;

  ModInt ret = 1;
  if(prev == -1){ // now == 0
    ret = 0;
  }else if(prev_prev == -1){ // prev == 0
    ret = 1;
  }else{
    ret = 1;
  }
  
  if(prev != -1){ // now != 0
    ret *= parent_cumprods[parent_cumprods_idx] * parent_cumprods_rev[parent_cumprods_idx + 1];
    //cout << parent_cumprods[parent_cumprods_idx] << ' ' << parent_cumprods_rev[parent_cumprods_idx + 1] << endl;
    // cumprods[i] := prod of {dp[adj[now][j]] + 1| j < i}
    // cumprods_rev[i] := prod of {dp[adj[now][j]] + 1| j >= i}
    // where dp[adj[now][j]] (adj[now][j] == prev) is regarded as ret
  }
  int adj_size = adj[now].size();
  //  cout << adj_size << endl;
  vector<ModInt> cumprods(adj_size + 1, 1), cumprods_rev(adj_size + 1, 1);
  for(int i = 1; i <= adj_size; i++){
    if(adj[now][i - 1] == prev){
      cumprods[i] = cumprods[i - 1] * (ret + 1);
    }else{
      cumprods[i] = cumprods[i - 1] * (dp[adj[now][i - 1]] + 1);
    }
  }
  for(int i = adj_size - 1; i >= 0; i--){
    if(adj[now][i] == prev){
      cumprods_rev[i] = cumprods_rev[i + 1] * (ret + 1);
    }else{
      cumprods_rev[i] = cumprods_rev[i + 1] * (dp[adj[now][i]] + 1);
    }
  }
  for(int i = 0; i < adj_size; i++){
    int next = adj[now][i];
    //    cout << now << ' ' << next << endl;
    if(next != prev){
      dfs2(next, now, prev, cumprods, cumprods_rev, i);
    }
  }

  return dp2[now] = ret;
}

int main(){
  int N, M;
  cin >> N >> M;

  ModInt::setMod(M);

  adj = vector<vector<int>>(N);
  for(int i = 0; i < N - 1; i++){
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    adj[x].push_back(y);
    adj[y].push_back(x);
  }

  // dp[i] := the answer for the subtree whose root is i of the tree whose root is 0
  dp = vector<ModInt>(N);
  dfs(0, -1);

  // dp2[i] := the answer for the "overtree" whose root is the parent of i...
  // dp2[0] = 1
  // dp2[i] = (dp2[parent(i)] + 1) * \prod(dp[j] + 1) where j is a child of parent(i) and j != i
  dp2 = vector<ModInt>(N);
  dfs2(0, -1, -1, vector<ModInt>(0), vector<ModInt>(0), 0);

  /*
  for(auto mi: dp){
    cout << mi << endl;
  }
  for(auto mi: dp2){
    cout << mi << endl;
  }
  */


  for(int i = 0; i < N; i++){
    cout << dp[i] * (dp2[i] + 1) << endl;
  }

  
}


Measured memory_bytes: 23168