// Original C++ solution (slower):
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

//#pragma GCC optimize("unroll-loops")



#define _CRT_SECURE_NO_WARNINGS

#include <bits/stdc++.h>



using namespace std;



typedef pair <int, int> pii;

typedef pair <long long, long long> pll;

typedef long long ll;

typedef unsigned long long ull;

typedef long double ld;

typedef vector <vector <ll>> matrix;



#define endl "\n"

#define mt make_tuple

#define mp make_pair



template <typename T1, typename T2>

bool umax(T1 &a, const T2&b) { return a < b ? a = b, 1 : 0;}

template <typename T1, typename T2> 

bool umin(T1 &a, const T2 &b) {return a > b ? a = b, 1 : 0;}

template <typename T>

T sqr(T a) {return a * a;}



const int mod = 1e9 + 7;

template <typename T1, typename T2>

void modadd(T1 &a, const T2 &b) {

    a += b;

    

    if (a >= mod)

        a -= mod;

}



mt19937 rng(20010709);

const int INF = 1000000001;

const int N = 100005;



int n, m, used[100005];

int down[N], up[N];

vector <int> e[N];



void dfsDown(int v);

void dfsUp(int v);



int main() {

    ios::sync_with_stdio(false);

    cin.tie(nullptr);



	cin >> n >> m;



	for (int i = 1; i < n; i++) {

		int x, y;

		cin >> x >> y;

		e[x].push_back(y);

		e[y].push_back(x);

	}



    dfsDown(1);

    fill(used, used + N, 0);

    up[1] = 1;

    dfsUp(1);



    for (int i = 1; i <= n; i++)

        cout << down[i] * 1LL * up[i] % m << endl;



    cout << endl << endl;



    return 0;

}



void dfsDown(int v) {

	used[v] = 1;

    down[v] = 1;



	for (auto it : e[v]) {

		if (!used[it]) {

			dfsDown(it);

            down[v] = down[v] * 1LL *  (down[it] + 1) % m;

		}

	}

}



void dfsUp(int v) {

    vector <int> pref(e[v].size() + 2), suff(e[v].size() + 2);

    pref[0] = up[v];

    suff[suff.size() - 1] = 1;

    used[v] = 1;



    for (int i = 0; i < e[v].size(); i++)

        pref[i + 1] = pref[i] * 1LL * (used[e[v][i]] ? 1 : (down[e[v][i]] + 1)) % m;



    for (int i = e[v].size() - 1; i > -1; i--)

        suff[i + 1] = suff[i + 2] * 1LL * (used[e[v][i]] ? 1 : (down[e[v][i]] + 1)) % m;



    for (int i = 0; i < e[v].size(); i++) {

        if (!used[e[v][i]]) {

            up[e[v][i]] = (1 + pref[i] * 1LL * suff[i + 2]) % m;

            dfsUp(e[v][i]);

        }

    }

}

// Optimized solution to the same problem (faster):
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

//#pragma GCC optimize("unroll-loops")



#define _CRT_SECURE_NO_WARNINGS

#include <bits/stdc++.h>



using namespace std;



typedef pair <int, int> pii;

typedef pair <long long, long long> pll;

typedef long long ll;

typedef unsigned long long ull;

typedef long double ld;

typedef vector <vector <ll>> matrix;



#define endl "\n"

#define mt make_tuple

#define mp make_pair



template <typename T1, typename T2>

bool umax(T1 &a, const T2&b) { return a < b ? a = b, 1 : 0;}

template <typename T1, typename T2> 

bool umin(T1 &a, const T2 &b) {return a > b ? a = b, 1 : 0;}

template <typename T>

T sqr(T a) {return a * a;}



const int mod = 1e9 + 7;

template <typename T1, typename T2>

void modadd(T1 &a, const T2 &b) {

    a += b;

    

    if (a >= mod)

        a -= mod;

}



mt19937 rng(20010709);

const int INF = 1000000001;

const int N = 100005;



int n, m;

int down[N], up[N];

vector <int> e[N];



void dfsDown(int v);

void dfsUp(int v);



int main() {

    ios::sync_with_stdio(false);

    cin.tie(nullptr);



	cin >> n >> m;



	for (int i = 1; i < n; i++) {

		int x, y;

		cin >> x >> y;

		e[x].push_back(y);

		e[y].push_back(x);

	}



    dfsDown(1);

    up[1] = 1;

    dfsUp(1);



    for (int i = 1; i <= n; i++)

        cout << down[i] * 1LL * up[i] % m << endl;



    cout << endl << endl;



    return 0;

}



void dfsDown(int v) {

    down[v] = 1;



	for (auto it : e[v]) {

        e[it].erase(find(e[it].begin(), e[it].end(), v));

		dfsDown(it);

        down[v] = down[v] * 1LL * (down[it] + 1) % m;

	}

}



void dfsUp(int v) {

    vector <int> pref(e[v].size() + 2), suff(e[v].size() + 1);

    pref[0] = up[v];

    suff[suff.size() - 1] = 1;



    for (int i = 0; i < e[v].size(); i++)

        pref[i + 1] = pref[i] * 1LL * (down[e[v][i]] + 1) % m;



    for (int i = e[v].size() - 1; i > -1; i--)

        suff[i] = suff[i + 1] * 1LL * (down[e[v][i]] + 1) % m;



    for (int i = 0; i < e[v].size(); i++) {

        up[e[v][i]] = (1 + pref[i] * 1LL * suff[i + 1]) % m;

        dfsUp(e[v][i]);

    }

}

// Fastest known solution for this problem: