#include <bits/stdc++.h>

using namespace std;

//#define FILE_IO

class Integer {
public:
    //Pregateste clasa pentru interactiunea cu un modul - ii calculeaza phi-ul si descompunerea in factori primi
    static void prepare_MOD(int mod) {
        MOD = mod;
        phi = MOD;
        p_sz = 0;
        memset(p, 0, sizeof p);
        for (int i = 2; i * i <= mod; ++ i)
            if (mod % i == 0) {
                p[p_sz ++] = i;
                phi /= i;
                phi *= (i - 1);

                while (mod % i == 0)
                    mod /= i;
            }

        if (mod > 1) {
            p[p_sz ++] = mod;
            phi /= mod;
            phi *= (mod - 1);
            mod = 1;
        }
    }

    Integer() {
        memset(k, 0, sizeof k);
        val = 1;
    }

    Integer(const int &arg) {
        memset(k, 0, sizeof k);
        val = arg;
        if(val == 0)
        {
            for(int i = 0; i < p_sz; i++)
                k[i] = int(1e6);
            return;
        }
        for (int i = 0; i < p_sz; ++ i) {
            while (val % p[i] == 0) {
                val /= p[i];
                ++ k[i];
            }
        }
        val %= MOD;
    }

    Integer operator*(const Integer &arg) const {
        Integer ans;
        for (int i = 0; i < p_sz; ++ i)
            ans.k[i] = k[i] + arg.k[i];
        ans.val = (1LL * val * arg.val) % MOD;
        return ans;
    }

    Integer operator/(const Integer &arg) const {
        Integer ans;
        for (int i = 0; i < p_sz; ++ i)
            ans.k[i] = k[i] - arg.k[i];
        ans.val = (1LL * val * inv(arg.val)) % MOD;
        return ans;
    }

    //Returneaza numarul complet - cu tot cu factorii primi tinuti separat
    int to_int() {
        int ans = val;
        for (int i = 0; i < p_sz; ++ i)
            ans = (1LL * ans * expo(p[i], k[i])) % MOD;
        return ans;
    }
private:
    static const int FACTMAX = 9;
    static int MOD;
    static int phi;
    static int p[FACTMAX];
    static int p_sz;

    //Returneaza a ^ b prin exponentiere rapida
    static int expo(int a, int b) {
        if (!b)
            return 1 % MOD;
        else if (b & 1)
            return (1LL * expo(a, b - 1) * a) % MOD;
        else {
            int aux = expo(a, b >> 1);
            return (1LL * aux * aux) % MOD;
        }
    }

    //Returneaza inversul modular al lui x
    static int inv(int x) {
        return expo(x, phi - 1);
    }

    //X = (facts, val)
    int k[FACTMAX]; // = facts
    int val;
};

int Integer :: MOD;
int Integer :: phi;
int Integer :: p[FACTMAX];
int Integer :: p_sz;

int N, M;
int sol[100005];
vector<int> edg[100005];
Integer dpdown[100005][2];

void DFS(int nod, int fth)
{
    dpdown[nod][0] = dpdown[nod][1] = 1;
    for(auto nxt: edg[nod])
    {
        if(nxt == fth)  continue;
        DFS(nxt, nod);
        dpdown[nod][0] = dpdown[nod][0] * dpdown[nxt][0];
        dpdown[nod][1] = dpdown[nod][1] * Integer(dpdown[nxt][1].to_int() + dpdown[nxt][0].to_int());
    }
}

void DFS2(int nod, int fth, Integer dpup[2])
{
    sol[nod] = ((dpdown[nod][1] * dpup[0]).to_int() + (dpdown[nod][1] * dpup[1]).to_int()) % M;
    Integer dpup0 = dpup[0] * dpdown[nod][0];
    Integer dpup1 = dpdown[nod][1] * Integer(dpup[0].to_int() + dpup[1].to_int());
    for(auto nxt: edg[nod])
    {
        if(nxt == fth)  continue;

        Integer dpupnxt[2];
        dpupnxt[0] = dpup0 / dpdown[nxt][0];
        dpupnxt[1] = dpup1 / Integer(dpdown[nxt][1].to_int() + dpdown[nxt][0].to_int());
        DFS2(nxt, nod, dpupnxt);
    }
}

int main()
{
    #ifdef FILE_IO
    freopen("1.in", "r", stdin);
    freopen("1.out", "w", stdout);
    #endif

    cin >> N >> M;
    for(int i = 1; i < N; i++)
    {
        int x, y;
        cin >> x >>  y;
        edg[x].push_back(y);
        edg[y].push_back(x);
    }

    Integer::prepare_MOD(M);

    DFS(1, 0);

    Integer dpup[2];
    dpup[0] = 1, dpup[1] = 0;
    DFS2(1, 0, dpup);

    for(int i = 1; i <= N; i++)
        printf("%d\n", sol[i]);

    return 0;
}


Measured memory_bytes: 46592