#include <cstdio>
#include <functional>
#include <vector>
#include <tuple>

using namespace std;

using ll = long long;
ll mod = 1LL<<32;
ll add(ll l, ll r) { return (((l)+(r))%mod); }
ll mul(ll l, ll r) { return (((l)*(r))%mod); }
// function<ll (ll, ll)> get_mul(ll m) { return [=](ll l, ll r) { return (((l)*(r))%m); }; }

constexpr ll nearest_pow2(ll n)
{
  ll i = 1;
  while (i < n) i <<= 1;
  return i;
}

template<typename T>
struct Node {
  T item;
  int left;
  int right;
};

template<typename T, ll N, T (*op)(T, T), T id>
struct Segtree {
  Node<T> nodes[2*N+100];
  int index;
  int size;
  int offset;
  int root;
  int levels[100];
  int depth;

  void init()
  {
    index = 0;
  }

  int add(T elem)
  {
    nodes[index].item = elem;
    nodes[index].left = index;
    nodes[index].right = index+1;
    return index++;
  }

  void build()
  {
    if (index == 0) return;
    build1(0, index, 0);
  }

  void build1(int cur, int num, int level)
  {
    while (1) {
    root = cur;
    depth = level;
    levels[level] = num;
    if (num == 1) return;
    int i = 0;
    // cur++;
    // nodes[cur-1+num] = num; // 0;
    for (; (i<<1)+1 < num; i++) {
      Node<T> l = nodes[cur+(i<<1)];
      Node<T> r = nodes[cur+(i<<1)+1];
      nodes[cur+num+i].item = op(l.item, r.item);
      nodes[cur+num+i].left = l.left;
      nodes[cur+num+i].right = r.right;
    }
    if ((i<<1) < num) {
      nodes[cur+num+i].item = nodes[cur+(i<<1)].item;
      nodes[cur+num+i].left = nodes[cur+(i<<1)].left;
      nodes[cur+num+i].right = nodes[cur+(i<<1)].right;
      i++;
      // nodes[cur-1+num] = 1;
    }
    cur += num;
    num = i;
    level++;
    }
  }

  T query(int left, int right)
  {
    if (left == right) return id;
    if (right <= 0 || index <= left) return id;
    return query1(left, right, depth, root, 0);
  }

  T query1(int left, int right, int l, int cur, int idx)
  {
    int curl, curr;
    if ((left < 0) || (index < right) || (cur < 0) || (root < cur+idx)) { printf("((left < 0) || (index < right) || (cur < 0) || (root < cur+idx))\n"); exit(0); }
    curl = nodes[cur+idx].left;
    curr = nodes[cur+idx].right;
    if (left == curl && right == curr) return nodes[cur+idx].item;
    if (l == 0) { printf("l == 0"); exit(0); }
    int nodes_oneleveldeeper = levels[l-1];
    int next = cur-nodes_oneleveldeeper;
    int lidx = idx<<1;
    int ridx = lidx+1;
    if (nodes[next+lidx].left <= left && right <= nodes[next+lidx].right)
      return query1(left, right, l-1, next, lidx);
    if (ridx >= nodes_oneleveldeeper) { printf("if (ridx >= nodes_oneleveldeeper)"); exit(0); }
    if (nodes[next+ridx].left <= left && right <= nodes[next+ridx].right)
      return query1(left, right, l-1, next, ridx);
    return op(query1(left, nodes[next+lidx].right, l-1, next, lidx),
              query1(nodes[next+ridx].left, right, l-1, next, ridx));
  }
};

vector<tuple<int, ll, int, ll>> G[100000];
ll ans[100000];

ll dfs(int cur, int last)
{
  // if (cur%100==0) printf("dfs(%d, %d)\n", cur, last);
  ll combinations = 1;
  for (auto& ncid : G[cur]) {
    int n = get<0>(ncid);
    if (n == last) continue;
    ll c = dfs(n, cur);
    get<1>(ncid) = c;
    combinations = mul(combinations, add(c, 1));
  }
  return combinations;
}

void dfs2(int cur, int last, ll lcc)
{
  static Segtree<ll, 100000, &mul, 1> tree;

  tree.init();
  for (auto& ncid : G[cur]) {
    int n = get<0>(ncid);
    ll c;
    if (n == last) {
      c = lcc;
    }
    else {
      c = get<1>(ncid);
    }
    get<2>(ncid) = tree.add(add(c, 1));
  }
  // printf("tree.build()\n");
  tree.build();

  for (auto& ncid : G[cur]) {
    int n = get<0>(ncid);
    if (n == last) continue;
    int i = get<2>(ncid);
    ll cnc = mul(tree.query(0, i), tree.query(i+1, G[cur].size()));
    get<3>(ncid) = cnc;
  }
  ans[cur] = tree.query(0, G[cur].size());
  for (auto& ncid : G[cur]) {
    int n = get<0>(ncid);
    if (n == last) continue;
    ll cnc = get<3>(ncid);
    dfs2(n, cur, cnc);
  }
}

int main_()
{
  int n;
  ll m;
  scanf("%d %lld", &n, &m);
  mod = m;
  for (int i = 0; i < n-1; i++) {
    int u, v;
    scanf("%d %d", &u, &v);
    u--;
    v--;
    G[u].push_back(make_tuple(v, 1, -1, 1));
    G[v].push_back(make_tuple(u, 1, -1, 1));
  }

  dfs(0, -1);
  // printf("dfs2\n");
  dfs2(0, -1, 0);
  for (int i = 0; i < n; i++) {
    printf("%lld\n", ans[i]);
  }
  return 0;
}

int main()
{
  ll rsp_orig, rsp_new;
  int stack_size = 8*100000*30+0x100;
  rsp_new = (((ll) malloc(stack_size))+stack_size)&(~0xffLL);
  // __asm__ volatile("int3");
  __asm__ volatile("mov %%rsp, %0" : "=r"(rsp_orig));
  // printf("%lld\n", rsp_orig);
  __asm__ volatile("mov %0, %%rsp" : : "r"(rsp_new));
  main_();
  __asm__ volatile("mov %0, %%rsp" : : "r"(rsp_orig));
  return 0;
}


Measured memory_bytes: 33408