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

#define ull unsigned long long

#define ll long long

#define endl '\n'

#define pb push_back

#define mod 1000000007

#define mp make_pair

#define pi acos(-1)

#define line cout<<"----------------------------"<<endl

#define fast ios_base::sync_with_stdio(false); cin.tie(NULL)

#define filein freopen("input.txt","r",stdin)

#define fileout freopen("output.txt","w",stdout)

using namespace std;

const ll mx=200009;	

vector<int> v[mx]; 

ll ans[mx];

int n,m; 

ll upor[mx]; 

void dfs(int s, int par)

{

	ans[s]=1;

	for(auto x: v[s])

	{

		if(x==par) continue; 

		dfs(x,s);

		ans[s]*=ans[x]+1;

		ans[s]%=m;

	}

}

void dfs1(int s, int par)

{

	vector<ll> pre((int)v[s].size()+5),suf((int)v[s].size()+5);

	pre[0]=1; 

	for(int i=1;i<=(int)v[s].size();i++)

	{

		int x=v[s][i-1] ;

		pre[i] =pre[i-1];

		if(x==par) continue; 

		pre[i]*=(ans[x]+1); 

		pre[i]%=m; 

	}

	suf[(int)v[s].size()+1]=1; 

	for(int i=(int)v[s].size();i>0;i--)

	{

		suf[i]=suf[i+1] ; 

		int x=v[s][i-1] ; 

		if(x==par) continue; 

		suf[i]*=(ans[x]+1); 

		suf[i]%=m; 

	}

	upor[1]=1; 

	for(int i=1;i<=(int)v[s].size();i++)

	{

		int x=v[s][i-1]; 

		if(x==par) continue; 

		upor[x]=1; 

		upor[x]=(upor[x]*pre[i-1])%m; 

		upor[x]=(upor[x]*suf[i+1])%m; 

		upor[x]*=upor[s];  

		upor[x]++; 

		upor[x]%=m; 

		dfs1(x,s);

	}

}

int main()

{

	

	cin>>n>>m;

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

	{

		ll a,b; 

		cin>>a>>b;

		v[a].pb(b);

		v[b].pb(a);

	}

	dfs(1,-1);

	dfs1(1,-1);

	upor[1]=1; 

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

	{

		ans[i]=(ans[i]*upor[i])%m; 

		cout<<ans[i]<<endl; 

	}

}


// Faster C++ solution for the same problem (measured speedup: 6.327685347308491x):
#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;

}