/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author tatsumack
 */

#include <iostream>
#include <fstream>

#include <iostream>
#include <limits.h>
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>

#include <list>
#include <map>
#include <numeric>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#include <queue>
#include <unordered_map>
#include <unordered_set>

#define int long long
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define REPS(i, n) for (int i = 1, i##_len = (n); i <= i##_len; ++i)
#define FOR(i, a, b) for (int i = (a), i##_len = (b); i <= i##_len; ++i)
#define REV(i, a, b) for (int i = (a); i >= (b); --i)
#define CLR(a, b) memset((a), (b), sizeof(a))
#define DUMP(x) cout << #x << " = " << (x) << endl;
#define INF 1001001001001001001ll
#define fcout cout << fixed << setprecision(12)

using namespace std;

template<typename T>
vector<T> make_v(size_t a) { return vector<T>(a); }

template<typename T, typename... Ts>
auto make_v(size_t a, Ts... ts) {
    return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}

template<typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type
fill_v(T& t, const V& v) { t = v; }

template<typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type
fill_v(T& t, const V& v) {
    for (auto& e:t) fill_v(e, v);
}

typedef pair<int, int> P;

class VSubtree {
public:

    int N, M;
    vector<int> G[100005];
    int dp[100005];
    int ans[100005];

    void dfs1(int cur, int par = -1) {
        dp[cur] = 1;
        for (int c: G[cur]) {
            if (c == par) continue;
            dfs1(c, cur);
            dp[cur] *= dp[c] + 1;
            dp[cur] %= M;
        }
    }

    void dfs2(int cur, int par = -1) {
        ans[cur] = 1;
        for (int c: G[cur]) {
            ans[cur] *= dp[c] + 1;
            ans[cur] %= M;
        }

        int n = G[cur].size();
        vector<int> left(n), right(n);
        REP(i, n) {
            left[i] = right[i] = dp[G[cur][i]] + 1;
        }
        FOR(i, 1, n - 1) {
            left[i] *= left[i - 1];
            left[i] %= M;
        }
        REV(i, n - 2, 0) {
            right[i] *= right[i + 1];
            right[i] %= M;
        }

        REP(i, n) {
            int c = G[cur][i];
            if (c == par) continue;
            dp[cur] = 1;
            if (i > 0) dp[cur] *= left[i - 1];
            if (i < n - 1) dp[cur] *= right[i + 1];
            dp[cur] %= M;
            dfs2(c, cur);
        }
    }

    void solve(std::istream& cin, std::ostream& cout) {
        cin >> N >> M;
        REP(i, N - 1) {
            int x, y;
            cin >> x >> y;
            x--;
            y--;
            G[x].push_back(y);
            G[y].push_back(x);
        }

        dfs1(0);
        dfs2(0);

        REP(i, N) cout << ans[i] << endl;
    }
};


signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    std::istream& in(std::cin);
    std::ostream& out(std::cout);

    VSubtree solver;
    solver.solve(in, out);
    return 0;
}


Measured memory_bytes: 18560