#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#ifdef ENABLE_DEBUG
#define dump(a) cerr<<#a<<"="<<a<<endl
#define dumparr(a,n) cerr<<#a<<"["<<n<<"]="<<a[n]<<endl
#else
#define dump(a) 
#define dumparr(a,n) 
#endif
#define FOR(i, a, b) for(ll i = (ll)a;i < (ll)b;i++)
#define For(i, a) FOR(i, 0, a)
#define REV(i, a, b) for(ll i = (ll)b-1LL;i >= (ll)a;i--)
#define Rev(i, a) REV(i, 0, a)
#define REP(a) For(i, a)
#define SIGN(a) (a==0?0:(a>0?1:-1))

typedef long long int ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef pair<ll, ll> pll;
typedef pair<ll,pll> ppll;
typedef vector<ll> vll;
typedef long double ld;
typedef pair<ld,ld> pdd;

const ll INF=(1LL<<60);
#if __cplusplus<201700L
ll gcd(ll a, ll b) {
  a=abs(a);
  b=abs(b);
  if(a==0)return b;
  if(b==0)return a;
  if(a < b) return gcd(b, a);
  ll r;
  while ((r=a%b)) {
    a = b;
    b = r;
  }
  return b;
}
#endif
template<class T>
bool chmax(T& a,const T& b){
  if(a<b){
    a=b;
    return true;
  }
  return false;
}
template<class T>
bool chmin(T& a,const T& b){
  if(a>b){
    a=b;
    return true;
  }
  return false;
}
template<class S,class T>
std::ostream& operator<<(std::ostream& os,pair<S,T> a){
  os << "(" << a.first << "," << a.second << ")";
  return os;
}
template<class T>
std::ostream& operator<<(std::ostream& os,vector<T> a){
  os << "[ ";
  REP(a.size()){
    os<< a[i] << " ";
  }
  os<< " ]";
  return os;
}


const long long MOD = 1000000007;
class UF{
  vector<long> par,rank,size_uf;
  long _chunk_size;
public:
  UF(long size):_chunk_size(size){
    for (long i = 0; i < size; i++) {
      par.push_back(i);
      rank.push_back(0);
      size_uf.push_back(1);
    }
  }
  long find(long x){
    if(par[x]==x)return x;
    return par[x]=find(par[x]);
  }
  void unite(long x,long y){
    x=find(x);
    y=find(y);
    if(x==y)return;
    --_chunk_size;
    if(rank[x]<rank[y]){
      par[x]=y;
      size_uf[y]+=size_uf[x];
    }
    else{
      par[y]=x;
      size_uf[x]+=size_uf[y];
      if(rank[x]==rank[y])rank[x]++;
    }
  }
  bool same(long x,long y){
    return find(x)==find(y);
  }
  long operator [](long x){
    return this->find(x);
  }
  long size(long x){
    x=find(x);
    return size_uf[x];
  }
  long chunk_size(){
    return this->_chunk_size;
  }
};
long long modpow(long long a,unsigned long long b){
  long long base=a,ans=(1LL);
  while(b){
    if(b&1){
      ans=ans*base%MOD;
    }
    base=base*base%MOD;
    b>>=1;
  }
  return ans;
}
long long inverse(long long a,unsigned long long MOD){
  return modpow(a,MOD-2);
}
template<long long m>
class Modulo{
  long long a;
public:
  Modulo(const long long b){
    a=(b%m+m)%m;
  }
  Modulo(const Modulo& b){
    a=b.a;
  }
  Modulo &operator = (const long long b){
    a=(b%m+m)%m;
    return *this;
  }
  Modulo &operator = (const Modulo& b){
    a=b.a;
    return *this;
  }
  Modulo operator + (const Modulo<m> &b)const{
    return {(a+b.a)%m};
  }
  Modulo operator - (const Modulo<m> &b)const{
    return {(a-b.a+m)%m};
  }
  Modulo operator * (const Modulo<m> &b)const{
    return {(a*b.a)%m};
  }
  Modulo operator / (const Modulo<m> &b)const{
    return {(a*inverse(b,m))%m};
  }
  Modulo operator + (const long long b)const{
    return {(a+b)%m};
  }
  Modulo operator - (const long long b)const{
    return {((a-b)%m+m)%m};
  }
  Modulo operator * (const long long b)const{
    return {(a*b)%m};
  }
  Modulo operator / (const long long b)const{
    return {(a*inverse(b%m,m))%m};
  }
  Modulo operator += (const Modulo<m> &b)const{
    this->a=(a+b.a)%m;
    return {(a+b.a)%m};
  }
  Modulo operator -= (const Modulo<m> &b)const{
    this->a=(a-b.a+m)%m;
    return {(a-b.a+m)%m};
  }
  Modulo operator *= (const Modulo<m> &b)const{
    this->a=(a*b.a)%m;
    return {(a*b.a)%m};
  }
  Modulo operator /= (const Modulo<m> &b)const{
    this->a=(a*inverse(b,m))%m;
    return {(a*inverse(b,m))%m};
  }
  Modulo operator += (const long long b)const{
    this->a=(a+b)%m;
    return {(a+b)%m};
  }
  Modulo operator -= (const long long b){
    this->a=((a-b)%m+m)%m;
    return {((a-b)%m+m)%m};
  }
  Modulo operator *= (const long long b)const{
    this->a=(a*b)%m;
    return {(a*b)%m};
  }
  Modulo operator /= (const long long b)const{
    this->a=(a*inverse(b%m,m))%m;
    return {(a*inverse(b%m,m))%m};
  }
  Modulo operator < (const Modulo<m> &b)const{
    return a<b.a;
  }
  Modulo operator > (const Modulo<m> &b)const{
    return a>b.a;
  }
  operator long long() const{return a;}
};
template<long long m>
std::ostream& operator<<(std::ostream& os,Modulo<m> a){
  os<<(long long)(a);
  return os;
}
void solve(long long N, std::vector<long long> P){
  UF uf(N);
  ll pathcnt=0;
  vector<ll> K;
  REP(N){
    if(P[i]!=-1&&uf.same(i,P[i])==false){
      ++pathcnt;
      uf.unite(i,P[i]);
    }
    if(P[i]==-1){
      K.push_back(i);
    }
  }
  Modulo<MOD> ans(modpow(N-1,K.size())*(K.size()+pathcnt)%MOD);
  vector<vector<ll>> dp(K.size()+1,vector<ll>(K.size()+1));
  dump(ans);
  For(i,K.size()+1){
    dp[i][0]=1;
  }
  FOR(i,1,K.size()+1){
    For(j,K.size()){
      dp[i][j+1]=(dp[i-1][j]*uf.size(K[i-1])%MOD+dp[i-1][j+1])%MOD;
    }
  }
  ll fact=1;
  FOR(i,1,K.size()+1){
    ll KK=K.size();
    if(i>1)fact=fact*(i-1)%MOD;
    if(i==1){
      ans-=(dp[KK][i]-KK)*fact%MOD*modpow(N-1,KK-i);
    }else{
      ans-=dp[KK][i]*fact%MOD*modpow(N-1,KK-i);
    }
  }
  cout<<(long long)(ans)<<endl;
}

int main(){
  cout<<setprecision(1000);
    long long N;
    scanf("%lld",&N);
    std::vector<long long> P(N);
    for(int i = 0 ; i < N ; i++){
        scanf("%lld",&P[i]);
        if(P[i]>0)--P[i];
    }
    solve(N, std::move(P));
    return 0;
}


Measured memory_bytes: 198844