Language: C++

#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<set>
#include<queue>
#include<stack>
#include<bitset>
#include<unordered_map>
#include<functional>
#include<map>
#include<iomanip>
#include<limits>
#include<unordered_set>
#include<cmath>
#include <numeric>
#include <array>
#include<utility>
#include <complex>
#define M_PI 3.141592653589793238
using namespace std;
#define upperbound(v,val) upper_bound(v.begin(),v.end(),val)-v.begin()
#define lowerbound(v,val) lower_bound(v.begin(),v.end(),val)-v.begin()
#define int long long
#define vel vector<long long>
#define vvel vector<vel>
#define rep(i,n) for(int i=0;i<n;i++)
#define sor(v) sort(v.begin(),v.end())
#define mmax(a,b) a=max(a,b)
#define mmin(a,b) a=min(a,b)
#define mkp(a,b) make_pair(a,b)
#define pin pair<int,int>
#define qin pair<pin,int>
#define V vector
#define Endl endl
#define veb vector<bool>
#define fcout cout << fixed << setprecision(15)
#define rev(s) reverse(s.begin(),s.end())
#define lower(h,val) (lower_bound(h.begin(),h.end(),val)-h.begin())
#define upper(h,val) (upper_bound(h.begin(),h.end(),val)-h.begin())
#define vveb V<veb>
#define omajinai cin.tie(0);ios::sync_with_stdio(false);
#define endl "\n"
#define pb push_back
int root(int x, vel& pa) {
	if (pa[x] == -1) { return x; }
	int ans = root(pa[x], pa); pa[x] = ans;
	return ans;
}
bool mar(int x, int y, vel& pa) {
	x = root(x, pa);
	y = root(y, pa);
	if (x != y) { pa[x] = y; }
	return (x != y);
}
int gcd(int x, int y) {
	if (x < y) { return gcd(y, x); }
	if (y == 0) { return x; }
	return gcd(y, x % y);
}
int lcm(int x, int y) {
	x = abs(x); y = abs(y);
	return x * (y / gcd(x, y));
}
long long modinv(long long a, long long m) {
	long long b = m, u = 1, v = 0;
	while (b) {
		long long t = a / b;
		a -= t * b; swap(a, b);
		u -= t * v; swap(u, v);
	}
	u %= m;
	if (u < 0) u += m;
	return u;
}
vel dijk(V<V<pin>> way, int st, int inf) {
	int n = way.size();
	vel dist(n, inf); dist[st] = 0;
	priority_queue<pin, vector<pin>, greater<pin>> pq;
	pq.push(mkp(0, st));
	veb is_checked(n, false);
	while (!pq.empty()) {
		pin x = pq.top(); pq.pop();
		int pot = x.second;
		if (!is_checked[pot]) {
			is_checked[pot] = true;
			for (auto y : way[pot]) {
				int nex_dist = x.first + y.second;
				int nex_pot = y.first;
				if (dist[nex_pot] > nex_dist) {
					dist[nex_pot] = nex_dist;
					pq.push(mkp(nex_dist, y.first));
				}
			}
		}
	}
	return dist;
}
V<V<pin>> make_w(vvel v) {
	int n = v.size();
	V<V<pin>> ret(n);
	rep(i, n) {
		for (int x : v[i]) {
			ret[i].push_back(mkp(x, 1));
		}
	}
	return ret;
}
void make_tree(vvel& chi, vel& par, int n) {
	V<V<pin>> way(n);
	rep(i, n - 1) {
		int a, b; cin >> a >> b; a--; b--;
		way[a].push_back(mkp(b, 1));
		way[b].push_back(mkp(a, 1));
	}
	vel dist = dijk(way, 0, n + 1);
	par = vel(n, -1);
	chi = vvel(n);
	rep(i, n) {
		for (auto nex : way[i]) {
			int pot = nex.first;
			if (dist[pot] > dist[i]) { chi[i].push_back(pot); }
			else { par[i] = pot; }
		}
	}
}
void pri(vel& v) {
	if (v.size() == 0) { return; }
	cout << v[0];
	rep(i, v.size() - 1) { cout << " " << v[i + 1]; }
	cout << endl;
	return;
}
#define ui long long
vvel disj_min(vel& v) {
	int n = v.size();
	vvel ret(22, vel(n));
	ret[0] = v;
	rep(i, 21) {
		rep(j, n) {
			int nex = j + (1 << i);
			if (nex < n) {
				ret[i + 1][j] = min(ret[i][j], ret[i][nex]);
			}
			else {
				ret[i + 1][j] = ret[i][j];
			}
		}
	}
	return ret;
}
int find_min(vvel& dv, int l, int r) {
	int i = 21;
	while (l + (1 << i) > r) {
		i--;
	}
	while (i >= 0) {
		if (dv[i][l] > dv[i][r - (1 << i)]) {
			l = r - (1 << i);
		}
		else {
			r = l + (1 << i);
		}
		i--;
	}
	return l;
}
V<V<pin>> dbl(V<pin>& v) {
	V<V<pin>> ans(20, V<pin>(v));
	int n = v.size();
	rep(i, 19) {
		rep(j, n) {
			ans[i + 1][j].first = ans[i][ans[i][j].first].first;
			ans[i + 1][j].second = max(ans[i][j].second, ans[i][ans[i][j].first].second);
		}
	}
	return ans;
}
int lca(int s, int t, int diff, V<V<pin>>& pa) {
	if (diff < 0) { return lca(t, s, -diff, pa); }
	int ans = 0;
	rep(i, 19) {
		if ((diff & (1 << i)) != 0) {
			mmax(ans, pa[i][s].second);
			s = pa[i][s].first;
		}
	}
	for (int i = 19; i >= 0; i--) {
		if (pa[i][s] != pa[i][t]) {
			mmax(ans, pa[i][s].second);
			s = pa[i][s].first;
			mmax(ans, pa[i][t].second);
			t = pa[i][t].first;
		}
	}
	if (s != t) {
		mmax(ans, pa[0][s].second);
		mmax(ans, pa[0][t].second);
	}
	return ans;
}
void alp(int n, vel& pr) {
	for (int i = 2; i * i <= n; i++) {
		if (n % i == 0) {
			pr.push_back(i);
			while (n % i == 0) { n /= i; }
		}
	}
	if (n != 1) { pr.push_back(n); }
}
vel dx = { 0,0,-1,1,1,-1 };
vel dy = { 1,-1,0,0,1,1 };
#define all(a) a.begin(),a.end()
template<typename T>
void mk_uni(V<T>& a) {
	std::sort(a.begin(), a.end());
	a.erase(std::unique(a.begin(), a.end()), a.end());
}
template <std::uint_fast64_t Modulus> class modint {
	using u64 = std::uint_fast64_t;

public:
	u64 a;

	constexpr modint(const u64 x = 0) noexcept : a(x% Modulus) {}
	constexpr u64& value() noexcept { return a; }
	constexpr const u64& value() const noexcept { return a; }
	constexpr modint operator+(const modint rhs) const noexcept {
		return modint(*this) += rhs;
	}
	constexpr modint operator-(const modint rhs) const noexcept {
		return modint(*this) -= rhs;
	}
	constexpr modint operator*(const modint rhs) const noexcept {
		return modint(*this) *= rhs;
	}
	constexpr modint operator/(const modint rhs) const noexcept {
		return modint(*this) /= rhs;
	}
	constexpr modint& operator+=(const modint rhs) noexcept {
		a += rhs.a;
		if (a >= Modulus) {
			a -= Modulus;
		}
		return *this;
	}
	constexpr modint& operator-=(const modint rhs) noexcept {
		if (a < rhs.a) {
			a += Modulus;
		}
		a -= rhs.a;
		return *this;
	}
	constexpr modint& operator*=(const modint rhs) noexcept {
		a = a * rhs.a % Modulus;
		return *this;
	}
	constexpr modint& operator/=(modint rhs) noexcept {
		u64 exp = Modulus - 2;
		while (exp) {
			if (exp % 2) {
				*this *= rhs;
			}
			rhs *= rhs;
			exp /= 2;
		}
		return *this;
	}
};
//using mint = modint<998244353>;
using mint = modint<1000000007>;
using vem = vector<mint>;
using vvem = vector<vem>;
mint mpow(mint a, int n) {
	mint ans = 1;
	while (n) {
		if (n & 1) { ans *= a; }
		a *= a; n /= 2;
	}
	return ans;
}
vem kai, inv_kai;
void make_kai(int n) {
	kai = vem(n + 1, 1);
	inv_kai = vem(n + 1, 1);
	rep(i, n) { kai[i + 1] = kai[i] * (i + 1); }
	inv_kai[n] = (mint)1 / kai[n];
	for (int i = n; i > 0; i--) { inv_kai[i - 1] = inv_kai[i] * i; }
}
mint com(int n, int r) {
	if (n < 0 || r < 0 || n < r) { return 0; }
	return kai[n] * inv_kai[r] * inv_kai[n - r];
}
mint per(int n, int r) {
	if (n < 0 || r < 0 || n < r) { return 0; }
	return kai[n] * inv_kai[r];
}
int val(vvel& abc, int a, int b) {
	vel v = { a,b,0 };
	auto itr = lower_bound(all(abc),v);
	if (itr != abc.end()) {
		vel w = *itr;
		if (w[0] == a && w[1] == b) { return w[2]; }
	}
	return -1;
}
signed main() {
	omajinai;
	int n; cin >> n;
	vel uni(n, -1);
	vel p(n);
	int ze = 0;
	vel lz;
	rep(i, n) {
		cin >> p[i];
		if (p[i] > 0) { p[i]--; mar(i, p[i], uni); }
		else { ze++; lz.push_back(i); }
	}
	rep(i, ze) { lz[i] = root(lz[i], uni); }
	vel cnt(ze);
	rep(i, n) {
		rep(j, ze) {
			if (lz[j] == root(i, uni)) { cnt[j]++; }
		}
	}
	int ex = 0;
	rep(i, n) { if (uni[i] == -1) { ex++; } }
	ex -= ze;
	make_mkai(n + 1);
	vem dp(ze + 1, 0); dp[0] = 1;
	rep(i, ze) {
		for (int j = ze; j > 0; j--) {
			dp[j] += dp[j - 1] * cnt[i];
		}
	}
	mint ans = 0;
	if (ze >= 1) {
		dp[1] -= ze;
	}
	for (int i = 1; i <= ze; i++) {
		mint pl = dp[i] * mkai[i - 1];
		pl *= mpow(n - 1, ze - i);
		ans += pl;
	}
	ans += mpow(n - 1, ze) * ex;
	ans = mpow(n - 1, ze) * n - ans;
	cout << ans.value() << endl;
	return 0;
}

signed main() {
	omajinai;
	int n; cin >> n;
	vel uni(n, -1);
	vel p(n);
	int ze = 0;
	vel lz;
	rep(i, n) {
		cin >> p[i];
		if (p[i] > 0) { p[i]--; mar(i, p[i], uni); }
		else { ze++; lz.push_back(i); }
	}
	rep(i, ze) { lz[i] = root(lz[i], uni); }
	vel cnt(ze);
	rep(i, n) {
		rep(j, ze) {
			if (lz[j] == root(i, uni)) { cnt[j]++; }
		}
	}
	int ex = 0;
	rep(i, n) { if (uni[i] == -1) { ex++; } }
	ex -= ze;
	make_mkai(n + 1);
	vem dp(ze + 1, 0); dp[0] = 1;
	rep(i, ze) {
		for (int j = ze; j > 0; j--) {
			dp[j] += dp[j - 1] * cnt[i];
		}
	}
	mint ans = 0;
	if (ze >= 1) {
		dp[1] -= ze;
	}
	for (int i = 1; i <= ze; i++) {
		mint pl = dp[i] * mkai[i - 1];
		pl *= mpow(n - 1, ze - i);
		ans += pl;
	}
	ans += mpow(n - 1, ze) * ex;
	ans = mpow(n - 1, ze) * n - ans;
	cout << ans.value() << endl;
	return 0;
}

Compiler output:
a.cc:20:9: warning: "M_PI" redefined
   20 | #define M_PI 3.141592653589793238
      |         ^~~~
In file included from /usr/include/c++/14/cmath:47,
                 from a.cc:15:
/usr/include/math.h:1121:10: note: this is the location of the previous definition
 1121 | # define M_PI           3.14159265358979323846  /* pi */
      |          ^~~~
a.cc:219:11: error: 'std::uint_fast64_t' has not been declared
  219 | template <std::uint_fast64_t Modulus> class modint {
      |           ^~~
a.cc:220:26: error: 'uint_fast64_t' in namespace 'std' does not name a type
  220 |         using u64 = std::uint_fast64_t;
      |                          ^~~~~~~~~~~~~
a.cc:20:1: note: 'std::uint_fast64_t' is defined in header '<cstdint>'; this is probably fixable by adding '#include <cstdint>'
   19 | #include <complex>
  +++ |+#include <cstdint>
   20 | #define M_PI 3.141592653589793238
a.cc:223:9: error: 'u64' does not name a type
  223 |         u64 a;
      |         ^~~
a.cc:225:32: error: 'u64' does not name a type
  225 |         constexpr modint(const u64 x = 0) noexcept : a(x% Modulus) {}
      |                                ^~~
a.cc:226:19: error: 'u64' does not name a type
  226 |         constexpr u64& value() noexcept { return a; }
      |                   ^~~
a.cc:227:25: error: 'u64' does not name a type
  227 |         constexpr const u64& value() const noexcept { return a; }
      |                         ^~~
a.cc:272:20: error: 'mint' was not declared in this scope; did you mean 'uint'?
  272 | using vem = vector<mint>;
      |                    ^~~~
      |                    uint
a.cc:272:24: error: template argument 1 is invalid
  272 | using vem = vector<mint>;
      |                        ^
a.cc:272:24: error: template argument 2 is invalid
a.cc:273:21: error: 'vem' was not declared in this scope; did you mean 'veb'?
  273 | using vvem = vector<vem>;
      |                     ^~~
      |                     veb
a.cc:273:24: error: template argument 1 is invalid
  273 | using vvem = vector<vem>;
      |                        ^
a.cc:273:24: error: template argument 2 is invalid
a.cc:274:1: error: 'mint' does not name a type; did you mean 'uint'?
  274 | mint mpow(mint a, int n) {
      | ^~~~
      | uint
a.cc:282:1: error: 'vem' does not name a type; did you mean 'veb'?
  282 | vem kai, inv_kai;
      | ^~~
      | veb
a.cc: In function 'void make_kai(long long int)':
a.cc:284:9: error: 'kai' was not declared in this scope
  284 |         kai = vem(n + 1, 1);
      |         ^~~
a.cc:284:15: error: 'vem' was not declared in this scope; did you mean 'veb'?
  284 |         kai = vem(n + 1, 1);
      |               ^~~
      |               veb
a.cc:285:9: error: 'inv_kai' was not declared in this scope
  285 |         inv_kai = vem(n + 1, 1);
      |         ^~~~~~~
a.cc:287:23: error: 'mint' was not declared in this scope; did you mean 'uint'?
  287 |         inv_kai[n] = (mint)1 / kai[n];
      |                       ^~~~
      |                       uint
a.cc: At global scope:
a.cc:290:1: error: 'mint' does not name a type; did you mean 'uint'?
  290 | mint com(int n, int r) {
      | ^~~~
      | uint
a.cc:294:1: error: 'mint' does not name a type; did you mean 'uint'?
  294 | mint per(int n, int r) {
      | ^~~~
      | uint
a.cc: In function 'int main()':
a.cc:329:9: error: 'make_mkai' was not declared in this scope; did you mean 'make_kai'?
  329 |         make_mkai(n + 1);
      |         ^~~~~~~~~
      |         make_kai
a.cc:330:9: error: 'vem' was not declared in this scope; did you mean 'veb'?
  330 |         vem dp(ze + 1, 0); dp[0] = 1;
      |         ^~~
      |         veb
a.cc:330:28: error: 'dp' was not declared in this scope; did you mean 'p'?
  330 |         vem dp(ze + 1, 0); dp[0] = 1;
      |                            ^~
      |                            p
a.cc:336:9: error: 'mint' was not declared in this scope; did you mean 'uint'?
  336 |         mint ans = 0;
      |         ^~~~
      |         uint
a.cc:341:21: error: expected ';' before 'pl'
  341 |                 mint pl = dp[i] * mkai[i - 1];
      |                     ^~~
      |                     ;
a.cc:342:17: error: 'pl' was not declared in this scope; did you mean 'p'?
  342 |                 pl *= mpow(n - 1, ze - i);
      |                 ^~
      |                 p
a.cc:342:23: error: 'mpow' was not declared in this scope; did you mean 'pow'?
  342 |                 pl *= mpow(n - 1, ze - i);
      |                       ^~~~
      |                       pow
a.cc:343:17: error: 'ans' was not declared in this scope; did you mean 'abs'?
  343 |                 ans += pl;
      |                 ^~~
      |                 abs
a.cc:345:9: error: 'ans' was not declared in this scope; did you mean 'abs'?
  345 |         ans += mpow(n - 1, ze) * ex;
      |         ^~~
      |         abs
a.cc:345:16: error: 'mpow' was not declared in this scope; did you mean 'pow'?
  345 |         ans += mpow(n - 1, ze) * ex;
      |                ^~~~
      |                pow
a.cc: At global scope:
a.cc:351:8: error: redefinition of 'int main()'
  351 | signed main() {
      |        ^~~~
a.cc:307:8: note: 'int main()' previously defined here
  307 | signed main() {
      |        ^~~~
a.cc: In function 'int main()':
a.cc:373:9: error: 'make_mkai' was not declared in this scope; did you mean 'make_kai'?
  373 |         make_mkai(n + 1);
      |         ^~~~~~~~~
      |         make_kai
a.cc:374:9: error: 'vem' was not declared in this scope; did you mean 'veb'?
  374 |         vem dp(ze + 1, 0); dp[0] = 1;
      |         ^~~
      |         veb
a.cc:374:28: error: 'dp' was not declared in this scope; did you mean 'p'?
  374 |         vem dp(ze + 1, 0); dp[0] = 1;
      |                            ^~
      |                            p
a.cc:380:9: error: 'mint' was not declared in this scope; did you mean 'uint'?
  380 |         mint ans = 0;
      |         ^~~~
      |         uint
a.cc:385:21: error: expected ';' before 'pl'
  385 |                 mint pl = dp[i] * mkai[i - 1];
      |                     ^~~
      |                     ;
a.cc:386:17: error: 'pl' was not declared in this scope; did you mean 'p'?
  386 |                 pl *= mpow(n - 1, ze - i);
      |                 ^~
      |                 p
a.cc:386:23: error: 'mpow' was not declared in this scope; did you mean 'pow'?
  386 |                 pl *= mpow(n - 1, ze - i);
      |                       ^~~~
      |                       pow
a.cc:387:17: error: 'ans' was not declared in this scope; did you mean 'abs'?
  387 |                 ans += pl;
      |                 ^~~
      |                 abs
a.cc:389:9: error: 'ans' was not declared in this scope; did you mean 'abs'?
  389 |         ans += mpow(n - 1, ze) * ex;
      |         ^~~
      |         abs
a.cc:389:16: error: 'mpow' was not declared in this scope; did you mean 'pow'?
  389 |         ans += mpow(n - 1, ze) * ex;
      |                ^~~~
      |                pow