#include <bits/stdc++.h>
#define N 5050
using namespace std;
const int mod = 1e9 + 7;

inline int fsp(int x, int k = mod - 2) {
    int res = 1;
    while (k) {
        if (k & 1) res = 1ll * res * x % mod;
        x = 1ll * x * x % mod, k >>= 1;
    }
    return res;
}

int n, p[N], sz[N], f[N][N], vis[N], t;
vector<int> G[N];

void dfs(int u, int r) {
    vis[u] = 1;
    if (r < 0) sz[-r]++;
    for (int v : G[u])
        if (v != r) dfs(v, r);
        else ++t;
}

int main() {
#ifdef isLOCAL
    freopen(".in", "r", stdin);
    freopen(".out", "w", stdout);
#endif

    cin >> n;
    for (int i = 1; i <= n; ++i) {
        scanf("%d", p + i);
        if (~p[i]) G[p[i]].push_back(i);
    }

    int K = 0;
    for (int i = 1; i <= n; ++i)
        if (p[i] == -1) dfs(i, -(++K));
    for (int i = 1; i <= n; ++i)
        if (!vis[i]) dfs(i, i);

    int ans = 1ll * t * fsp(n - 1, K) % mod;

    f[0][0] = 1;
    for (int i = 1; i <= K; ++i) {
        f[i][0] = 1;
        for (int j = 1; j <= i; ++j)
            f[i][j] = (f[i - 1][j] + 1ll * f[i - 1][j - 1] * sz[i]) % mod;
    }

    if (K) ans = (ans + 1ll * (f[K][1] - K) * fsp(n - 1, K - 1)) % mod;
    for (int i = 2, j = 1; i <= K; ++i) {
        j = 1ll * j * (i - 1) % mod;
        ans = (ans + 1ll * f[K][i] * j % mod * fsp(n - 1, K - i)) % mod;
    }

    ans = (1ll * n * fsp(n - 1, K) - ans) % mod;
    if (ans < 0) ans += mod;
    cout << ans << endl;

    return 0;
}
