// Slower C++ solution:
#include <bits/stdc++.h>



#define pb push_back

using namespace std;

typedef long long ll;

const int N = 100007;

vector<int> e[N];



ll f[N];

ll ans[N];

int m;



void solve(int u, int p = -1) {



	f[u] = 1;

	for(int v : e[u]) {

		if(v == p) continue;

		solve(v, u);



		f[u] = f[u] * f[v] % m;

	}



	f[u] = (f[u] + 1)%m;

}



void dfs(int u, int p = -1) {



	if(p != -1) { // adiciona f do pai

		f[u] = (f[u] - 1 + m)%m;

		f[u] = f[u] * f[p] % m;

		f[u] = (f[u] + 1)%m;

	}



	ans[u] = (f[u] - 1 + m)%m;



	vector<ll> suf(e[u].size() + 1, 1);

	for(int i = e[u].size()-1; i >= 0; --i) {

		suf[i] = suf[i+1] * f[e[u][i]] % m;

	}



	ll aux = 1;

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

		int v = e[u][i];



		ll valor = f[v]; // valor antes de mudar, pra nao precisar remover o pai

		if(v != p) {

			// remove v

			f[u] = aux * suf[i + 1] % m;

			f[u] = (f[u] + 1)%m; 



			dfs(v, u);	

		}

		

		aux = aux * valor % m;

	}

}



int main() {

	ios::sync_with_stdio(0); cin.tie(0);



	int n; cin >> n >> m;



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

		int u, v; cin >> u >> v;

		e[u].pb(v);

		e[v].pb(u);

	}



	solve(1, -1);

	dfs(1, -1);



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

		cout << ans[i] << '\n';

	}

}

// Faster C++ solution for the same problem (measured speedup: 3.4824098991516936x):
#include <bits/stdc++.h>

#define f first

#define s second

#define pb push_back

#define ENDL '\n'

#define all(a) begin(a),end(a)

#define sz(a) (int)(a.size())

#define deb(a) cout << #a << ": " << a << ENDL

#define fore(i, a, b) for(int i(a), ThkMk(b); i < ThkMk; ++i)

#define _ ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);



using namespace std;



typedef long long lli;

typedef long double ld;

typedef pair<lli, lli> ii;

typedef vector<lli> vi;



vector<vector<lli>> graph;

vector<lli> sol;

vector<lli> down;



lli m;



lli ddfs(int u, int pr) {

    lli ans = 1;

    for(auto it : graph[u]) {

        if(it == pr) continue;

        ans = (ans * (1LL + ddfs(it, u))) % m;

    }

    down[u] = ans;

    return ans;

}



lli dfs(int u, int pr, lli up) {

    int sz = sz(graph[u]);

    vector<lli> v(sz);

    fore(i, 0, sz) {

        if(graph[u][i] == pr) {

            v[i] = 1;

            continue;

        }

        v[i] = down[graph[u][i]] + 1;

    }

    vector<lli> prefix(sz);

    vector<lli> suffix(sz);

    prefix[0] = v[0];

    fore(i, 1, sz) prefix[i] = (prefix[i - 1] * v[i]) % m;

    suffix[sz - 1] = v[sz - 1];

    for(int i = sz - 2; i >= 0; --i) suffix[i] = (suffix[i + 1] * v[i]) % m;

    sol[u] = ((up + 1) * suffix[0]) % m;

    fore(i, 0, sz) {

        if(graph[u][i] == pr) continue;

        lli param = (up + 1) % m;

        if(sz == 1) {

            dfs(graph[u][0], u, param);

            break;

        }

        if(i == 0) param = (param * suffix[1]) % m;

        else if(i == (sz - 1)) param = (param * prefix[sz - 2]) % m;

        else param = (((param * prefix[i - 1]) % m) * suffix[i + 1]) % m;

        dfs(graph[u][i], u, param);

    }

    return sol[u];

}



int main()

{   _

    lli n, u, v; cin >> n >> m;

    graph.resize(n);

    sol.resize(n);

    down.resize(n);

    fore(i, 1, n) {

        cin >> u >> v;

        graph[u - 1].pb(v - 1);

        graph[v - 1].pb(u - 1);

    }

    if(n == 1) {

        cout << 1 << ENDL;

        return 0;

    }

    ddfs(0, -1);

    dfs(0, -1, 0);

    fore(i, 0, n) cout << sol[i] << ENDL;

    return 0;

}