Language: C++

#include <bits/stdc++.h>

using namespace std;



#define F first
#define S second
 
#define UP_LIMIT 100010
 
#define PB push_back
#define SZ(x) (int)x.size()
#define BE(x) x.begin(),x.end()
#define MOD 1000000007
#define PI acos(-1)
 
#define debug(a...) cout << #a << "="; dbg::Out(a);

using namespace std;

namespace dbg
{
	template<typename PType1,typename PType2>
	ostream & operator<<(ostream & out, const pair<PType1,PType2> & p){
		out << "(" << p.first << "," << p.second << ")";
	}
	template<typename VType>
	ostream & operator<<(ostream & out, const vector<VType> & v){
		out << "[";
		for(auto it = v.begin(); it != v.end(); it++){
			if(it != v.begin())
				out << " ";
			out << *it ;	
		}
		out << "]";	
		return out;
	}
	template<typename SType>
	ostream & operator<<(ostream & out, const set<SType> & s){
		out << "[";
		for(auto it = s.begin(); it != s.end(); it++){
			if(it != s.begin())
				out << ", ";
			out << *it;	
		}
		out << "]";
	}
	template<typename MType1, typename MType2>
	ostream & operator<<(ostream & out, const map<MType1,MType2> & m){
		out << "[";
		for(auto it = m.begin(); it != m.end(); it++){
			if(it != m.begin())
				out << ",";
			out << "{" << it->first << ":" << it->second << "}";
		}
		out << "]";
	}
	void Out(){cout << "\n";}
	template<typename T, typename... U>
	void Out(T start,U... rest){
		cout << start << ", ";
		Out(rest...);
	}	
}








int n,m;
vector<vector<int> > g; 
vector<int> dn,up;
void dfs1(int cur, int par){
	dn[cur] = 1;
	for(int son : g[cur]){
		if(son==par) continue;
		dfs1(son,cur);
		dn[cur] = (1ll*(dn[cur] * (1+dn[son])))%m;
	}
}

void dfs2(int cur, int par){
	int k = g[cur].size();
	vector<int> pref(k+2,1),suff(n+2,1);
	for(int i = 0; i < k; i++){
		pref[i] = suff[i] = 1;
	}
	for(int i = 0; i < k; i++){
		if(par == g[cur][i]){
			if(i!=0) pref[i] = pref[i-1]%m;
			continue;
		}
		if(i==0) pref[i] = (1+dn[g[cur][i]])%m;
		else     pref[i] = 1ll*(pref[i-1] * (1+dn[g[cur][i]]))%m ;
	}
	for(int i = k-1; i >= 0; i--){
		if(par == g[cur][i]){
			if(i!=k-1) suff[i] = suff[i+1]%m;
			continue;
		}
		if(i==k-1) suff[i] = (1+dn[g[cur][i]])%m;
		else       suff[i] = 1ll*(suff[i+1] * (1+dn[g[cur][i]]) )%m;
	}
	//debug(cur,par,pref);
	//debug(cur,par,suff);
	for(int i = 0; i < k; i++){
		if(par == g[cur][i]) continue;
		int res = 1;
		if(i!=0) res = pref[i-1]%m;
		if(i!=k-1) res = 1ll * res * suff[i+1] % m;
		up[g[cur][i]] = 1ll * (1 + 1ll * up[cur] * res) % m;
		dfs2(g[cur][i],cur);
	}
}


int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(null);
	cout.tie(null);
	cin >> n >> m;
	g.resize(n+2);
	dn.resize(n+2);
	up.resize(n+2);
	for(int i = 1; i < n; i++){
		int x,y; cin >> x >> y;
		g[x].push_back(y);
		g[y].push_back(x);
	}
	dfs1(1,0);
	up[1] = 1;
	dfs2(1,0);
	for(int i = 1; i <= n; i++){
		cout << (1ll*dn[i]*up[i]) << "\n";
	}
	return 0;
}


Compiler output:
a.cc: In function 'std::ostream& dbg::operator<<(std::ostream&, const std::pair<_T1, _T2>&)':
a.cc:27:9: warning: no return statement in function returning non-void [-Wreturn-type]
   27 |         }
      |         ^
a.cc: In function 'int main()':
a.cc:123:17: error: 'null' was not declared in this scope
  123 |         cin.tie(null);
      |                 ^~~~