Language: C++

#include <bits/stdc++.h>
using namespace std;
#define int long long
int n,M,to[200005],next[200005],head[200005],cnt=1;
int f[200005],S[200005],L[200005],R[200005],p[200005],dp[200005];
inline void add(int u,int v) {
	to[cnt]=v;next[cnt]=head[u];head[u]=cnt++;
	to[cnt]=u;next[cnt]=head[v];head[v]=cnt++;
}
void dfs(int cur,int fa){
	int cnt=0;
	f[cur]++;
	for(int i=head[cur];i;i=next[i])
		if(to[i]!=fa){
			dfs(to[i],cur);
			f[cur]=f[cur]*f[to[i]]%M;
		}
	for(int i=head[cur];i;i=next[i])
		if(to[i]!=fa)
			p[++cnt]=to[i];
	f[cur]++;f[cur]%=M;
	L[0]=R[cnt+1]=1;
	for(int i=1;i<=cnt;i++)	L[i]=L[i-1]*f[p[i]]%M;
	for(int i=cnt;i>=1;i--)	R[i]=R[i+1]*f[p[i]]%M;
	for(int i=1;i<=cnt;i++)	S[p[i]]=L[i-1]*R[i+1]%M;
}
void dfs2(int cur,int fa){
	if(cur==1) dp[cur]=1;
	else	dp[cur]=(S[cur]*dp[fa]+1)%M;
	for(int i=head[cur];i;i=next[i])
		if(to[i]!=fa) dfs2(to[i],cur);
}
signed main(){
	cin>>n>>M;
	for(int i=1;i<n;i++){
		int u,v;
		cin>>u>>v;
		add(u,v);
	}
	dfs(1,1);
	dfs2(1,1);
	for(int i=1;i<=n;i++)	cout<<dp[i]*(f[i]+M-1)%M<<endl;
    return 0;
}

Compiler output:
a.cc: In function 'void add(long long int, long long int)':
a.cc:7:19: error: reference to 'next' is ambiguous
    7 |         to[cnt]=v;next[cnt]=head[u];head[u]=cnt++;
      |                   ^~~~
In file included from /usr/include/c++/14/bits/stl_algobase.h:66,
                 from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:4:20: note:                 'long long int next [200005]'
    4 | int n,M,to[200005],next[200005],head[200005],cnt=1;
      |                    ^~~~
a.cc:8:19: error: reference to 'next' is ambiguous
    8 |         to[cnt]=u;next[cnt]=head[v];head[v]=cnt++;
      |                   ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:4:20: note:                 'long long int next [200005]'
    4 | int n,M,to[200005],next[200005],head[200005],cnt=1;
      |                    ^~~~
a.cc: In function 'void dfs(long long int, long long int)':
a.cc:13:33: error: reference to 'next' is ambiguous
   13 |         for(int i=head[cur];i;i=next[i])
      |                                 ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:4:20: note:                 'long long int next [200005]'
    4 | int n,M,to[200005],next[200005],head[200005],cnt=1;
      |                    ^~~~
a.cc:18:33: error: reference to 'next' is ambiguous
   18 |         for(int i=head[cur];i;i=next[i])
      |                                 ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:4:20: note:                 'long long int next [200005]'
    4 | int n,M,to[200005],next[200005],head[200005],cnt=1;
      |                    ^~~~
a.cc: In function 'void dfs2(long long int, long long int)':
a.cc:30:33: error: reference to 'next' is ambiguous
   30 |         for(int i=head[cur];i;i=next[i])
      |                                 ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:4:20: note:                 'long long int next [200005]'
    4 | int n,M,to[200005],next[200005],head[200005],cnt=1;
      |                    ^~~~