Language: C++


#include <bits/stdc++.h>

#define ln                '\n'
#define all(dat)           dat.begin(), dat.end()
#define loop(i, to)        for (int i = 0; i < to; ++i)
#define cont(i, to)        for (int i = 1; i <= to; ++i)
#define circ(i, fm, to)    for (int i = fm; i <= to; ++i)
#define foreach(i, dat)    for (__typeof(dat.begin()) i = dat.begin(); i != dat.end(); ++i)

typedef long long          num;

using namespace std;

template<int mod>
struct modulo {
	int w;
	operator int&() const { return w; }
	
	modulo() {}
	modulo(int w, bool f = 0) { this->w = w; if (f) fix(); }
	void inline fix() { w %= mod; if (w < 0) w += mod; }
	modulo inline fix(int w) const { w %= mod; if (w < 0) w += mod; return w; }
	
	inline modulo& operator += (const modulo &b) { if ((w += b.w) >= mod) w -= mod; return *this; }
	inline modulo& operator -= (const modulo &b) { if ((w -= b.w) < 0) w += mod; return *this; }
	inline modulo& operator *= (const modulo &b) { w = (num) w * b.w % mod; return *this; }
	inline modulo& operator /= (const modulo &b) { w = (num) w * b.inv() % mod; return *this; }
	inline modulo  operator +  (const modulo &b) const { modulo r = *this; r += b; return r; }
	inline modulo  operator -  (const modulo &b) const { modulo r = *this; r -= b; return r; }
	inline modulo  operator *  (const modulo &b) const { modulo r = *this; r *= b; return r; }
	inline modulo  operator /  (const modulo &b) const { modulo r = *this; r /= b; return r; }
	
	modulo inline inv() const {
		int a = w, m = mod, u = 0, v = 1, t;
		for (; a != 0; t = m / a, m -= t * a, swap(a, m), u -= t * v, swap(u, v));
		return fix(u);
	}
	
	modulo inline pow(int p) const {
		if (!w)  return 0;
		modulo a = *this, r = 1;
		for (; p > 0; a *= a, p >>= 1) if (p & 1) r *= a;
		return r;
	}
};
const int mod = int(1e9 + 7);
typedef modulo<mod> rem;

const int nsz = int(5e3);
int n, k, a[nsz + 5];
rem dp[nsz + 5], ans;
vector<int> r;

struct union_find {
	int pr[nsz + 5], siz[nsz + 5];
	
	union_find() {
		cont (u, nsz) pr[u] = u, siz[u] = 1;
	}
	
	int inline operator [] (int id) { return qry(id); }
	
	int qry(int u) { return pr[u] == u ? u : pr[u] = qry(pr[u]); }
	
	bool inline link(int u, int v) {
		int pu = qry(u), pv = qry(v);
		if (pu == pv) return 0;
		pr[pv] = pu;
		siz[pu] += siz[pv];
		return 1;
	}
};
union_find uf;

int main() {
	scanf("%d", &n);
	cont (i, n) {
		scanf("%d", &a[i]);
		if (a[i] != -1) uf.link(a[i], i);
		if (a[i] == -1) r.push_back(i);
	}
	k = int(r.size());
	
	cont (u, n) {
		if (uf[u] != u || a[u] == -1) continue;
		ans += rem(n - 1).pow(k);
	}
	
	dp[0] = 1;
	loop (i, k) {
		int sz = uf.siz[r[i]];
		for (int j = i; j >= 0; --j) {
			dp[j + 1] += dp[j] * (j ? rem(j) : rem(1)) * rem(sz);
		}
	}
	
	cont (j, k) {
		ans += dp[j] * rem(n - 1).pow(k - j);
	}
	cont (u, n) {
		if (uf[u] != u || a[u] != -1) continue;
		ans -= rem(n - 1).pow(k - 1);
	}
	
	ans = rem(n) * rem(n - 1).pow(k) - ans;
	printf("%d\n", ans);
}

Compiler output:
a.cc: In member function 'modulo<mod>::operator int&() const':
a.cc:18:40: error: binding reference of type 'int&' to 'const int' discards qualifiers
   18 |         operator int&() const { return w; }
      |                                        ^