#include <bits/stdc++.h>
using namespace std;

#define SZ(v) ((int)(v).size())
using ll = long long;

struct UF
{
	vector<int> id;

	UF(int n)
	{
		id.assign(n, -1);
	}

	int find(int u)
	{
		if (id[u] < 0)
			return u;
		return id[u] = find(id[u]);
	}

	int getSize(int u)
	{
		return -id[find(u)];
	}

	bool merge(int u, int v)
	{
		u = find(u), v = find(v);
		if (u == v) return false;
		if (id[u] < id[v]) swap(u, v);
		id[u] += id[v];
		id[v] = u;
		return true;
	}
};

template<const int32_t MOD>
struct ModInt {
	long long x;
	ModInt() : x(0) {}
	ModInt(long long u) : x(u) {}
	ModInt& operator = (const ModInt &m) { x = m.x; return *this; }
	friend bool operator == (const ModInt& a, const ModInt& b) { return a.x == b.x; }
	friend bool operator != (const ModInt& a, const ModInt& b) { return a.x != b.x; }
	friend bool operator < (const ModInt& a, const ModInt& b) { return a.x < b.x; }
	friend bool operator > (const ModInt& a, const ModInt& b) { return a.x > b.x; }
	friend bool operator <= (const ModInt& a, const ModInt& b) { return a.x <= b.x; }
	friend bool operator >= (const ModInt &a, const ModInt& b) { return a.x >= b.x; }
	static ModInt sign(long long k) { return ((k & 1) ? ModInt(MOD-1) : ModInt(1)); }

 
	ModInt& operator += (const ModInt& m) { x += m.x; if(x >= MOD) x -= MOD; return *this; }
	ModInt& operator -= (const ModInt& m) { x -= m.x; if(x < 0LL) x += MOD; return *this; }
	ModInt& operator *= (const ModInt& m) { x = (1LL * x * m.x) % MOD; return *this; }
	
	friend ModInt operator - (const ModInt& a) { ModInt res(a); if(res.x) res.x = MOD - res.x; return res; }
	friend ModInt operator + (const ModInt& a, const ModInt& b) { return ModInt(a) += ModInt(b); }
	friend ModInt operator - (const ModInt& a, const ModInt& b) { return ModInt(a) -= ModInt(b); }
	friend ModInt operator * (const ModInt& a, const ModInt& b) { return ModInt(a) *= ModInt(b); }
	
	static long long fp(long long u, long long k) {
		long long res = 1LL;
		while(k > 0LL) {
			if(k & 1LL) res = (res * u) % MOD;
			u = (u * u) % MOD;
			k /= 2LL;
		}
		return res;
	}
	
	ModInt fastpow(long long k) { return ModInt(fp(x, k)); }
	ModInt inv() { return ModInt(fp(x, MOD-2)); }
	ModInt& operator /= (const ModInt& m) { return *this *= ModInt(m).inv(); }
	friend ModInt operator / (const ModInt& a, const ModInt& b) { return ModInt(a) *= ModInt(b).inv(); }
 
	friend ostream& operator << (ostream& out, const ModInt& a) { return out << a.x; }
};

using Mint = ModInt<(int)1e9+7>;

int main(void)
{
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

	int nbSommets;
	cin >> nbSommets;
	vector<int> P(nbSommets);

	UF dsu = UF(nbSommets);
	int nbAretes(0);

	for (int i(0); i < nbSommets; ++i)
	{
		cin >> P[i];
		if (P[i] != -1)
		{
			--P[i];
			nbAretes += dsu.merge(i, P[i]);
		}
	}

	int nbRestant(0);
	vector<int> tailles;
	for (int i(0); i < nbSommets; ++i)
		if (P[i] == -1)
		{
			nbRestant++;
			tailles.push_back(dsu.getSize(i));
		}

	if (!nbRestant)
	{
		cout << nbAretes << endl;
		return 0;
	}

	vector<vector<Mint>> nbCycles(nbRestant, vector<Mint>(nbRestant+1));

	nbCycles[0][1] = tailles[0];
	for (int i(1); i < nbRestant; ++i)
	{
		nbCycles[i][1] = nbCycles[i-1][1] + tailles[i];
		for (int j(2); j <= i+1; ++j)
			nbCycles[i][j] = nbCycles[i-1][j] + tailles[i] * nbCycles[i-1][j-1];
	}

	Mint ans = (nbRestant + nbAretes) * Mint(nbSommets-1).fastpow(nbRestant);
	Mint fact(1);
	for (int tailleCycle(2); tailleCycle <= nbRestant; ++tailleCycle)
	{
		fact *= tailleCycle-1;
		ans -= fact * nbCycles[nbRestant-1][tailleCycle] * Mint(nbSommets-1).fastpow(nbRestant-tailleCycle);
	}
	for (int i(0); i < nbRestant; ++i)
		ans -= (tailles[i] - 1) * Mint(nbSommets-1).fastpow(nbRestant-1);
	cout << ans << endl;
}


Measured memory_bytes: 198800