Language: C++

//https://atcoder.jp/contests/nomura2020/tasks/nomura2020_d
#include<bits/stdc++.h>
using namespace std;
#define nn 5008
#define mod 1000000007
#define int long long 
vector<int> v[nn];int n,pa[nn];
int unknown[nn];int og[nn];
int find(int x){
	if(pa[x]==x) return x;
	return pa[x]=find(pa[x]);	
}
void link(int x,int y){
	pa[find(x)]=find(y);
}
int qpow(int x,int y){
	int res=1;
	while(y){
		if(y&1) res=res*x%mod;
		x=x*x%mod;y=y/2;
	}
	return res;
}
void add(int&x,int y){
	x=(x+y)%mod;
}

int dp[nn],size[nn],ans,f[nn];;
signed main(){
	f[0]=1;for(int i=1;i<nn;i++) f[i]=f[i-1]*i%mod;
	scanf("%lld",&n);for(int i=1;i<=n;i++) pa[i]=i;int uk=0;
	for(int i=1;i<=n;i++){
		int pp;scanf("%lld",&pp);
		if(pp==-1) og[i]=1,uk++;
		else link(pp,i);
	}
	for(int i=1;i<=n;i++) unknown[find(i)]+=og[i],size[find(i)]++;
	for(int i=1;i<=n;i++) if(i==find(i) and !unknown[i]) ans+=qpow(n-1,uk),ans%=mod;
	vector<int> dope;
	for(int i=1;i<=n;i++) if(find(i)==i and unknown[i]) dope.push_back(size[i]),add(ans,(size[i]-1)*qpow(n-1,uk-1));
	
	
	dp[0]=1;for(int i=0;i<dope.size();i++) for(int j=nn-2;j>=0;j--) add(dp[j+1],dp[j]*dope[i]);
	
	for(int i=2;i<nn;i++) add(ans,dp[i]*f[i-1]%mod*qpow(n-1,uk-i));
			
		
	ans=qpow(n-1,uk)*n-ans;ans%=mod;ans+=mod;ans%=mod;cout<<ans;
	return 0;
} 

Compiler output:
a.cc: In function 'int main()':
a.cc:37:55: error: reference to 'size' is ambiguous
   37 |         for(int i=1;i<=n;i++) unknown[find(i)]+=og[i],size[find(i)]++;
      |                                                       ^~~~
In file included from /usr/include/c++/14/string:53,
                 from /usr/include/c++/14/bitset:52,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:52,
                 from a.cc:2:
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:28:12: note:                 'long long int size [5008]'
   28 | int dp[nn],size[nn],ans,f[nn];;
      |            ^~~~
a.cc:40:76: error: reference to 'size' is ambiguous
   40 |         for(int i=1;i<=n;i++) if(find(i)==i and unknown[i]) dope.push_back(size[i]),add(ans,(size[i]-1)*qpow(n-1,uk-1));
      |                                                                            ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:28:12: note:                 'long long int size [5008]'
   28 | int dp[nn],size[nn],ans,f[nn];;
      |            ^~~~
a.cc:40:94: error: reference to 'size' is ambiguous
   40 |         for(int i=1;i<=n;i++) if(find(i)==i and unknown[i]) dope.push_back(size[i]),add(ans,(size[i]-1)*qpow(n-1,uk-1));
      |                                                                                              ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:28:12: note:                 'long long int size [5008]'
   28 | int dp[nn],size[nn],ans,f[nn];;
      |            ^~~~