Language: C++

#include<cstdio>
#include<iostream>
#include<vector>
#define mo 1000000007
using namespace std;
int n,dep[2010],i,j,num[2010],x,y,p,f[2010][2010],ans,c[2010],next[2010],first[2010],end[2010];
int pow(int x,int y)
{
	if(!y)return 1;
	int ans=pow(x,y/2);
	ans=1ll*ans*ans%mo;
	if(y&1)ans=1ll*ans*x%mo;
	return ans;
}
int work(int x,bool bo,int up)
{
	int i,sum=1,t=0,sum2=1;
	if(dep[x]==up)return 1;
	vector<int>a[2];
	for(i=first[x];i;i=next[i])
	{
		int v=end[i];
		a[0].push_back(work(v,0,up));
		a[1].push_back(work(v,1,up));
	}
	for(i=0;i<a[0].size();i++)
		sum=1ll*sum*(a[0][i]+a[1][i])%mo;
	for(i=0;i<a[0].size();i++)
		sum2=1ll*sum2*a[0][i]%mo;
	for(i=0;i<a[0].size();i++)
		t=(t+(1ll*sum2*pow(a[0][i],mo-2)%mo*a[1][i]%mo))%mo;
	//cout<<sum<<" "<<t<<endl;
	if(bo)return t;else return(sum-t+mo)%mo;
}
int main()
{
	cin>>n;
	for(i=1;i<=n;i++)
	{
		scanf("%d",&x);
		dep[i]=dep[x]+1;
		p=max(p,dep[i]);
		next[i]=first[x];
		first[x]=i;
		end[i]=i;
	}
	c[0]=1;
	for(i=1;i<=2000;i++)
		c[i]=c[i-1]*2%mo;
	f[0][0]=f[0][1]=1;
	for(i=1;i<=p;i++)
	{
		x=work(0,0,i);
		y=(c[num[i]]-x+mo)%mo;
		for(j=0;j<=i+1;j++)
			f[i][j]=((1ll*f[i-1][j]*x%mo)%mo+((j>0)?(y*f[i-1][j-1]%mo):0))%mo;
	}
	for(i=1;i<=p+1;i++)
		ans=(ans+1ll*i*f[p][i]%mo)%mo;
	cout<<ans<<endl;
}

Compiler output:
a.cc: In function 'int work(int, bool, int)':
a.cc:20:28: error: reference to 'next' is ambiguous
   20 |         for(i=first[x];i;i=next[i])
      |                            ^~~~
In file included from /usr/include/c++/14/string:47,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:2:
/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:6:63: note:                 'int next [2010]'
    6 | int n,dep[2010],i,j,num[2010],x,y,p,f[2010][2010],ans,c[2010],next[2010],first[2010],end[2010];
      |                                                               ^~~~
a.cc:22:23: error: reference to 'end' is ambiguous
   22 |                 int v=end[i];
      |                       ^~~
In file included from /usr/include/c++/14/string:53:
/usr/include/c++/14/bits/range_access.h:116:37: note: candidates are: 'template<class _Tp> const _Tp* std::end(const valarray<_Tp>&)'
  116 |   template<typename _Tp> const _Tp* end(const valarray<_Tp>&) noexcept;
      |                                     ^~~
/usr/include/c++/14/bits/range_access.h:115:31: note:                 'template<class _Tp> _Tp* std::end(valarray<_Tp>&)'
  115 |   template<typename _Tp> _Tp* end(valarray<_Tp>&) noexcept;
      |                               ^~~
/usr/include/c++/14/bits/range_access.h:106:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::end(_Tp (&)[_Nm])'
  106 |     end(_Tp (&__arr)[_Nm]) noexcept
      |     ^~~
/usr/include/c++/14/bits/range_access.h:85:5: note:                 'template<class _Container> constexpr decltype (__cont.end()) std::end(const _Container&)'
   85 |     end(const _Container& __cont) -> decltype(__cont.end())
      |     ^~~
/usr/include/c++/14/bits/range_access.h:74:5: note:                 'template<class _Container> constexpr decltype (__cont.end()) std::end(_Container&)'
   74 |     end(_Container& __cont) -> decltype(__cont.end())
      |     ^~~
In file included from /usr/include/c++/14/bits/range_access.h:36:
/usr/include/c++/14/initializer_list:99:5: note:                 'template<class _Tp> constexpr const _Tp* std::end(initializer_list<_Tp>)'
   99 |     end(initializer_list<_Tp> __ils) noexcept
      |     ^~~
a.cc:6:86: note:                 'int end [2010]'
    6 | int n,dep[2010],i,j,num[2010],x,y,p,f[2010][2010],ans,c[2010],next[2010],first[2010],end[2010];
      |                                                                                      ^~~
a.cc: In function 'int main()':
a.cc:43:17: error: reference to 'next' is ambiguous
   43 |                 next[i]=first[x];
      |                 ^~~~
/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:6:63: note:                 'int next [2010]'
    6 | int n,dep[2010],i,j,num[2010],x,y,p,f[2010][2010],ans,c[2010],next[2010],first[2010],end[2010];
      |                                                               ^~~~
a.cc:45:17: error: reference to 'end' is ambiguous
   45 |                 end[i]=i;
      |                 ^~~
/usr/include/c++/14/bits/range_access.h:116:37: note: candidates are: 'template<class _Tp> const _Tp* std::end(const valarray<_Tp>&)'
  116 |   template<typename _Tp> const _Tp* end(const valarray<_Tp>&) noexcept;
      |                                     ^~~
/usr/include/c++/14/bits/range_access.h:115:31: note:                 'template<class _Tp> _Tp* std::end(valarray<_Tp>&)'
  115 |   template<typename _Tp> _Tp* end(valarray<_Tp>&) noexcept;
      |                               ^~~
/usr/include/c++/14/bits/range_access.h:106:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::end(_Tp (&)[_Nm])'
  106 |     end(_Tp (&__arr)[_Nm]) noexcept
      |     ^~~
/usr/include/c++/14/bits/range_access.h:85:5: note:                 'template<class _Container> constexpr decltype (__cont.end()) std::end(const _Container&)'
   85 |     end(const _Container& __cont) -> decltype(__cont.end())
      |     ^~~
/usr/include/c++/14/bits/range_access.h:74:5: note:                 'template<class _Container> constexpr decltype (__cont.end()) std::end(_Container&)'
   74 |     end(_Container& __cont) -> decltype(__cont.end())
      |     ^~~
/usr/include/c++/14/initializer_list:99:5: note:                 'template<class _Tp> constexpr const _Tp* std::end(initializer_list<_Tp>)'
   99 |     end(initializer_list<_Tp> __ils) noexcept
      |     ^~~
a.cc:6:86: note:                 'int end [2010]'
    6 | int n,dep[2010],i,j,num[2010],x,y,p,f[2010][2010],ans,c[2010],next[2010],first[2010],end[2010];
      |                                                                                      ^~~