#include <bits/stdc++.h>

using namespace std;

typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
//#define int ll
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector< vi > vvi;
typedef vector< vvi > vvvi;
typedef vector<short> vs;
typedef vector<vs> vvs;
typedef vector<vvs> vvvs;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<vvl> vvvl;
typedef vector<ld> vld;
typedef vector<vld> vvld;
typedef vector<vvld> vvvld;
typedef vector<string> vst;
typedef vector<vst> vvst;
typedef pair<ld, ld> pld;
typedef complex<double> base;

#define inmin(a, b) a = min(a, (b))
#define inmax(a, b) a = max(a, (b))
#define mp(a,b) make_pair(a,b)
#define modsum(a, b) ((a) + (b) >= M ? (a) + (b) - M : (a) + (b))
#define modsubtract(a, b) ((a) - (b) < 0 ? (a) - (b) + M : (a) - (b))
#define modmultiply(a, b) (((a) * (b)) % M)
#define ALL(a) a.begin(),a.end()
#define RALL(a) a.rbegin(),a.rend()
#define sqr(x) ((x) * (x))
#define fori(i, n) for(int i = 0; i < int(n); ++i)
#define cint const int &
#define SZ(a) ((int)((a).size()))
#define watch(x) cout << (#x) << " = " << (x) << endl;

const double PI = 2 * acos(0.0);

const string DIGITS = "0123456789";
const string ALPH = "abcdefghijklmnopqrstuvwxyz";


inline void transpose(vector<string> &a) {

    int n = a.size();
    if (n == 0) return;
    int m = a[0].size();
    vector<string> b(m, string(n, ' '));
    fori (i, n) fori (j, m) b[j][i] = a[i][j];
    a = b;
}


template<class T>
inline ostream & operator << (ostream &out, vector<T> &a) {
    out << "{";
    fori (i, SZ(a))
        out << a[i] << vector<string>{", ", "}"}[i + 1 == SZ(a)];
    return out;
}

void smain();

signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);


#ifdef MAX_HOME
    freopen("input.txt", "r", stdin);
    clock_t start = clock();
#define cerr cout
#endif
    cout << setprecision(12) << fixed;
    smain();
#ifdef MAX_HOME
    cout << "\n\n\n\n";
    cout << "TOTAL EXECUTION TIME: " << float( clock () - start ) /  CLOCKS_PER_SEC << endl;
#endif
}

ll M;
const int N = 1e5 + 9;

vi g[N];
int n;

ll dp_down[N], dp_up[N];

void dfs0(int v, int par) {
    dp_down[v] = 1;
    for (int to : g[v]) if (to != par) {
        dfs0(to, v);
        dp_down[v] = (dp_down[v] * (1LL + dp_down[to])) % M;
    }
}


void dfs1(int v, int par) {
    vl pref(g[v].size());
    vl suf(g[v].size());
    vl vals(g[v].size());
    fori (i, g[v].size()) {
        int to = g[v][i];
        if (to == par) {
            vals[i] = 1;
            continue;
        }
        vals[i] = (dp_down[to] + 1LL) % M;
    }
    pref[0] = vals[0];
    for (int i = 1; i < vals.size(); ++i) {
        pref[i] = (pref[i - 1] * vals[i]) % M;
    }
    suf.back() = vals.back();
    for (int i = SZ(vals) - 2; i >= 0; --i) {
        suf[i] = (suf[i + 1] * vals[i]) % M;
    }
    for (int i = 0; i < g[v].size(); ++i) {
        int to = g[v][i];
        if (to == par) continue;
        dp_up[to] = dp_up[v];
        if (i) dp_up[to] = dp_up[to] * pref[i - 1] % M;
        if (i != SZ(g[v]) - 1) dp_up[to] = dp_up[to] * suf[i + 1] % M;
        dp_up[to] = (dp_up[to] + 1) % M;
        dfs1(to, v);
    }
}

void smain() {

    cin >> n >> M;
    if (n == 1) {
        cout << "1";
        return;
    }
    fori (iter, n - 1) {
        int u, v;
        cin >> u >> v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    dfs0(1, -1);
    dp_up[1] = 1;
    dfs1(1, -1);
    fori (i, n) {
        int v = i + 1;
        cout << dp_down[v] * dp_up[v] % M << "\n";
    }
}

Measured memory_bytes: 20736