#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define ST first
#define ND second
#define ll long long
#define ld long double
using namespace std;
using namespace __gnu_pbds;

// replace int with pair if multiset needed
typedef tree<int, null_type, greater<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
ordered_set secik;

const int INF = 1e9 + 9;
const int MOD = 1e9 + 7;
const long long LINF = (ll)1e18 + 3;

// secik.insert({x, t++});
// secik.erase(secik.lower_bound({x,-1}));
// *secik.find_by_order(x)).first << "\n";
// secik.order_of_key(x)

//random_device device;
//mt19937 gener(device());
//uniform_int_distribution<int> gen(0,n-1);
//gen(gener); // generate random number

int mod;

int add(int a, int b) {
    return (a + b) % mod;
}
int mul(int a, int b) {
    return (ll) a * b % mod;
}

const int nax = 1e5 + 5;
vector<int> edges[nax], children[nax];
vector<pair<int,int>> downs[nax], pref[nax], suf[nax];
pair<int,int> down[nax], up[nax];
#define all_white first
#define sth_is_black second

pair<int,int> merge(const pair<int,int>& a, const pair<int,int>& b) {
    pair<int,int> p;
    p.all_white = mul(a.all_white, b.all_white);
    p.sth_is_black = add(
        mul(a.sth_is_black, add(b.all_white, b.sth_is_black)),
        mul(a.all_white, b.sth_is_black)
    );
    return p;
}

pair<int,int> process(pair<int,int> p) {
    return {p.all_white, add(p.sth_is_black, p.all_white)};
}

void dfs_one(int a, int parent) {
    pair<int,int> p{1, 0};
    for(int b : edges[a]) {
        if(b != parent) {
            children[a].push_back(b);
            dfs_one(b, a);
            p = merge(p, down[b]);
            downs[a].push_back(down[b]);
            //~ down[a].black = mul(down[a].black,  add(down[b].black, down[b].white) );
            //~ down[a].white = mul(down[a].white, down[b].white);
        }
    }
    down[a] = process(p);
}

void dfs_two(int a, int parent) {
    for(int i = 0; i < (int) children[a].size(); ++i) {
        pair<int,int> p{1, 0};
        if(i != 0) {
            p = merge(p, pref[a][i-1]);
        }
        if(i != (int) downs[a].size() - 1) {
            p = merge(p, suf[a][i+1]);
        }
        if(a != 1) {
            p = merge(p, up[a]);
        }
        int b = children[a][i];
        up[b] = process(p);
        dfs_two(b, a);
    }
}

int main() {
    int n;
    scanf("%d%d", &n, &mod);
    for(int i = 0; i < n - 1; ++i) {
        int a, b;
        scanf("%d%d", &a, &b);
        edges[a].push_back(b);
        edges[b].push_back(a);
    }
    dfs_one(1, -1);
    for(int a = 1; a <= n; ++a) {
        int k = downs[a].size(); // children[a].size
        assert(k == (int) children[a].size());
        pair<int,int> p{1, 0};
        for(int i = 0; i < k; ++i) {
            p = merge(p, downs[a][i]);
            pref[a].push_back(p);
        }
        p = {1, 0};
        for(int i = k - 1; i >= 0; --i) {
            p = merge(p, downs[a][i]);
            suf[a].push_back(p);
        }
        reverse(suf[a].begin(), suf[a].end());
    }
    dfs_two(1, -1);
    for(int a = 1; a <= n; ++a) {
        pair<int,int> p{1, 0};
        if(a != 1) {
            p = merge(p, up[a]);
        }
        if(!children[a].empty()) {
            p = merge(p, pref[a].back());
        }
        p = process(p);
        printf("%d\n", p.sth_is_black);
    }
}

Measured memory_bytes: 35328