#include <bits/stdc++.h>
using namespace std;

inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}

typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> P;
typedef tuple<int,int,int> tpl;

#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define SORT(c) sort((c).begin(),(c).end())
#define REVERSE(c) reverse((c).begin(),(c).end())
#define LB(a,x) lower_bound((a).begin(), (a).end(), x) - (a).begin()
#define UB(a,x) upper_bound((a).begin(), (a).end(), x) - (a).begin()

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define RFOR(i,a,b) for(int i=(a)-1;i>=(b);--i)
#define RREP(i,n) RFOR(i,n,0)

#define en "\n"

const double EPS = 1e-9;
const double PI  = acos(-1.0);
const int INF = 2147483647;
const long long LINF = 1LL<<60;
long long MOD = 1000000007; // 998244353

#define CLR(a) memset((a), 0, sizeof(a))

#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }

struct mint {
    long long x;
    mint(long long x=0):x((x%MOD+MOD)%MOD){}
    mint operator-() const { return mint(-x);}
    mint& operator+=(const mint a) {
        if ((x += a.x) >= MOD) x -= MOD;
        return *this;
    }
    mint& operator-=(const mint a) {
        if ((x += MOD-a.x) >= MOD) x -= MOD;
    	return *this;
    }
    mint& operator*=(const mint a) {
    	(x *= a.x) %= MOD;
    	return *this;
    }
    mint operator+(const mint a) const {
        mint res(*this);
        return res+=a;
    }
    mint operator-(const mint a) const {
        mint res(*this);
        return res-=a;
    }
    mint operator*(const mint a) const {
        mint res(*this);
        return res*=a;
    }
    mint pow(long long t) const {
        if (!t) return 1;
        mint a = pow(t>>1);
        a *= a;
    	if (t&1) a *= *this;
        return a;
    }
    // for prime MOD
    mint inv() const {
        return pow(MOD-2);
    }
    mint& operator/=(const mint a) {
    	return (*this) *= a.inv();
    }
    mint operator/(const mint a) const {
        mint res(*this);
        return res/=a;
    }
};

template<typename T>
struct Node{
    int id;
    T v;
    bool done = false;
    Node(int id, T v=1e9): id(id), v(v) {}
    Node(): id(0), v(1e9) {}
};

template<typename T>
struct Edge{
    int from, to, id;
    T cost;
    Edge(int from, int to, T cost=1, int id=-1): from(from), to(to), cost(cost), id(id) {}
};

template<typename T>
struct Graph{
    int N;
    vector<vector<Edge<T>>> adj;
    vector<Node<T>> node;

    Graph(int N): N(N) {
        adj.assign(N, vector<Edge<T>>());
        node.assign(N, Node<T>());
        for(int i=0; i<N; ++i) node[i].id = i;
    }

    Graph() {}

    void add_edge(int i, int j, T cost=1, int id=-1){
        Edge<T> e(i, j, cost, id);
        adj[i].emplace_back(e);
    }

    vector<Edge<T>> get_adj(Node<T> node){
        return adj[node.id];
    }

    void assign(int n){
        N = n;
        adj.assign(N, vector<Edge<T>>());
        node.assign(N, Node<T>());
        for(int i=0; i<N; ++i) node[i].id = i;
    }

    void reset(){
        for(int i=0; i<N; ++i){
            node[i].done = false;
            node[i].v = 1e9;
        }
    }
};

template<typename T>
struct ReRooting{
    typedef function<T(T,T)> F1;
    typedef function<T(T,int)> F2;

    int N;
    F1 merge;
    F2 addNode;
    T e;
    Graph<T> G;

    vector<vector<int>> i4adj;
    vector<vector<T>> dp; // dp[i][j] := iを親, jを根とする部分木の値
    vector<T> ret;

    ReRooting(int n, F1 f, F2 g, T identity): N(n), merge(f), addNode(g), e(identity){
        G.assign(n);
        i4adj.resize(n);
    }

    void add_edge(int i, int j, T cost=1, int id=-1){
        i4adj[i].push_back((int)G.adj[j].size());
        i4adj[j].push_back((int)G.adj[i].size());
        G.add_edge(i, j, cost, id); // G.adj[i][G.adj[i].size()] = j: iから見てk番目のノードはj
        G.add_edge(j, i, cost, id); // G.adj[j][G.adj[j].size()] = i: jから見てl番目のノードはi
    }

    void initialize(){
        dp.resize(N);
        for(int i=0; i<N; i++)
            dp[i].resize(G.adj[i].size());
        ret.resize(N);

        if(N == 1){
            ret[0] = addNode(e, 0);
            return;
        }

        vector<int> par(N), order(N); // par[i] := 0を根としたときの親 order[i] := dfsでのi番目の訪問ノード
        par[0] = -1;
        int index = 0;
        stack<int> stk; stk.push(0);
        while(!stk.empty()){
            int node = stk.top(); stk.pop();
            order[index++] = node;

            for(Edge<T>& e : G.adj[node]){
                if(e.to == par[node]) continue;
                stk.push(e.to);
                par[e.to] = node;
            }
        }

        // 0を根とする木について解を求める
        for(int i=N-1; i>=1; i--){
            int node = order[i];
            int parent = par[node];

            T result = e;
            int parIndex = -1;
            for(int j=0; j<G.adj[node].size(); j++){
                Edge<T>& e = G.adj[node][j];
                if(e.to == parent){
                    parIndex = j;
                    continue;
                }
                result = merge(result, dp[node][j]);
            }
            dp[parent][i4adj[node][parIndex]] = addNode(result, node);
        }

        // 全ての頂点について解を求める
        for(int i=0; i<N; i++){
            int node = order[i];
            T accum = e;
            vector<T> accumsFromTail(G.adj[node].size());
            accumsFromTail[accumsFromTail.size() - 1] = e;

            for (int j=accumsFromTail.size()-1; j >= 1; j--)
                    accumsFromTail[j - 1] = merge(dp[node][j], accumsFromTail[j]);

            for (int j = 0; j < accumsFromTail.size(); j++) {
                dp[G.adj[node][j].to][i4adj[node][j]] = addNode(merge(accum, accumsFromTail[j]), node);
                accum = merge(accum, dp[node][j]);
            }
            ret[node] = addNode(accum, node);
        }
    }

    T query(int node){
        return ret[node];
    }

};

mint f(mint a, mint b){
    return a*b;
}

mint g(mint a, int b){
    return a + 1;
}

int main(void){
    int N,M; cin >> N >> M;
    MOD = M;
    ReRooting<mint> rrt(N, f, g, mint(1));
    REP(i,N-1){
        int x,y; cin >> x >> y; x--; y--;
        rrt.add_edge(x,y);
    }

    rrt.initialize();

    REP(i,N) cout << (rrt.query(i)-1).x << en;
    return 0;
}


Measured memory_bytes: 26032