#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vl = vector<ll>;
template<class T> using vc = vector<T>;
template<class T> using vvc = vector<vector<T>>;

#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define repr(i, n) for (ll i = (n)-1; i >= 0; i--)
#define repe(i, l, r) for (ll i = (l); i < (r); i++)
#define reper(i, l, r) for (ll i = (r)-1; i >= (l); i--)
#define repa(i,n) for (auto& i: n)

template<class T> inline bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> inline bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
void init() {cin.tie(0);ios::sync_with_stdio(false);cout << fixed << setprecision(15);}

#ifdef DEBUG
template <class T, class N> void verr(const T& a, const N& n) { rep(i, n) cerr << a[i] << " "; cerr << "\n" << flush; }
ll dbgt = 1; void err() { cerr << "passed " << dbgt++ << "\n" << flush; }
template<class H, class... T> void err(H&& h,T&&... t){ cerr<< h << (sizeof...(t)?" ":"\n") << flush; if(sizeof...(t)>0) err(forward<T>(t)...); }
#endif

const ll INF = 5e18;
const ld EPS = 1e-11;
const ld PI = acos(-1.0L);
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
//--------------------------------------------------------------------------------//
struct UnionFind {
private:
    vector<int> par;
    vector<int> count;
    vector<int> rank;
public:
    UnionFind(int N) {
        count.assign(N, 1);
        rank.assign(N, 0);
        par.assign(N, 0);
        rep(i, N) par[i] = i;
    }

    int root(int x) {
        if (par[x] == x)
            return x;
        else {
            par[x] = root(par[x]);
            return par[x];
        }
    }

    void unite(int x, int y) {
        x = root(x), y = root(y);
        if (x == y) return;
        if (rank[x] < rank[y])
            swap(x, y);
        else if (rank[x] == rank[y])
            rank[y]++;
        par[y] = x;
        count[x] += count[y];
        return;
    }

    int size(int x) {
        return count[root(x)];
    }

    bool issame(int x, int y) {
        return root(x) == root(y);
    }
};
using UF = struct UnionFind;

ll modpow(ll a, ll n, ll mod_) {
    ll res = 1;
    while (n > 0) {
        if (n & 1) res = res * a % mod_;
        a = a * a % mod_;
        n >>= 1;
    }
    return res;
}

const ll MAX_SIZE = 100005;
array<ll, MAX_SIZE> fac, inv, finv;
void finit() {
    // combination init
    fac[0] = 1;
    for (ll i = 1; i < MAX_SIZE; i++) fac[i] = fac[i - 1] * i % MOD;
    finv[MAX_SIZE - 1] = modpow(fac[MAX_SIZE - 1], MOD - 2, MOD);
    for(ll i = MAX_SIZE - 1; i > 0; i--) finv[i - 1] = finv[i] * i % MOD;
    
    //inv init
    repe(i, 1, MAX_SIZE) inv[i] = modpow(i, MOD - 2, MOD);
}

ll perm(ll a, ll b) { return fac[a] * finv[a - b] % MOD; }

ll comb(ll a, ll b) { return fac[a] * finv[b] % MOD * finv[a - b] % MOD; }

ll dp[5005][5005];

int main() {
    init();
    finit();
    ll N;
    cin >> N;

    UF uf(N);

    ll cnt = 0;
    vl cand;

    rep(i,N){
        ll p;
        cin >> p;
        if(p>0){
            p--;
            if (!uf.issame(i, p)) uf.unite(i, p), cnt++;
        }else{
            cand.eb(i);
        }
    }

    ll kn = cand.size();
    ll ans = cnt * modpow(N - 1, kn, MOD) % MOD;

    map<ll, bool> mpbool;
    repa(p, cand){
        mpbool[uf.root(p)] = true;
    }

    map<ll, ll> mpsize;
    rep(i,N){
        if(mpsize.find(uf.root(i))==mpsize.end()){
            mpsize[uf.root(i)] = uf.size(i);
        }
    }

    map<ll, ll> mpt, mpf;
    repa(p, mpsize){
        if(mpbool[p.first])
            mpt[p.first] = p.second;
        else
            mpf[p.first] = p.second;
    }

    ll p1 = (kn >= 1 ? modpow(N - 1, kn - 1, MOD) : 0), p2 = (kn >= 2 ? modpow(N - 1, kn - 2, MOD) : 0);

    for (auto iter = mpt.begin(); iter != mpt.end(); iter++){
        ll nowsize = (*iter).second;
        auto biter = iter;
        for (biter++; biter != mpt.end(); biter++){
            ll tosize = (*biter).second;
            (ans += ((N - 1) * (N - 1) - (N - nowsize - 1) * (N - tosize - 1)) % MOD * p2) %= MOD;
        }
        for (auto ziter = mpf.begin(); ziter != mpf.end(); ziter++){
            ll tosize = (*ziter).second;
            (ans += tosize * p1) %= MOD;
        }
    }

    ll tn = mpt.size();
    dp[0][0] = 1;
    ll ti = 0;
    for(auto iter = mpt.begin(); iter!=mpt.end(); iter++, ti++){
        rep(j,ti+1){
            (dp[ti + 1][j] += dp[ti][j]) %= MOD;
            dp[ti + 1][j + 1] = dp[ti][j] * (*iter).second % MOD;
        }
    }
    // verr(dp[tn], tn + 1);
    repe(i,3,tn+1){
        ans += MOD - dp[tn][i] * fac[i-1] %MOD * modpow(N-1, tn-i, MOD) % MOD;
    }
    ans %= MOD;

    cout << ans << endl;
}

Measured memory_bytes: 123672