#include <bits/stdc++.h>

int ri() {
	int n;
	scanf("%d", &n);
	return n;
}
#define MOD 1000000007

template<int mod>
struct ModInt{
	int x;
	ModInt():x(0){}
	ModInt(long long y):x(y>=0?y%mod:(mod-(-y)%mod)%mod){}
	ModInt &operator+=(const ModInt &p){
		if((x+=p.x)>=mod)x-=mod;
		return *this;
	}
	ModInt &operator-=(const ModInt &p){
		if((x+=mod-p.x)>=mod)x-=mod;
		return *this;
	}
	ModInt &operator*=(const ModInt &p){
		x=(int)(1LL*x*p.x%mod);
		return *this;
	}
	ModInt &operator/=(const ModInt &p){
		*this*=p.inverse();
		return *this;
	}
	ModInt &operator^=(long long p){
		ModInt res = 1;
		for (; p; p >>= 1) {
			if (p & 1) res *= *this;
			*this *= *this;
		}
		return *this = res;
	}
	ModInt operator-()const{return ModInt(-x);}
	ModInt operator+(const ModInt &p)const{return ModInt(*this)+=p;}
	ModInt operator-(const ModInt &p)const{return ModInt(*this)-=p;}
	ModInt operator*(const ModInt &p)const{return ModInt(*this)*=p;}
	ModInt operator/(const ModInt &p)const{return ModInt(*this)/=p;}
	ModInt operator^(long long p)const{return ModInt(*this)^=p;}
	bool operator==(const ModInt &p)const{return x==p.x;}
	bool operator!=(const ModInt &p)const{return x!=p.x;}
	explicit operator int() const { return x; }						   // added by QCFium
	ModInt operator=(const int p) {x = p; return ModInt(*this);} // added by QCFium
	ModInt inverse()const{
		int a=x,b=mod,u=1,v=0,t;
		while(b>0){
			t=a/b;
			a-=t*b;
			std::swap(a,b);
			u-=t*v;
			std::swap(u,v);
		}
		return ModInt(u);
	}
	friend std::ostream &operator<<(std::ostream &os,const ModInt<mod> &p){
		return os<<p.x;
	}
	friend std::istream &operator>>(std::istream &is,ModInt<mod> &a){
		long long x;
		is>>x;
		a=ModInt<mod>(x);
		return (is);
	}
};
typedef ModInt<MOD> mint;

struct MComb {
	std::vector<mint> fact;
	std::vector<mint> inv;
	MComb (int n) { // O(n + log(mod))
		fact = std::vector<mint>(n + 1, 1);
		for (int i = 1; i <= n; i++) fact[i] = fact[i - 1] * mint(i);
		inv.resize(n + 1);
		inv[n] = fact[n] ^ (MOD - 2);
		for (int i = n; i--; ) inv[i] = inv[i + 1] * mint(i + 1);
	}
	mint ncr(int n, int r) {
		return fact[n] * inv[r] * inv[n - r];
	}
	mint npr(int n, int r) {
		return fact[n] * inv[n - r];
	}
	mint nhr(int n, int r) {
		assert(n + r - 1 < (int) fact.size());
		return ncr(n + r - 1, r);
	}
};


int main() {
	int n = ri();
	int a[n];
	for (auto &i : a) i = ri() - 1;
	for (auto &i : a) if (i == -2) i = -1;
	MComb com(10000);
	
	int uncertain = std::count(a, a + n, -1);
	
	std::vector<int> child[n];
	for (int i = 0; i < n; i++) if (a[i] != -1) child[a[i]].push_back(i);
	
	bool used[n];
	memset(used, 0, sizeof(used));
	std::vector<int> dist(n, 1000000000);
	std::vector<mint> ps;
	mint one = 0;
	for (int i = 0; i < n; i++) {
		if (a[i] == -1) {
			std::queue<int> que;
			que.push(i);
			dist[i] = 0;
			int num = 0;
			int cnt = 0;
			while (que.size()) {
				auto i = que.front();
				que.pop();
				used[i] = true;
				if (a[i] == -1) num++;
				cnt++;
				for (auto j : child[i]) dist[j] = dist[i] + 1, que.push(j);
			}
			mint kei = mint(cnt) / (mint(n - 1) ^ num);
			ps.push_back(kei);
			one += mint(cnt - 1) / (mint(n - 1) ^ num);
		}
	}
	
	std::vector<bool> tmpused(n);
	mint res = 0;
	for (int i = 0; i < n; i++) {
		if (!used[i]) {
			int cur = i;
			do {
				tmpused[cur] = true;
				cur = a[cur];
			} while (!tmpused[cur]);
			
			std::queue<int> que;
			que.push(cur);
			int num = 0;
			while (que.size()) {
				auto i = que.front();
				que.pop();
				used[i] = true;
				if (a[i] == -1) num++;
				for (auto j : child[i]) if (!used[j]) used[j] = true, que.push(j);
			}
			res += mint(n - 1) ^ (uncertain - num);
		}
	}
	std::vector<mint> poly{1};
	for (auto i : ps) {
		std::vector<mint> next(poly.size() + 1);
		for (int j = 0; j < (int) poly.size(); j++) {
			next[j + 1] += poly[j] * i;
			next[j] += poly[j];
		}
		poly = next;
	}
	
	res += one * (mint(n - 1) ^ uncertain);
	int m = ps.size();
	for (int i = 2; i <= m; i++) {
		res += poly[i] * com.fact[i - 1] * (mint(n - 1) ^ uncertain);
	}
	res = (mint(n - 1) ^ uncertain) * n - res;
	// res += mint(n - 1) ^ uncertain;
	std::cout << res << std::endl;
	
	return 0;
}


Measured memory_bytes: 4156