//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define rep(i, n) for(int i=0; i<n; ++i)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using namespace std;
using ll = int64_t;
using P = pair<int, int>;
using vs = vector<string>;
using vi = vector<int>;
using vvi = vector<vi>;

const int mod = 1000000007;
//const int mod = 998244353;

struct mint {
    int64_t x;
    mint(int64_t x=0):x((x%mod+mod)%mod){}
    mint operator-() const { return mint(-x);}
    mint& operator+=(const mint a) {
        if ((x += a.x) >= mod) x -= mod;
        return *this;
    }
    mint& operator-=(const mint a) {
        if ((x += mod-a.x) >= mod) x -= mod;
        return *this;
    }
    mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this;}
    mint operator+(const mint a) const { return mint(*this) += a;}
    mint operator-(const mint a) const { return mint(*this) -= a;}
    mint operator*(const mint a) const { return mint(*this) *= a;}
    mint pow(int64_t t) const {
        if (!t) return 1;
        mint a = pow(t>>1);
        a *= a;
        if (t&1) a *= *this;
        return a;
    }

    //for prime mod
    mint inv() const { return pow(mod-2);}
    mint& operator/=(const mint a) { return *this *= a.inv();}
    mint operator/(const mint a) {return mint(*this) /= a;}
};

istream& operator>>(istream& is, const mint& a) { return is >> a.x;}
ostream& operator<<(ostream& os, const mint& a) { return os << a.x;}

struct UnionFind {
    vector<int> par; // 親を指すvector,-par[親]は木のサイズ
    UnionFind(int n):par(n, -1) {} // uniteで親を埋め込んでいく必要あり
    int root(int x) { // 親をたどる&データの整理
        if(par[x] < 0) return x;
        return par[x] = root(par[x]);
    }
    bool unite(int x, int y) { // データの結合
        x = root(x);
        y = root(y);
        if(x == y) return false;
        if(par[x] > par[y]) swap(x, y);
        par[x] += par[y];
        par[y] = x;
        return true;
    }
    bool same(int x, int y) {return root(x) == root(y);} // 所属判定
    int size(int x) {return -par[root(x)];} // 木のサイズ
}uft(5000);

struct combination {
    vector<mint> frac, ifrac;
    combination(int n):frac(n+1), ifrac(n+1) {
        assert(n < mod);
        frac[0] = 1;
        for (int i = 1; i <= n; ++i) frac[i] = frac[i-1]*i;
        ifrac[n] = frac[n].inv();
        for (int i = n; i >= 1; --i) ifrac[i-1] = ifrac[i]*i;
    }
    mint operator()(int n, int k) {
        if (k < 0 || k > n) return 0;
        return frac[n]*ifrac[k]*ifrac[n-k];
    }
} c(5000);

int main() {
    int n;
    cin >> n;
    vi p(n);
    rep(i, n) {
        cin >> p[i];
    }

    vi minus;
    rep(i, n) {
        if(p[i] == -1) {
            minus.push_back(i);
        } else {
            uft.unite(i, p[i]-1);
        }
    }
    vi minus_;
    rep(i, minus.size()) {
        if(uft.root(minus[i]) != minus[i]) {
            minus_.push_back(minus[i]);
        }
    }


    mint ans;
    rep(i, minus_.size()) {
        ans -= mint(uft.size(minus_[i])-1) * mint(n-1).pow((ll)minus.size()-1);
    }
    if(minus.size()) {
        ans += mint(n-1).pow((ll)minus.size()) * mint(minus.size());
        //if(minus.size() > 1) ans -= mint(n-1).pow((ll)minus.size()-2) * (minus.size()-1) * minus.size()/2;
        //cout << (int)minus.size() << endl;

        vector<vector<mint> > dp((int)minus.size(), vector<mint>((int)minus.size()));
        dp[0][0] = uft.size(minus[0]);
        rep(i, minus.size()-1) {
            dp[0][i+1] = mint(uft.size(minus[i+1])) + dp[0][i];
        }
        rep(i, minus.size()-1) {
            dp[i+1][i+1] = dp[i][i] * uft.size(minus[i+1]);
            rep(j, minus.size()-i-2) {
                dp[i+1][j+i+2] = dp[i+1][j+i+1];
                dp[i+1][j+i+2] += dp[i][j+i+1] * uft.size(minus[j+i+2]);
            }
        }

        /*rep(i, minus.size()) {
            rep(j, minus.size()) {
                cout << dp[i][j] << ' ';
            } cout << endl;
        }*/

        rep(i, minus.size()-1) {
            int i_ = i+2;
            ans -= mint(n-1).pow((ll)minus.size()-i_) * dp[i+1][minus.size()-1] * c.frac[i+1];
        }
    }

    //cout << ans << endl;
    mint ans_;
    vi roo;
    rep(i, n) {
        if(p[i] != -1) {
            roo.push_back(uft.root(i));
        }
    }
    sort(all(roo));
    roo.erase(unique(all(roo)), roo.end());
    
    rep(i, roo.size()) {
        ans_ += uft.size(roo[i])-1;
    }
    ans_ *= mint(n-1).pow((ll)minus.size());
    ans += ans_;

    cout << ans << endl;
}

Measured memory_bytes: 198704