#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll p[5009];
ll f[5009];
ll dp[5009][5009];
ll pw[5009];
vector<ll> v[5009];
ll mod = 1e9 + 7;
ll A,B;
void dfs(ll x){
    A++;
    if(p[x] == -1) B++;
    f[x] = 1;
    for(ll y : v[x]){
        if(f[y] == 0) dfs(y);
    }
}
int main(){
    ll n;
    cin >> n;
    for(ll i = 1; i <= n; i++){
        cin >> p[i];
        if(p[i] != -1){
            v[i].push_back(p[i]);
            v[p[i]].push_back(i);
        }
    }
    vector<ll > u;
    ll dead = 0, C = 0;
    for(ll i =1; i <= n; i++){
        if(f[i] == 0){
            A = 0;
            B = 0;
            dfs(i);
            if(B == 0) dead += A, C++;
            else u.push_back(A);
        }
    }
    ll ans, cur = 1, X;
    ll S = u.size();
    pw[0] = 1;
    for(ll i = 1; i <= S; i++)
        pw[i] = pw[i-1] * (n-1) % mod;
    X = pw[S-1];
    ans = (n - C) * pw[S] % mod;
    if(!u.size()) {cout<<ans<<endl; return 0;}
    dp[0][0] = n-1;
    dp[0][1] = u[0];
    for(ll i = 1; i < S; i++){
        for(int j = 0; j <= i+1; j++){
            int P = 1;
            if(j > 2) P = j -1;
            dp[i][j] = (n - 1) * dp[i-1][j] % mod;
            if(j >= 1) dp[i][j] = (dp[i][j] + u[i] * dp[i-1][j-1] % mod * P % mod) %mod;
        }
    }
    for(int i = 0; i < S; i++)
    for(int j = 1; j <= S; j++){
            ll P = 1;
            if(j > 2) P = j -1;
            
            if(j == 1 || i > 0)
                ans= (ans + mod - pw[S-i-1] * (i ? dp[i-1][j-1] : 1)%mod * u[i] %mod* P%mod ) % mod;
    }
    cout<<(ans + mod + X * (ll)S%mod)%mod<<endl;
}
