import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.string;

immutable long MOD = 10^^9 + 7;

void main() {
    auto s = readln.split.map!(to!int);
    auto N = s[0];
    auto M = s[1].to!long;
    auto G = new int[][](N);
    foreach (i; 0..N-1) {
        s = readln.split.map!(to!int);
        G[s[0]-1] ~= s[1]-1;
        G[s[1]-1] ~= s[0]-1;
    }


    auto f = new long[](N);
    auto l = new long[][](N);
    auto r = new long[][](N);
    auto g = new long[](N);

    long dfs1(int n, int p) {
        long[] a;
        f[n] = 1;
        foreach (m; G[n]) {
            if (m == p) continue;
            auto v = dfs1(m, n) + 1;
            f[n] = f[n] * v % M;
            a ~= v;
        }

        l[n] = new long[](a.length+1);
        r[n] = new long[](a.length+1);
        l[n][0] = r[n][a.length] = 1;

        foreach (i; 0..a.length.to!int) {
            l[n][i+1] = l[n][i] * a[i] % M;
        }

        foreach_reverse (i; 0..a.length.to!int) {
            r[n][i] = r[n][i+1] * a[i] % M;
        }

        return f[n];
    }


    void dfs2(int n, int p, long pv) {
        if (p == -1) {
            g[n] = f[n];
        } else {
            g[n] = f[n] * pv % M;
        }

        int i = 0;
        foreach (m; G[n]) {
            if (m == p) continue;
            dfs2(m, n, l[n][i] * r[n][i+1] % M * pv % M + 1);
            i += 1;
        }
    }


    dfs1(0, -1);
    dfs2(0, -1, 1);
    g.each!writeln;
}
