#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
//const int INF = 2e9;
//const ll INF = 9e18;

const int mod = 1000000007;
struct mint {
    ll x; // typedef long long ll;
    mint(ll 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(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 { 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> parent; //parent[i]はiの親の番号
    vector<int> rank; // rank[i]は木の深さ
    vector<int> size; // size[i]はグループに含まれている頂点数
    vector<int> loop; // グループに含まれているloopの数

    UnionFind(int n) { 
        //コンストラクタは全て自分が根
        parent = vector<int>(n);
        rank = vector<int>(n);
        size = vector<int>(n);
        loop = vector<int>(n);
        for (int i=0;i<n;i++) {
            parent[i] = i;
            rank[i] = 0;
            size[i] = 1;
        }
    }

    int root(int x) { //根を再帰で辿りつつ、探索したものを繋ぎなおし
        if (parent[x] == x) return x;
        return parent[x] = root(parent[x]);
    }

    void unite(int x, int y){
        int rx = root(x);
        int ry = root(y);
        if (rx == ry) {
            loop[rx]++;
            return;
        }
        if (rank[rx] < rank[ry]) {
            parent[rx] = ry;
            size[ry] += size[rx];
            loop[ry] += loop[rx];
        }
        else {
            parent[ry] = rx;
            size[rx] += size[ry];
            loop[rx] += loop[ry];
            if (rank[rx] == rank[ry]) rank[rx]++;
        }
    }

    bool same(int x, int y){
        int rx = root(x);
        int ry = root(y);
        return rx == ry;
    }

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

    int getLoopNum(int x){
        return loop[root(x)];
    }
};

int mypow(int p, int n){
    int res = 1;
    while (n>0){
        if (n%2==0){
            p = 1LL*p*p%mod;
            n /= 2;
        } else {
            res = 1LL*res*p%mod;
            n--;
        }
    }
    return res;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int N;
    cin >> N;
    vector<int> P(N);
    for (int i=0;i<N;i++) {
        cin >> P[i];
        if (P[i] != -1) P[i]--;
    }
    UnionFind uf = UnionFind(N);
    for (int i=0;i<N;i++) {
        if (P[i] != -1){
            uf.unite(i,P[i]);
        }
    }
    int loop = 0;
    vector<int> groups;
    for (int i=0;i<N;i++){
        if (i!=uf.root(i)) continue;
        if (uf.getLoopNum(i) > 0) loop += 1;
        else groups.push_back(uf.getSize(i));
    }
    int S = groups.size();
    vector<vector<mint>> dp(S+5,vector<mint>(S+5));
    dp[0][0] = 1;
    for (int i=0;i<S;i++){
        for (int j=0;j<S;j++){
            dp[i][j+1] += dp[i][j];
            dp[i+1][j+1] += dp[i][j]*groups[j];
        }
    }
    vector<mint> fact(S+5);
    fact[0] = 1;
    for (int i=0;i<=S;i++){
        fact[i+1] = fact[i]*(i+1);
    }
    // dp[i][S]を使う
    mint pt = 0;
    for (int i=1;i<=S;i++){
        if (i==1) pt += mint(N-1).pow(S-1)*(dp[1][S]-S);
        else pt += mint(N-1).pow(S-i)*fact[i-1]*dp[i][S];
    }
    pt += mint(N-1).pow(S)*loop;
    mint ans = mint(N-1).pow(S)*N - pt;
    cout << ans << "\n";
}

Measured memory_bytes: 199188