#include <bits/stdc++.h>
#include <iomanip>
using namespace std;
#define reps(i,s,n) for(int i = s; i < n; i++)
#define rep(i,n) reps(i,0,n)
#define Rreps(i,n,e) for(int i = n - 1; i >= e; --i)
#define Rrep(i,n) Rreps(i,n,0)
#define ALL(a) a.begin(), a.end()
#define fi first
#define se second
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;

ll N,M,H,W,Q,K,A,B;
string S;
const ll MOD = 998244353;
//const ll MOD = (1e+9) + 7;
typedef pair<ll, ll> P;
const ll INF = (1LL<<62);
class mint{
    public:
        ll x;
        static unsigned long long mod;
        static unsigned long long mod_plus;
        mint(){x = 0;}
        mint(ll _x) : x((_x < 0 ? ((_x += mod_plus) < 0 ? _x + mod_plus : _x) : _x)%mod){}
        /*mint& operator=(const ll &a){
            x = a;
            return *this;
        }*/

        mint operator-(){
            return x == 0 ? 0 : mod - x;
        }
        mint& operator+=(const mint& a){
            if((x += a.x) >= mod) x -= mod;
            return *this;
        }
        mint operator+(const mint& a) const{
            mint res(*this);
            return res += a;
        }
        mint& operator-=(const mint& a){
            if((x -= a.x) < 0) x += mod;
            return *this;
        }
        mint operator-(const mint& a) const{
            mint res(*this);
            return res -= a;
        }
        mint& operator*=(const mint& a){
            (x *= a.x)%=mod;
            return *this;
        }
        /*mint& operator*=(const ll& a){
            mint temp(a);
            return *this *= temp;
        }*/
        mint operator*(const mint& a) const{
            mint res(*this);
            return res *= a;
        }
        /*mint operator*(const ll& a) const{
            mint res(*this);
            return res *= a;
        }*/
        mint pow(unsigned long long pw) const{
            mint res(1), comp(*this);
            while(pw){
                if(pw&1) res *= comp;
                comp *= comp;
                pw >>= 1;
            }
            return res;
        }
        //以下、modが素数のときのみ
        mint inv() const{
            mint res(*this);
            return res.pow(mod - 2);
        }
        mint& operator/=(const mint &a){
            (x *= a.inv().x)%=mod;
            return *this;
        }
        mint operator/(const mint &a) const{
            mint res(*this);
            return res /= a;
        }
};
ostream& operator<<(ostream& os, const mint& a){
    os << a.x;
    return os;
}
unsigned long long mint::mod = int(1e+9) + 7;
unsigned long long mint::mod_plus = (LLONG_MAX / mint::mod) * mint::mod;
typedef vector<mint> vm;
const ll MAX_N = ll(4e+5) + 10;
vm fact(MAX_N, mint(1));
void makefact(){
    reps(i,2,MAX_N) fact[i] = fact[i-1] * mint(i);
}
mint nCm(ll n, ll m){
    return fact[n] / (fact[n-m] * fact[m]);
}
int main() {
    makefact();
    class union_find{
            vector<long> parent, tree_size;
        public:
            union_find(unsigned long _n) : parent(_n), tree_size(_n, 1){
                for(unsigned long i = 0; i < _n; ++i)parent[i] = i;
            }
            ll find(ll x){
                while(parent[x] != x)x = parent[x] = parent[parent[x]];
                return x;
            }
            void merge(ll a, ll b){
                a = find(a);
                b = find(b);
    	    if(a==b) return;
                if(tree_size[a] < tree_size[b])swap(a, b);
                tree_size[a] += tree_size[b];
                parent[b] = a;
                return;
            }
    	bool same(ll a, ll b){
    	    a = find(a);
    	    b = find(b);
    	    return a == b;
    	}
    	ll sz(ll a){return tree_size[a];}
        };
    //uf.mergeのように使う
    //ufはMAX_N+1でとるか、代入時に0-indexedにする
    cin>>N;
    vec p(N);
    vector<bool> minus(N, false);
    rep(i,N) {
        cin>>p[i];
        --p[i];
        minus[i] = (p[i] == -2);
    }
    mint res(1);
    res = mint(N - 1).pow(accumulate(ALL(minus), 0LL));
    union_find uf(N);
    ll already = 0;
    rep(i,N){
        if(p[i] >= 0){
            bool temp = minus[uf.find(i)] || minus[uf.find(p[i])];
            if(uf.same(i, p[i])) ++already;
            else uf.merge(i, p[i]);
            minus[uf.find(i)] = temp;
        }
    }
    res *= N - already;
    vec a(0);
    rep(i,N){
        if(i == uf.find(i) && minus[i]) a.emplace_back(uf.sz(i));
    }
    M = a.size();
    vector<vm> dp(M + 1, vm(M + 1, 0));
    dp[0][0] = 1;
    rep(i,M){
        rep(j,M){
            dp[i+1][j] += dp[i][j];
            dp[i+1][j+1] += dp[i][j] * a[i];
        }
    }
    mint N_1(N - 1);
    if(M >= 1) res -= (dp[M][1] - M) * N_1.pow(M - 1);
    reps(i,2,M + 1) res -= dp[M][i] * fact[i - 1] *N_1.pow(M - i);
    cout<<res<<endl;
}

Measured memory_bytes: 201884