#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
//typedef pair<ll,ll> P;

class UnionFind{
    public:
        vector<ll> uni;
        UnionFind(ll s) : uni(s, -1) { }
 
       //頂点aが所属するグループを調べる
        ll root(ll a)
        {
            if (uni[a] < 0) return a;
            return uni[a] = root(uni[a]);
        }
    
        //頂点aと頂点bを繋ぐ。もともと同じグループの時falseを返す
        bool connect(ll a,ll b)
        {
            a = root(a);
            b = root(b);
            if (a == b) return false;
    
            if (uni[a] > uni[b])
            {
                a ^= b;
                b ^= a;
                a ^= b;
            }
    
            uni[a] = uni[a] + uni[b];
            uni[b] = a;
            return true;
        }
    
        //頂点a,bが同じグループであるかを調べる
        bool isConnect(ll a,ll b)
        {
            return root(a) == root(b);
        }
    
        //頂点aを含むグループの頂点数を調べる
        ll size(ll a)
        {
            return -uni[root(a)];
        }
    
};

class Mod{
  public:
    ll mod;
    ll kaijou[200005];
    ll inv[200005];
    // mod must be a prime number
    Mod(ll m){
        mod = m;
        kaijou[0] = 1;
        for (ll i = 1;i <= 200003;i++){
            kaijou[i] = kaijou[i-1]*i%mod;
            inv[i] = modpow(kaijou[i],mod-2);
        }
    }
    //a^p
    ll modpow(ll a, ll p){
        if(p == 0) return 1;
        if(p%2 == 0){
            ll half = modpow(a, p/2);
            return half * half % mod;
        }
        else return a * modpow(a,p-1) % mod;
    }

    //nCr
    ll comb(ll n, ll r){
        if(r > n-r) return comb(n,n-r);
        ll mul = 1;
        ll div = 1;
        for (int i = 0;i < r;i++){
            mul *= n-i;
            mul %= mod;
            div *= i+1;
            div %= mod;
        }
        return mul * modpow(div,mod-2) % mod;
    }

    ll fcomb(ll n, ll r){
        if(r > n-r) return fcomb(n,n-r);
        if(r == 0) return 1;
        return (kaijou[n]*inv[n-r]%mod)*inv[r]%mod;
    }
};

ll N;
ll P[6000];
ll cnt_m[6005];
bool used[6005];
bool used2[6005][6005];
ll dp[6000][6000];
ll ans;

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    cin >> N;

    UnionFind uf(N+10);
    Mod md(1e9+7);

    ll K = 0;

    for (int i = 1;i <= N;i++){
        cin >> P[i];
        if(P[i] != -1){
            uf.connect(i,P[i]);
        }
    }

    ll sum = 0;

    vector<ll> lis_size;

    for (int i = 1;i <= N;i++){
        if(P[i] == -1){
            lis_size.push_back(uf.size(i));
            used[uf.root(i)]  = true;
            K++;
        }
    }

    ll ans = N*md.modpow(N-1,K);

    for (int i = 1;i <= N;i++){
        if(!used[uf.root(i)]){
            ans += md.mod-md.modpow(N-1,K);
            ans %= md.mod;
            used[uf.root(i)] = true;
        }
    }

    //cout << ans << endl;

    for (int i = 0;i <= K;i++){
        dp[0][i] = 1;
    }

    for (int i = 1;i <= K;i++){
        for (int j = 1;j <= K;j++){
            dp[i][j] += dp[i][j-1];
            dp[i][j] %= md.mod;
            dp[i][j] += dp[i-1][j-1]*lis_size[j-1];
            dp[i][j] %= md.mod;
            if(i == 1){
                ans = ans + md.mod-((lis_size[j-1]-1)*md.modpow(N-1,K-1)%md.mod);
                ans %= md.mod;
            }
        }
        if(i != 1){
            ans = ans + md.mod-((dp[i][K]*md.modpow(N-1,K-i)%md.mod)*md.kaijou[i-1]%md.mod);
            ans %= md.mod;
        }
    }

    cout << ans << endl;

    return 0;

}

Measured memory_bytes: 221960