#include <bits/stdc++.h>

#define F first
#define S second
#define prev azaza
#define MP make_pair
#define PB push_back

using namespace std;
typedef long long ll;
typedef long double ld;

const int max_n = 5011, inf = 1000111222;
const ll mod = 1000000007;

int powm(int a, int b) {
    if (b == 0) {
        return 1;
    }
    if (b % 2 == 0) {
        return powm((1LL * a * a) % mod, b / 2);
    } else {
        return (1LL * a * powm(a, b - 1)) % mod;
    }
}

ll fact[max_n];
vector<int> a;
vector<int> b;

vector<int > v[max_n];
bool vis[max_n];
int p[max_n];

int sz = 0;
bool isq = 0;
void dfs(int cur){
    vis[cur] = 1;
    ++sz;
    if (p[cur] == -1) {
        isq = 1;
    }
    for (int to : v[cur]) {
        if (!vis[to]) {
            dfs(to);
        }
    }
}
int n;
ll pn[max_n];
ll ss[max_n][max_n];

void init() {
    fact[0] = 1;
    for (int i = 1; i < max_n; ++i) {
        fact[i] = (i * fact[i - 1]) % mod;
    }
    for (int i = 0; i < max_n; ++i) {
        pn[i] = powm(n - 1, i);
    }
    for (int i = 0; i < b.size(); ++i) {
        for (int k = 1; k <= i + 1; ++k) {
            if (i == 0) {
                if (k == 1) {
                    ss[i][k] = b[0];
                } else {
                    ss[i][k] = 0;
                }
                continue;
            }
            ss[i][k] += ss[i - 1][k];
            if (k == 1) {
                ss[i][k] += b[i];
            } else {
                ss[i][k] += (ss[i - 1][k - 1] * b[i]) % mod;
            }
            ss[i][k] %= mod;
        }
    }
}


int main()
{
    //freopen("input.txt", "r", stdin);

    cin >> n;

    for (int i= 0; i < n; ++i) {
        cin >> p[i];
        if (p[i] == -1) {
            continue;
        }
        --p[i];
        v[i].PB(p[i]);
        v[p[i]].PB(i);
    }
    for (int i = 0; i < n; ++i) {
        if (!vis[i]) {
            sz = 0;
            isq = 0;
            dfs(i);
            if (isq) {
                b.PB(sz);
            } else {
                a.PB(sz);
            }
        }
    }
    if (b.size() == 0) {
        ll ans = 0;
        for (int x : a) {
            ans += (x - 1);
        }
        cout << ans << endl;
        return 0;
    }
    init();
    ll ans = 0;
    ll suma = 0;
    for (int x : a) {
        suma += x;
    }
    ll allways = pn[b.size()];
    for (int i = 0; i < b.size(); ++i) {
        ans += allways;
        ans %= mod;
        ll bad_ways = 0;
        for (int kleft = 0; kleft <= i; ++kleft) {
            if (kleft == 0) {
                bad_ways += (b[i] - 1) * pn[b.size() - 1];
                bad_ways %= mod;
                continue;
            }
            ll sum = ss[i - 1][kleft] % mod;
            sum *= fact[kleft];
            sum %= mod;
            sum *= b[i];
            sum %= mod;
            sum *= pn[b.size() - 1 - kleft];
            sum %= mod;
            bad_ways += sum;
            bad_ways %= mod;
        }
        ans -= bad_ways;
        ans = (ans + mod) % mod;
    }
    ll s = 0;
    for (int x : a) {
        s += (x - 1);
    }

    for (int x : b) {
        s += (x - 1);
    }
    ans += (allways * s) % mod;
    ans %= mod;
    cout << ans << endl;
    return 0;
}




Measured memory_bytes: 120548