#include <iostream>
#include <iosfwd>
#include <iomanip>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <cassert>
#include <cctype>
#include <climits>
#include <vector>
#include <bitset>
#include <set>
#include <queue>
#include <stack>
#include <map>
#include <deque>
#include <string>
#include <list>
#include <iterator>
#include <sstream>
#include <complex>
#include <fstream>
#include <functional>
#include <numeric>
#include <utility>
#include <algorithm>

using namespace std;

// MACROS_BEGIN
#define CLEAR() cerr << endl;
#define LET(x, a) __typeof(a) x = a
#define FOREACH(it, v) for (LET(it, (v).begin()); it != (v).end(); ++it)
#define REPEAT(i, n) for (int i = 0; i < (n); ++i)
// MACROS_END

// GENERIC_UTILITIES_BEGIN
template <class T> inline int size(const T& c) {return (int) c.size();}
inline long long two(int x) {return (1LL << (x));}

vector<string> split(string s, string delim) {
    s += delim[0];
    string tmp;
    vector<string> result;
    for (int i = 0; i < size(s); ++i) {
        if (delim.find(s[i]) == string::npos) {
            tmp.push_back(s[i]);
        }
        else {
            if (tmp != "") result.push_back(tmp);
            tmp.clear();
        }
    }
    return result;
}
// GENERIC_UTILITIES_END

// FAST_IO_BEGIN
// FAST_IO_END

// STANDARD_IO_BEGIN
#ifndef USING_FAST_IO
int readInt() {int N = -1; scanf("%d", &N); return N;}
double readDouble() {double D; scanf("%lf", &D); return D;}
string readString() {char buffer[1 << 20]; scanf("%s", buffer); return buffer;}
long long readLongLong() {long long N = -1; scanf("%lld", &N); return N;}
#endif // NOT DEFINED USING_FAST_IO
// STANDARD_IO_END


// OUTPUT_UTILITIES_BEGIN
template <class A, class B> ostream& operator << (ostream& o, const pair<A, B>& p);
template <class T> ostream& operator << (ostream& o, const vector<T>& v);
template <class A, class B> ostream& operator << (ostream& o, const map<A, B>& m);
template <class T> ostream& operator << (ostream& o, const set<T>& s);
template <class T> ostream& operator << (ostream& o, const queue<T>& q);
template <class T> ostream& operator << (ostream& o, const stack<T>& s);

template <class A, class B> ostream& operator << (ostream& o, const pair<A, B>& p) {
    o << "(" << p.first << "," << p.second << ")"; return o;
}

template <class T> ostream& operator << (ostream& o, const vector<T>& v) {
    o << "{"; bool first = true; FOREACH(it, v) { if (!first) o << ","; first = false; o << *it; } return o << "}";
}

template <class A, class B> ostream& operator << (ostream& o, const map<A, B>& m) {
    o << "{"; bool first = true; FOREACH(it, m) { if (!first) o << ","; first = false; o << *it; } return o << "}";
}

template <class T> ostream& operator << (ostream& o, const set<T>& s) {
    o << "{"; bool first = true; FOREACH(it, s) { if (!first) o << ","; first = false; o << *it; } return o << "}";
}

template <class T> ostream& operator << (ostream& o, const queue<T>& q) {
    o << "{"; bool first = true; queue<T> p = q; while (!p.empty()) { if (!first) o << ","; first = false; o << p.front(); p.pop(); } return o << "}";
}

template <class T> ostream& operator << (ostream& o, const stack<T>& s) {
    o << "{"; bool first = true; stack<T> r = s; while (!r.empty()) { if (!first) o << ","; first = false; o << r.top(); r.pop(); } return o << "}";
}
// OUTPUT_UTILITIES_END

// DEBUGGING_UTILITIES_BEGIN
// // DEBUGGING SWITCH
// #define PHOENIX_DEBUG

#ifdef PHOENIX_DEBUG
#define BUG(x) cerr << __LINE__ << ": " << #x << " = " << x << endl;
#else
#define BUG(...)
#endif
// DEBUGGING_UTILITIES_END

const double epsilon = 1e-8;
const int infinite    = 2000000000; // 2 * 10^9
const long long infiniteLL = 2000000000000000000LL; // 2 * 10^18
// long long modulo = 1000000007;

#define black first
#define white second

struct Task {
    int N;
    long long modulo;
    vector< vector<int> > adj;
    vector< vector<int> > children;
    vector< vector< pair<int, int>> > prefix;
    vector< vector< pair<int, int>> > suffix;
    vector< pair<int, int> > down;
    vector< pair<int, int> > up;

    void readInput() {
        N = readInt();
        modulo = readInt();
        adj.resize(N);

        for (int i = 0; i < N - 1; ++i) {
            int u = readInt() - 1, v = readInt() - 1;
            adj[u].push_back(v);
            adj[v].push_back(u);
        }

        down.resize(N);
        up.resize(N);
        children.resize(N);
        prefix.resize(N);
        suffix.resize(N);
    }

    int mul(int a, int b) {
        return (1LL * a * b) % modulo;
    }

    int add(int a, int b) {
        return (a + b) % modulo;
    }

    void merge(pair<int, int>& u, const pair<int, int>& v) {
        u.black = mul(u.black, add(v.white, v.black));
        u.white = mul(u.white, v.white);
    }

    void dfsDown(int u, int p) {
        down[u] = {1, 1};

        for (int v : adj[u]) {
            if (v != p) {
                dfsDown(v, u);
                children[u].push_back(v);
                merge(down[u], down[v]);
                // down[u].black = mul(down[u].black, add(down[v].white, down[v].black));
                // down[u].white = mul(down[u].white, down[v].white);
            }
        }
    }

    void dfsUp(int u, int parent) {
        if (parent == -1) {
            up[u] = {1, 1};
        }

        for (int i = 0; i < size(children[u]); ++i) {
            pair<int, int> p = up[u];
            int v = children[u][i];
            BUG(u); BUG(v); BUG(i);
            BUG(up[u]);
            BUG(p);

            if (i - 1 >= 0) {
            BUG(prefix[u][i - 1]);
                p.black = mul(p.black, prefix[u][i - 1].black);
                BUG(p);
            }

            if (i + 1 < size(suffix[u])) {
            BUG(suffix[u][i + 1]);
                p.black = mul(p.black, suffix[u][i + 1].black);
                BUG(p);
            }

            up[v] = {p.black + p.white, p.white};
            BUG(up[v]);
            dfsUp(v, u);
        }
    }

    void solve() {
        dfsDown(0, -1);

        // calculate prefix and suffix of downs
        for (int u = 0; u < N; ++u) {
            pair<int, int> p = {1, 1};
            prefix[u].resize(size(children[u]));
            suffix[u].resize(size(children[u]));

            for (int k = 0; k < size(children[u]); ++k) {
                int v = children[u][k];
                merge(p, down[v]);
                prefix[u][k] = p;
            }

            p = {1, 1};

            for (int k = size(children[u]) - 1; k >= 0; --k) {
                int v = children[u][k];
                merge(p, down[v]);
                suffix[u][k] = p;
            }
        }

        dfsUp(0, -1);

        for (int i = 0; i < N; ++i) {
            BUG(i);
            BUG(up[i].black); BUG(down[i].black);
            int result = mul(up[i].black, down[i].black);

            cout << result << endl;
        }

    }

    void perform() {
        readInput();
        solve();
    }
};

int main()
{
    Task task;
    task.perform();

    return 0;
}

// Powered by PhoenixAI


Measured memory_bytes: 27136