import std.conv, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.container, std.math, std.range, std.regex, std.typecons;
import core.bitop, core.thread;

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) throw new EOFException; tokens = readln.split; } auto token = tokens[0]; tokens.popFront; return token; }
int readInt() { return readToken().to!int; }
long readLong() { return readToken().to!long; }
real readReal() { return readToken().to!real; }

void chmin(T)(ref T t, in T f) { if (t > f) t = f; }
void chmax(T)(ref T t, in T f) { if (t < f) t = f; }

int binarySearch(T)(in T[] as, in bool delegate(T) test) { int low = -1, upp = cast(int)(as.length); for (; low + 1 < upp; ) { int mid = (low + upp) >> 1; (test(as[mid]) ? low : upp) = mid; } return upp; }
int lowerBound(T)(in T[] as, in T val) { return as.binarySearch((T a) => (a < val)); }
int upperBound(T)(in T[] as, in T val) { return as.binarySearch((T a) => (a <= val)); }


int N;
long M;
int[] X, Y;

int[][] graph, children;
long[] dp0, dp1;
long[][] ls, rs;

void dfs0(int u, int p) {
  foreach (v; graph[u]) {
    if (v != p) {
      children[u] ~= v;
    }
  }
  foreach (v; children[u]) {
    dfs0(v, u);
  }
  dp0[u] = 1;
  foreach (v; children[u]) {
    (dp0[u] *= (dp0[v] + 1)) %= M;
  }
}

void dfs1(int u) {
  foreach (j, v; children[u]) {
    dp1[v] = 1;
    (dp1[v] *= dp1[u]) %= M;
    (dp1[v] *= ls[u][j]) %= M;
    (dp1[v] *= rs[u][j + 1]) %= M;
    ++dp1[v];
  }
  foreach (v; children[u]) {
    dfs1(v);
  }
}

void main() {
  try {
    for (; ; ) {
      N = readInt();
      M = readLong();
      X = new int[N - 1];
      Y = new int[N - 1];
      foreach (i; 0 .. N - 1) {
        X[i] = readInt() - 1;
        Y[i] = readInt() - 1;
      }
      graph = new int[][N];
      foreach (i; 0 .. N - 1) {
        graph[X[i]] ~= Y[i];
        graph[Y[i]] ~= X[i];
      }
      children = new int[][N];
      dp0 = new long[N];
      dfs0(0, -1);
      ls = new long[][N];
      rs = new long[][N];
      foreach (u; 0 .. N) {
        ls[u] = new long[children[u].length + 1];
        rs[u] = new long[children[u].length + 1];
        ls[u][0] = 1;
        foreach (j, v; children[u]) {
          ls[u][j + 1] = (ls[u][j] * (dp0[v] + 1)) % M;
        }
        rs[u][children[u].length] = 1;
        foreach_reverse (j, v; children[u]) {
          rs[u][j] = (rs[u][j + 1] * (dp0[v] + 1)) % M;
        }
      }
      dp1 = new long[N];
      dp1[0] = 1;
      dfs1(0);
      foreach (u; 0 .. N) {
        long ans = 1;
        (ans *= dp1[u]) %= M;
        (ans *= ls[u][children[u].length]) %= M;
        writeln(ans);
      }
      debug {
        writefln("dp0 = %s", dp0);
        writefln("dp1 = %s", dp1);
      }
    }
  } catch (EOFException e) {
  }
}
