#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
#define REP(i,n) for (ll i = 0; i < (n); ++i)
#define RREP(i, n) for (ll i = (n) - 1; i >= 0; --i)
#define ALL(v) (v).begin(), (v).end()

struct mint{
    static const long long MOD = 1000000007;

    long long v;

    mint() : v(0) {}
    mint(long long x){
        x = x % MOD;
        if(x < 0){ x += MOD; }
        v = x;
    }
    mint& operator+=(mint a){
        v += a.v;
        if(v >= MOD){ v -= MOD; }
        return *this;
    }
    mint& operator*=(mint a){
        v *= a.v;
        v %= MOD;
        return *this;
    }
    mint& operator-=(mint a){
        v -= a.v;
        if(v < 0){ v += MOD; }
        return *this;
    }
    mint& operator/=(mint a){
        return (*this) *= a.inv();
    }

    mint operator*(mint a) const{
        return mint(v) *= a;
    }

    mint operator+(mint a) const {
        return mint(v) += a;
    }

    mint operator-(mint a) const {
        return mint(v) -= a;
    }

    mint operator/(mint a) const {
        return mint(v) /= a;
    }

    mint pow(long long k) const {
        mint res(1),tmp(v);
        while(k){
            if(k&1) res *= tmp;
            tmp *= tmp;
            k >>= 1;
        }
        return res;
    }
    mint inv(){return pow(MOD - 2); }

    static mint comb(long long n, int k){
        mint res(1);
        for(int i = 0; i < k; ++i){
            res *= mint(n - i);
            res /= mint(i + 1);
        }
        return res;
    }

    static mint factorial(long long n){
        mint res(1);
        for(int i = n; i > 1; --i){
            res *= mint(i);
        }
        return res;
    }

    bool operator<(const mint &a) const {
        return v < a.v;
    };
};

mint operator*(long long l, mint r){ return r * l; }

std::ostream& operator<<(std::ostream& stream, const mint& m){
    stream << m.v;
    return stream;
}

int main(){
    ll n;
    cin >> n;
    vector<ll> p(n);
    ll k = 0;
    REP(i, n){
        cin >> p.at(i);
        if(p.at(i) != -1){
            p.at(i)--;
        }else{
            k++;
        }
    }

    vector<vector<ll>> net(n);
    REP(i, n){
        if(p.at(i) == -1){ continue; }
        ll j = p.at(i);
        net.at(i).push_back(j);
        net.at(j).push_back(i);
    }

    vector<bool> visited(n, false);
    vector<ll> csize0, csize1;
    REP(i, n){
        if(visited.at(i)){ continue; }
        visited.at(i) = true;
        ll cur_size = 1;
        bool cur_neg = (p.at(i) == -1);
        queue<ll> que;
        que.push(i);
        while(!que.empty()){
            ll cur = que.front(); que.pop();
            for(ll nxt : net.at(cur)){
                if(visited.at(nxt)){ continue; }
                visited.at(nxt) = true;
                cur_size++;
                cur_neg = cur_neg | (p.at(nxt) == -1);
                que.push(nxt);
            }
        }
        if(cur_neg){
            csize1.push_back(cur_size);
        }else{
            csize0.push_back(cur_size);
        }
    }
    assert(csize1.size() == k);

    vector<mint> pows(k + 1), fact(k + 1);
    pows.at(0) = 1;
    fact.at(0) = 1;
    for(ll i = 1; i <= k; ++i){
        pows.at(i) = (n - 1) * pows.at(i - 1);
        fact.at(i) = i * fact.at(i - 1);
    }

    mint ans = n * pows.at(k);
    ans -= csize0.size() * pows.at(k);
    if(k > 0){
        // 自身が属する連結成分の中で完結する
        REP(j, k){
            // cerr << "csize1[" << j << "]=" << csize1.at(j) << endl;
            ans -= (csize1.at(j) - 1) * pows.at(k - 1);
        }

        vector<vector<mint>> dp(k, vector<mint>(k, 0));
        REP(j, k){
            dp.at(0).at(j) = csize1.at(j);
            if(j > 0){
                dp.at(0).at(j) += dp.at(0).at(j - 1);
            }
        }

        for(ll i = 1; i < k; ++i){
            for(ll j = 1; j < k; ++j){
                dp.at(i).at(j) += dp.at(i - 1).at(j - 1) * csize1.at(j) + dp.at(i).at(j - 1);
            }
        }

        for(ll i = 1; i < k; ++i){
            // cerr << "dp[" << i + 1 << "][" << k << "]=" << dp.at(i).at(k - 1) << endl;
            ans -= fact.at(i) * pows.at(k - (i + 1)) * dp.at(i).at(k - 1);
        }
    }
    cout << ans << endl;

    return 0;
}


Measured memory_bytes: 199056