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

#define rep(i,m,n) for(int (i)=(int)(m);i<(int)(n);++i)
#define rep2(i,m,n) for(int (i)=(int)(n)-1;i>=(int)(m);--i)
#define REP(i,n) rep(i,0,n)
#define REP2(i,n) rep2(i,0,n)
#define FOR(i,c) for(decltype((c).begin())i=(c).begin();i!=(c).end();++i)
#define all(hoge) (hoge).begin(),(hoge).end()
#define en '\n'
using ll = long long;
using ull = unsigned long long;
template <class T> using vec = vector<T>;
template <class T> using vvec = vector<vec<T>>;
typedef pair<ll, ll> P;
constexpr long long INF = 1LL << 60;
constexpr int INF_INT = 1 << 25;
constexpr long long MOD = (ll) 1e9 + 7;
//constexpr long long MOD = 998244353LL;
using ld=long double;
static const ld pi = 3.141592653589793L;
typedef vector<ll> Array;
typedef vector<Array> Matrix;


template<class T> inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template<class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

struct Edge {
	ll to, rev;
    long double cap;
	Edge(ll _to, long double _cap, ll _rev) {
	to = _to; cap = _cap; rev = _rev;
	}
};
using Edges = vector<Edge>;
using Graph = vector<Edges>;

void add_edge(Graph& G, ll from, ll to, long double cap, bool revFlag, long double revCap) {
	G[from].push_back(Edge(to, cap, (ll)G[to].size()));
	if (revFlag)G[to].push_back(Edge(from, revCap, (ll)G[from].size() - 1));
}

ll mod_pow(ll x, ll n, ll mod) {
 ll res = 1LL;
 while (n > 0) {
  if (n & 1) res = res * x % mod;
  x = x * x % mod;
  n >>= 1;
 }
 return res;
}


class UnionFind {
 vector<int> data;
 int num;
public:
 UnionFind(int size) : data(size, -1), num(size) { }
 bool unionSet(int x, int y) {
  x = root(x); y = root(y);
  if (x != y) {
   if (data[y] < data[x]) swap(x, y);
   data[x] += data[y]; data[y] = x;
   num--;
  }
  return x != y;
 }
 bool findSet(int x, int y) {
  return root(x) == root(y);
 }
 int root(int x) {
  return data[x] < 0 ? x : data[x] = root(data[x]);
 }
 int size(int x) {
  return -data[root(x)];
 }
 int numSet() {
  return num;
 }
};


void solve(){
    ll n;
    cin>>n;
    vec<ll> p(n);
    ll k=0;
    vec<ll> ks;
    UnionFind uni(n);
    REP(i,n){
        cin>>p[i];
        if(p[i]==-1){
            k++;
            ks.push_back(i);
        }else{
            uni.unionSet(i,p[i]-1);
        }
    }

    vec<ll> powk(k+1,1);
    REP(i,k) powk[i+1]=powk[i]*(n-1)%MOD;
    vec<ll> fact(k+1,1);
    REP(i,k) fact[i+1]=fact[i]*(i+1)%MOD;


    ll ans=0;
    ll m=uni.numSet();
    (ans += powk[k]*(n-m))%=MOD;

    vec<ll> ms;
    REP(i,n) if(uni.root(i)==i) ms.push_back(i);

    REP(i,k){
        REP(j,m){
            if(uni.root(ks[i])==ms[j]) continue;//同じ連結成分の時
            //i->jに辺がある時
            (ans+=uni.size(ms[j]) * powk[k-1] %MOD)%=MOD;
        }
    }

    //閉路の場合をとりのぞく
    vvec<ll> dp(k+1,vec<ll>(k+1,0));//i番目まで見て、j個の連結成分を含む閉路
    dp[0][0]=1;
    REP(i,k){
        ll si=uni.size(ks[i]);
        REP(j,k+1){
            if(dp[i][j]==0) continue;
            (dp[i+1][j] += dp[i][j])%=MOD;
            (dp[i+1][j+1] += dp[i][j]*si%MOD)%=MOD;
        }
    }
    rep(i,2,k+1){
        (ans += (MOD - dp[k][i] * fact[i-1]%MOD * powk[k-i] %MOD)%MOD)%=MOD;
    }
    cout<<ans<<en;

}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
	
	solve();
	//ll t;cin>>t;REP(i,t) solve();

    return 0;
}


Measured memory_bytes: 199016