#ifdef DEBUG
#define _GLIBCXX_DEBUG
#endif
//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
typedef long double ld;
typedef long long ll;
const int mod = (int)1e9 + 7;
int mult(int a, int b) {
    return (1LL * a * b) % mod;
}
int sum(int a, int b) {
    int s = a + b;
    if (s >= mod) s -= mod;
    return s;
}
int sub(int a, int b) {
    int s = a - b;
    if (s < 0) s += mod;
    return s;
}
const int maxN = 5005;
int pw[maxN];
int n;
int p[maxN];
vector < int > g[maxN];
bool used[maxN];
int par[maxN];
int sz[maxN];
void dfs(int v) {
    used[v] = true;
    sz[v] = 1;
    for (int to : g[v]) {
        if (!used[to]) {
            par[to] = par[v];
            dfs(to);
            sz[v] += sz[to];
        }
    }
}
int d[maxN][maxN];
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
//    freopen("input.txt", "r", stdin);
    cin >> n;
    int k = 0;
    vector < int > id;
    for (int i = 1; i <= n; i++) {
        cin >> p[i];
        if (p[i] != -1) {
            g[p[i]].emplace_back(i);
            g[i].emplace_back(p[i]);
        }
        else {
            k++;
            id.emplace_back(i);
        }
    }
    int c = 0;
    for (int i = 1; i <= n; i++) {
        if (!used[i]) {
            par[i] = i;
            dfs(i);
            c++;
        }
    }
    pw[0] = 1;
    for (int i = 1; i <= n; i++) pw[i] = mult(pw[i - 1], n - 1);
    int ans = mult(pw[k], n);
    int cycles = c - k;
    ans = sub(ans, mult(cycles, pw[k]));
    d[0][0] = 1;
    for (int i = 0; i < id.size(); i++) {
        int cur_sz = sz[par[id[i]]];
        ans = sub(ans, mult(pw[k - 1], (cur_sz - 1)));
        for (int was = 0; was <= i; was++) {
            d[i + 1][was] = sum(d[i + 1][was], d[i][was]);
            d[i + 1][was + 1] = sum(d[i + 1][was + 1], mult(d[i][was], cur_sz));
        }
    }
    int fct = 1;
    for (int i = 2; i <= k; i++) {
        fct = mult(fct, i - 1);
        int R = d[k][i];
        R = mult(R, fct);
        ans = sub(ans, mult(pw[k - i], R));
    }
    cout << ans;
    return 0;
}