#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef vector<vector<int> > vvi;
typedef vector<pair<int, int> > vpii;
typedef pair<int, int> pii;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
typedef vector<ll> vll;
#define INF 0x3f3f3f3f
#define MOD 1000000007LL
#define HF   500000004LL
#define EPS 0.00001
#define f first
#define s second
#define pb push_back

#define FOR(i, a, b) for (int i=(a); i<=(signed)(b); i++)
#define F0R(i, a) for (int i=0; i<(signed)(a); i++)
#define RFOR(i, a, b) for (int i=(a); i >= b; i--)

#define MN 5005
int n;
int p[MN];

int dsu[MN];
int ec[MN];
int fd(int x){
    if(dsu[x] < 0) return x;
    return dsu[x] = fd(dsu[x]);
}
bool jn(int x, int y){
    x = fd(x); y = fd(y);
    if(x == y) return false;
    if(dsu[x] > dsu[y]) swap(x, y);
    dsu[x] += dsu[y];
    dsu[y] = x;
    ec[x] += ec[y];
    return true;
}

ll modExp(ll b, ll ex){
    ll prod = 1;
    while(ex > 0){
        if(ex % 2 == 0){
            b = (b * b) % MOD;
            ex /= 2;
        } else{
            prod = (prod*b) % MOD;
            ex--;
        }
    }
    
    return prod;
}
ll aah[MN];
void mult(int sz){
    RFOR(i, n, 1){
        aah[i] = (aah[i]+aah[i-1]*sz)%MOD;
    }
}
ll fac[MN];
int main(){
    //ios_base::sync_with_stdio(false);
    //cin.tie(0);

    int cyccnt = 0;
    int uncnt = 0;
    cin >> n;
    FOR(i, 1, n) dsu[i] = -1;
    FOR(i, 1, n){
        cin >> p[i];
        if(p[i] != -1) ec[fd(i)]++;
        //cout << "hi\n";
        if(p[i] == -1) ++uncnt;
        else if(!jn(i, p[i])) ++cyccnt;
    }
    int bruh = 0;
    FOR(i, 1, n){
        if(p[i] == -1){
            int x = fd(i);
            if(dsu[x]+ec[x] != 0) ++bruh;
            //cout << i << " yay\n";
        }
    }
    //cout << "hi\n";
    ll ans = (modExp(n-1, uncnt)*cyccnt)%MOD;
    aah[0] = 1;
    //FOR(i, 1, n) cout << dsu[i] << " " << ec[i] << "\n";
    FOR(i, 1, n){
        if(dsu[i] < 0 && ec[i]+dsu[i] != 0){
            //cout << "mult " << i << " " << dsu[i] << "\n";
            mult(-dsu[i]); //multiply by 1+sz*x
        }
    }
    fac[0] = 1;
    FOR(i, 1, n){
        fac[i] = (fac[i-1]*i)%MOD;
    }
    //cout << "hi\n";
    //F0R(i, uncnt+1) cout << aah[i] << " ";
    //cout << "\n";
    FOR(i, 1, uncnt){
        //if i=1, answer loses a bit
        ll numCycs = (aah[i]*fac[i-1])%MOD;
        if(i == 1){
            numCycs = (numCycs-bruh+MOD)%MOD;
        }
        ll ot = modExp(n-1, uncnt-i);
        ans = (ans+numCycs*ot)%MOD;
    }
    //cout << ans << "\n";
    ll fin = (modExp(n-1, uncnt)*n-ans+MOD)%MOD;
    cout << fin << "\n";

    return 0;
}
