#include <bits/stdc++.h>
#define elif else if
#define ALL(a) a.begin(), a.end()
using namespace std;
using LL = long long;
using LD = long double;
using PII = pair<int, int>;
using PLI = pair<long long, int>;
using PIL = pair<int, long long>;
using PLL = pair<long long, long long>;
using TI = tuple<int, int, int>;
mt19937 mrand(random_device{}());
int rnd(int x) { return mrand() % x; }
template <class T> int bcnt(const T &a) { return __builtin_popcount(a); }
template <> int bcnt(const int64_t &a) { return __builtin_popcountll(a); }
template <class T> int blen(const T &a) { return a ? 1 + blen(a / 2) : 0; }
template <class A, class B> B conv(const A &a, B b = B()) {
    stringstream s;
    s << a, s >> b;
    return b;
}
template <class T> T sign(const T &a) { return a == 0 ? 0 : (a < 0 ? -1 : 1); }
template <class T> T floor(const T &a, const T &b) {
    assert(b != 0);
    return sign(a) * sign(b) > 0 ? abs(a) / abs(b) : -(abs(a) + abs(b) - 1) / abs(b);
}
template <class T> T ceil(const T &a, const T &b) {
    assert(b != 0);
    return sign(a) * sign(b) > 0 ? (abs(a) + abs(b) - 1) / abs(b) : -abs(a) / abs(b);
}
template <class T> bool chmin(T &a, T b) { return b < a ? a = b, true : false; }
template <class T> bool chmax(T &a, T b) { return b > a ? a = b, true : false; }
template <class T> auto min(const T &a) { return *min_element(ALL(a)); } 
template <class T> auto max(const T &a) { return *max_element(ALL(a)); }
template <class T> auto sum(const T &a) { return accumulate(ALL(a), (typename T::value_type)0); }
template <> auto sum(const vector<int> &a) { return accumulate(ALL(a), 0LL); }
template <> auto sum(const vector<string> &a) { return accumulate(ALL(a), string()); }
template <class T> void uniq(T &a) { sort(ALL(a)); a.erase(unique(ALL(a)), a.end()); }

template<class A, class B> ostream& operator <<(ostream& out, const pair<A, B> &p) {
    return out << "(" << p.first << ", " << p.second << ")";
}
template <typename T, size_t N>
ostream& operator <<(ostream& out, const array<T, N>& a) {
    out << "["; bool first = true;
    for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "]";
    return out;
}
template <typename T>
ostream& operator <<(ostream& out, const vector<T>& a) {
    out << "["; bool first = true;
    for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "]";
    return out;
}
template <typename T>
ostream& operator <<(ostream& out, const deque<T>& a) {
    out << "["; bool first = true;
    for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "]";
    return out;
}
template <typename T, class Cmp>
ostream& operator <<(ostream& out, const set<T, Cmp>& a) {
    out << "{"; bool first = true;
    for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "}";
    return out;
}
template <typename T>
ostream& operator <<(ostream& out, const unordered_set<T>& a) {
    out << "{"; bool first = true;
    for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "}";
    return out;
}
template <typename U, typename T, class Cmp>
ostream& operator <<(ostream& out, const map<U, T, Cmp>& a) {
    out << "{"; bool first = true;
    for (auto& p : a) { out << (first ? "" : ", "); out << p.first << ":" << p.second; first = 0;} out << "}";
    return out;
}
template<class T> inline void YES(T condition) { 
    if(condition) cout << "YES" << '\n';
    else cout << "NO" << '\n';
}
template<class T> inline void Yes(T condition) {
    if(condition) cout << "Yes" << '\n';
    else cout << "No" << '\n';
}
template<class T> T gcd(T a, T b) {
    while (b) {
        T tmp = b;
        b = a % b;
        a = tmp;
    }
    return a;
}
template<class T> T lcm(T a, T b){
    return a / gcd(a, b) * b;
}

#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
    cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
    const char* comma = strchr(names + 1, ',');
    cerr.write(names, comma - names) << ": " << arg1 << " |";
    __f(comma + 1, args...);
}

int qpow(int a, int b, int MOD) {
    return qpow(static_cast<long long>(a), static_cast<long long>(b), MOD);
}
long long qpow(long long a, long long b, int MOD) {
    assert(b >= 0);
    if (b == 0) return 1;
    long long res = 1;
    while (b) {
        if (b & 1) res = (res * a) % MOD;
        a = (a * a) % MOD;
        b >>= 1;
    }
    return res;
}
LL inv(LL a, int MOD) {
    return qpow(a, (MOD - 2) * 1LL, MOD);
}
// template <class T> auto vect(const T &v, int n) { return vector<T>(n, v); }
// template <class T, class... D> auto vect(const T& v, int n, D... m) {
//     return vector<decltype(vect(v, m...))>(n, vect(v, m...));
// }

// template <typename T>
// vector<vector<T> > matmul(vector<vector<T> >& a, vector<vector<T> >& b) {
//     int n = a.size(), m = b.size();
//     assert(n > 0 && m > 0);
//     assert((int)a[0].size() == m);
//     int p = b[0].size();
//     vector<vector<T> > res(n, vector<T>(p, (typename T::value_type)0));
//     for (int i = 0; i < n; i++) {
//         for (int k = 0; k < p; k++) {
//             T now = (typename T::value_type)0;
//             for (int j = 0; j < m; j++) {
//                 now += a[i][j] * b[j][k];
//             }
//             res[i][k] = now;
//         }
//     }
// }
// template <>
// vector<vector<LL> > matmul(vector<vector<LL> >& a, vector<vector<LL> >& b, int MOD) {
//     int n = a.size(), m = b.size();
//     assert(n > 0 && m > 0);
//     assert((int)a[0].size() == m);
//     int p = b[0].size();
//     vector<vector<LL> > res(n, vector<LL>(p, 0LL));
//     for (int i = 0; i < n; i++) {
//         for (int k = 0; k < p; k++) {
//             LL now = (typename T::value_type)0;
//             for (int j = 0; j < m; j++) {
//                 now += a[i][j] * b[j][k];
//             }
//             res[i][k] = now % MOD;
//         }
//     }
// }
// vector<vector<LL> > qpow(vector<vector<LL> >& a, long long b, int MOD) {
//     assert(b >= 0);
//     int n = a.size();
//     assert(n >= 1);
//     int m = a[0].size();
//     assert(n == m);
//     if (b == 0) {
//         vector<vector<LL> > res(n, vector<LL>(n, 0LL));
//         for (int i = 0; i < n; i++) res[i][i] = 1LL;
//         return res;
//     }
//     while (b) {
//         if (b & 1) 
//     }

// }


// long long qpow(long long a, long long b, int MOD) {
//     if (b == 0) return 1;
//     long long res = 1;
//     while (b) {
//         if (b & 1) res = (res * a) % MOD;
//         a = (a * a) % MOD;
//         b >>= 1;
//     }
//     return res;
// }

const int MOD = 1e9 + 7;
const int iinf = __INT_MAX__ / 2;
const LL linf = __INT64_MAX__ / 2;
const int maxn = 2e6;
const LD eps = 1e-8;
const LD pi = acos(-1);
// const int INF = 2e9;
int fac[maxn + 5];
int invfac[maxn + 5];
void init() {
    fac[0] = 1;
    for (int i = 1; i <= maxn; i++) fac[i] = 1LL * fac[i - 1] * i % MOD;
    invfac[maxn] = (int)(qpow(fac[maxn], MOD - 2, MOD));
    for (int i = maxn - 1; i >= 0; i--) invfac[i] = 1LL * invfac[i + 1] * (i + 1) % MOD;
}
int C(int n, int k) {
    assert(0 <= k && n >= k);
    int tmp = (int)(1LL * invfac[k] * invfac[n - k] % MOD);
    return (int)(1LL * fac[n] * tmp % MOD);
}
    // vector<int> fa(n);
    // vector<int> sz(n, 1);
    // iota(fa.begin(), fa.end(), 0);
    // function<int(int)> Find = [&](int x) {
    //     return x == fa[x] ? x : fa[x] = Find(fa[x]);
    // };
    // function<bool(int, int)> Union = [&](int x, int y) {
    //     int px = Find(x), py = Find(y);
    //     if (px == py) return false;
    //     if (sz[px] > sz[py]) swap(px, py);
    //     fa[px] = py;
    //     sz[py] += sz[px];
    //     return true;
    // };

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);

    int n, m;
    cin >> n >> m;
    vector<vector<int> > G(n + 1);
    vector<int> fa(n + 1);
    for (int _ = 0; _ < n - 1; _++) {
        int x, y;
        cin >> x >> y;
        G[x].push_back(y);
        G[y].push_back(x);
    }
    // trace(G);
    // down
    vector<int> down(n + 1, -1);
    // use in dfs2: down except itself
    vector<int> downecpt(n + 1, 1);
    vector<int> q;
    function<int(int)> dfs = [&] (int u) {
        int& res = down[u];
        if (res >= 0) return res;
        LL ans = 1LL;
        vector<PII> vtmp;
        // prefix product, suffix product
        vector<LL> prep, sufp;
        for (auto& v: G[u]) if (v != fa[u]) {
            fa[v] = u;
            vtmp.emplace_back(v, (dfs(v) + 1) % m);
        }
        if (vtmp.empty()) { q.push_back(u); return res = 1; }
        int sz = vtmp.size();
        prep.resize(sz + 1); sufp.resize(sz + 1);
        prep[0] = 1LL; sufp[sz] = 1LL;
        for (int i = 0; i < sz; i++) {
            int tmp = vtmp[i].second;
            prep[i + 1] = prep[i] * tmp % m;
            ans = (ans * tmp) % m;
        }
        for (int i = sz - 1; i >= 0; i--) {
            int v = vtmp[i].first, tmp = vtmp[i].second;
            downecpt[v] = (prep[i] * sufp[i + 1]) % m;
            sufp[i] = sufp[i + 1] * tmp % m;
        }
        return res = ans % m;
    };
    dfs(1);

    vector<int> up(n + 1, -1);
    function<int(int)> dfs2 = [&] (int u) {
        if (u == 1) return up[u] = 0;
        int& res = up[u];
        if (res >= 0) return res;
        LL ans = (dfs2(fa[u]) + 1) % m;
        ans *= downecpt[u];
        ans %= m;
        return res = ans;
    };
    for (auto &v: q) dfs2(v);

    for (int i = 1; i <= n; i++) {
        cout << ((1LL * down[i] * (up[i] + 1)) % m) << '\n';
    }

    

    return 0;
}


Measured memory_bytes: 22564