Language: C++

#include <bits/stdc++.h>

using namespace std;

#define f first
#define s second
#define pb push_back
#define mp make_pair
#define all(v) v.begin(), v.end()
#define sz(v) (int)v.size()

#define MOO(i, a, b) for (int i=a; i<b; i++)
#define M00(i, a) for (int i=0; i<a; i++)
#define MOOd(i,a,b) for (int i = (b)-1; i >= a; i--)
#define M00d(i,a) for (int i = (a)-1; i >= 0; i--)

#define FAST ios::sync_with_stdio(0); cin.tie(0);
#define finish(x) return cout << x << '\n', 0;
#define dbg(x) cerr << ">>> " << #x << " = " << x << "\n";
#define _<< " _ " <<

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int,int> pi;
typedef pair<ld,ld> pd;
typedef vector<ll> vll;
typedef pair<ll,ll> pll;

const int N = 100010;
ll M, n;
vi adj[N];
map<pi, ll> DP;
int count = 0;

ll solve(int u, int v) {
	if(DP.find(pi(u,v)) != DP.end()) {
		return DP.find(pi(u,v))->second;
	}
	ll ans = 1;
	for(auto &a : adj[v]) {
		if(a != u) {
			ans *= solve(v, a) + 1;
			ans %= M;
		}
	}
	DP[pi(u, v)] = ans;
	count++;
	return ans;
}

int main() { FAST
	mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
	cin >> n >> M;
	int u, v;
	M00(i, n-1) {
		cin >> u >> v;
		u--; v--;
		adj[u].pb(v);
		adj[v].pb(u);
	}
	M00(i, n) {
		ll ans = 1;
		for(auto &u : adj[i]) {
			ans *= 1 + solve(i, u);
			ans %= M;
		}
		cout << ans%M << endl;
	}	
	dbg(count);
}


Compiler output:
a.cc:20:9: warning: ISO C++11 requires whitespace after the macro name
   20 | #define _<< " _ " <<
      |         ^
a.cc: In function 'll solve(int, int)':
a.cc:48:9: error: reference to 'count' is ambiguous
   48 |         count++;
      |         ^~~~~
In file included from /usr/include/c++/14/algorithm:86,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:1:
/usr/include/c++/14/pstl/glue_algorithm_defs.h:101:1: note: candidates are: 'template<class _ExecutionPolicy, class _ForwardIterator, class _Tp> __pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, typename std::iterator_traits<_II>::difference_type> std::count(_ExecutionPolicy&&, _ForwardIterator, _ForwardIterator, const _Tp&)'
  101 | count(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, const _Tp& __value);
      | ^~~~~
In file included from /usr/include/c++/14/algorithm:61:
/usr/include/c++/14/bits/stl_algo.h:4025:5: note:                 'template<class _IIter, class _Tp> typename std::iterator_traits< <template-parameter-1-1> >::difference_type std::count(_IIter, _IIter, const _Tp&)'
 4025 |     count(_InputIterator __first, _InputIterator __last, const _Tp& __value)
      |     ^~~~~
a.cc:34:5: note:                 'int count'
   34 | int count = 0;
      |     ^~~~~
a.cc: In function 'int main()':
a.cc:70:13: error: reference to 'count' is ambiguous
   70 |         dbg(count);
      |             ^~~~~
a.cc:19:49: note: in definition of macro 'dbg'
   19 | #define dbg(x) cerr << ">>> " << #x << " = " << x << "\n";
      |                                                 ^
/usr/include/c++/14/pstl/glue_algorithm_defs.h:101:1: note: candidates are: 'template<class _ExecutionPolicy, class _ForwardIterator, class _Tp> __pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, typename std::iterator_traits<_II>::difference_type> std::count(_ExecutionPolicy&&, _ForwardIterator, _ForwardIterator, const _Tp&)'
  101 | count(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, const _Tp& __value);
      | ^~~~~
/usr/include/c++/14/bits/stl_algo.h:4025:5: note:                 'template<class _IIter, class _Tp> typename std::iterator_traits< <template-parameter-1-1> >::difference_type std::count(_IIter, _IIter, const _Tp&)'
 4025 |     count(_InputIterator __first, _InputIterator __last, const _Tp& __value)
      |     ^~~~~
a.cc:34:5: note:                 'int count'
   34 | int count = 0;
      |     ^~~~~