#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T, class U> using Pa = pair<T, U>;
template <class T> using vec = vector<T>;
template <class T> using vvec = vector<vec<T>>;

constexpr ll mod = 1e9+7;
struct mint {
    ll x;
    mint(ll x=0):x((x%mod+mod)%mod){}
    
    friend ostream &operator<<(ostream& os,const mint& a){
        return os << a.x;
    }

    friend istream &operator>>(istream& is,mint& a){
        ll t;
        is >> t;
        a = mint(t);
        return (is);
    }

    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 {
        mint res(*this);
        return res+=a;
    }
    mint operator-(const mint a) const {
        mint res(*this);
        return res-=a;
    }
    mint operator*(const mint a) const {
        mint res(*this);
        return res*=a;
    }
    mint pow(ll 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) const {
        mint res(*this);
        return res/=a;
    }
};

class UnionFind{
private:
    vec<int> p,s;
    int cnt;
public:
    vec<int> e;
    UnionFind(){}
    UnionFind(int N):cnt(N),s(N,1),p(N),e(N){
        iota(p.begin(),p.end(),0);
    }
    int find(int x){
        if(p[x]==x) return x;
        else return p[x] = find(p[x]);
    }
    void unite(int x,int y){
        x = find(x); y = find(y);
        if(x==y){
            e[x]++;
            return;
        }
        if(s[x]>s[y]) swap(x,y);
        p[x] = y;
        s[y] += s[x];
        e[y] += e[x]+1;
        cnt--;
    }
    bool is_same_set(int x,int y) {return find(x)==find(y);}
    int size(int x) {return s[find(x)];}
    int compnents_number(){return cnt;}
    bool is_cycle(int x){return s[find(x)]==e[find(x)];}
};

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N;
    cin >> N;
    UnionFind uf(N);
    vec<int> P(N);
    int K = 0;
    for(int i=0;i<N;i++){
        cin >> P[i];
        P[i]--;
        if(P[i]==-2) K++;
    }
    for(int i=0;i<N;i++) if(P[i]>=0) uf.unite(i,P[i]);
    vec<int> cnt(N+1);
    int c = 0;
    vec<int> checked(N);
    vec<mint> fact(N+1,1);
    for(int i=1;i<=N;i++) fact[i] = fact[i-1]*i;
    vec<mint> dp(N+1);
    mint one = 0;
    dp[0] = 1;
    for(int i=0;i<N;i++){
        int p = uf.find(i);
        if(checked[p]) continue;
        checked[p] = 1;
        if(uf.is_cycle(p)) c++;
        else{
            vec<mint> ndp(N+1);
            for(int j=0;j<N;j++){
                ndp[j] += dp[j];
                ndp[j+1] += dp[j]*uf.size(p);
            }
            one += uf.size(p)-1;
            dp.swap(ndp);
        }
    }
    mint ans = ((mint) N-1).pow(K)*N;
    for(int i=1;i<=K;i++){
        mint val = (i==1? one:dp[i]);
        int k = K-i;
        ans -= val*((mint)(N-1)).pow(k)*fact[i-1];
    }
    ans -= ((mint) N-1).pow(K)*c;
    cout << ans << "\n";
}

Measured memory_bytes: 3792