#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)n;++i)
#define rrep(i,n) for(int i=(int)n-1;i>=0;--i)
using namespace std;
using ll = long long;
template<typename T> inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
template<typename T> inline bool chmin(T& a,T b){if(b<a){a=b;return 1;}return 0;}
template<typename T> vector<T> make_vec(size_t a){return vector<T>(a);}
template<typename T,typename... Ts>
auto make_vec(size_t a,Ts... ts){return vector<decltype(make_vec<T>(ts...))>(a,make_vec<T>(ts...));}
template<typename T,typename U,typename... V>
typename enable_if<is_same<T,U>::value>::type fill_v(U& u,const V... v){u=U(v...);}
template<typename T,typename U,typename... V>
typename enable_if<!is_same<T,U>::value>::type fill_v(U& u,const V... v){for(auto& e:u)fill_v<T>(e,v...);}

template <typename T, typename Edge>
struct rerooting {
    struct node {
        int to, rev;
        Edge data;
        node(int to, Edge data) : to(to), data(data) {}
        bool operator<(const node& v) const { return to < v.to;};
    };

    using F1 = function<T(T, T)>;
    using F2 = function<T(T, Edge)>;

    vector<vector<node>> g;
    vector<vector<T>> ld, rd;
    vector<int> lp, rp;

    const F1 f1;
    const F2 f2;
    const T id;

    rerooting(int n, const F1 f1, const F2 f2, const T id) :
        g(n), ld(n), rd(n), lp(n), rp(n), f1(f1), f2(f2), id(id) {}

    void add_edge(int u, int v, Edge d, Edge e) {
        g[u].emplace_back(v, d);
        g[v].emplace_back(u, e);
    }

    void add_edge(int u, int v, Edge d) { add_edge(u, v, d, d);}

    // k : idx for edge (not vertex)
    T dfs(int v, int k) {
        while (lp[v] != k && lp[v] < (int)g[v].size()) {
            auto& e = g[v][lp[v]];
            ld[v][lp[v]+1] = f1(ld[v][lp[v]], f2(dfs(e.to, e.rev), e.data));
            lp[v]++;
        }
        while (rp[v] != k && rp[v] >= 0) {
            auto& e = g[v][rp[v]];
            rd[v][rp[v]] = f1(rd[v][rp[v]+1], f2(dfs(e.to, e.rev), e.data));
            rp[v]--;
        }
        if (k < 0) return rd[v][0];
        return f1(ld[v][k], rd[v][k+1]);
    }

    int search(vector<node>& vs, int idx) {
        return lower_bound(vs.begin(), vs.end(), node(idx, vs[0].data)) - vs.begin();
    }
    
    vector<T> build() {
        int n = g.size();
        for (int i = 0; i < n; ++i) {
            sort(g[i].begin(), g[i].end());
            ld[i].assign((int)g[i].size() + 1, id);
            rd[i].assign((int)g[i].size() + 1, id);
            lp[i] = 0;
            rp[i] = (int)g[i].size() - 1;
        }
        for (int i = 0; i < n; ++i) for (node& t : g[i]) t.rev = search(g[t.to], i);
        
        vector<T> res;
        for (int i = 0; i < n; ++i) res.emplace_back(dfs(i, -1));

        return res;
    }

    // p : idx for vertex
    T subtree(int v, int p) {
        int k = search(g[p], v);
        assert(k < (int)g[p].size() && g[p][k].to == v);
        return f2(dfs(v, g[p][k].rev), g[p][k].data);
    }
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n, m;
    cin >> n >> m;
    auto f1 = [&](ll a, ll b) { return a * b % m;};
    auto f2 = [&](ll a, int b) { return (a + b) % m;};
    rerooting<ll, int> g(n, f1, f2, 1);
    rep(i, n-1) {
        int u, v;
        cin >> u >> v;
        u--; v--;
        g.add_edge(u, v, 1);
    }
    for (ll x : g.build()) cout << x << endl;
}


Measured memory_bytes: 33120