Language: C++

#include <bits/stdc++.h>
#define R register
#define mp make_pair
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
using namespace std;
const int mod = 1e9 + 7, N = 5100;

int n, m, p[N], fa[N], size[N];
ll f[N];
vector<int> a;

inline int addMod(int a, int b) {
	return (a += b) >= mod ? a - mod : a;
}

inline ll quickpow(ll base, ll pw) {
	ll ret = 1;
	while (pw) {
		if (pw & 1) ret = ret * base % mod;
		base = base * base % mod, pw >>= 1;
	}
	return ret;
}

template <class T>
inline void read(T &x) {
	x = 0;
	char ch = getchar(), w = 0;
	while (!isdigit(ch)) w = (ch == '-'), ch = getchar();
	while (isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
	x = w ? -x : x;
	return;
}

int find(int x) {
	return fa[x] == x ? x : fa[x] = find(fa[x]);
}

inline void unite(int x, int y) {
	x = find(x), y = find(y);
	if (x == y) return;
	fa[x] = y, size[y] += size[x];
	return;
}

int main() {
	read(n), m = n;
	for (R int i = 1; i <= n; ++i) fa[i] = i, size[i] = 1;
	for (R int i = 1; i <= n; ++i) {
		read(p[i]);
		if (p[i] != -1) unite(i, p[i]);
	}
	int sum = 0;
	for (R int i = 1; i <= n; ++i) {
		if (fa[i] != i) continue;
		if (p[i] == -1) a.push_back(size[i]), sum += size[i];
		--m;
	}
	if (!a.size()) {
		cout << m * quickpow(n - 1, a.size()) << endl;
		return 0;
	}
	int cur = 0;
	ll ans = ((n * a.size() - sum) * quickpow(n - 1, a.size() - 1) + m * quickpow(n - 1, a.size())) % mod;
	f[0] = 1;
	for (auto &v : a) {
		++cur;
		ll s = 0, w = quickpow(n - 1, a.size() - cur);
		for (R int i = cur; i; --i) {
			s = (s + f[i] * w) % mod;
			f[i] = (f[i] + f[i - 1] * v % mod * i) % mod;
			if (i != cur) w = w * (n - 1) % mod;
		}
		ans = (ans - s * v % mod + mod) % mod;
	}
	cout << ans << endl;
	return 0;
}

Compiler output:
a.cc: In function 'void unite(int, int)':
a.cc:44:20: error: reference to 'size' is ambiguous
   44 |         fa[x] = y, size[y] += size[x];
      |                    ^~~~
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: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:10:24: note:                 'int size [5100]'
   10 | int n, m, p[N], fa[N], size[N];
      |                        ^~~~
a.cc:44:31: error: reference to 'size' is ambiguous
   44 |         fa[x] = y, size[y] += size[x];
      |                               ^~~~
/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:10:24: note:                 'int size [5100]'
   10 | int n, m, p[N], fa[N], size[N];
      |                        ^~~~
a.cc: In function 'int main()':
a.cc:50:20: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   50 |         for (R int i = 1; i <= n; ++i) fa[i] = i, size[i] = 1;
      |                    ^
a.cc:50:51: error: reference to 'size' is ambiguous
   50 |         for (R int i = 1; i <= n; ++i) fa[i] = i, size[i] = 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:10:24: note:                 'int size [5100]'
   10 | int n, m, p[N], fa[N], size[N];
      |                        ^~~~
a.cc:51:20: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   51 |         for (R int i = 1; i <= n; ++i) {
      |                    ^
a.cc:56:20: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   56 |         for (R int i = 1; i <= n; ++i) {
      |                    ^
a.cc:58:45: error: reference to 'size' is ambiguous
   58 |                 if (p[i] == -1) a.push_back(size[i]), sum += size[i];
      |                                             ^~~~
/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:10:24: note:                 'int size [5100]'
   10 | int n, m, p[N], fa[N], size[N];
      |                        ^~~~
a.cc:58:62: error: reference to 'size' is ambiguous
   58 |                 if (p[i] == -1) a.push_back(size[i]), sum += size[i];
      |                                                              ^~~~
/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:10:24: note:                 'int size [5100]'
   10 | int n, m, p[N], fa[N], size[N];
      |                        ^~~~
a.cc:71:28: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   71 |                 for (R int i = cur; i; --i) {
      |                            ^