#include <bits/stdc++.h>
#include <math.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define PI 3.14159265359
using namespace std;
using ll = long long;
using P = pair<int, int>;

const int mod = 1000000007;
struct mint {
  ll x;
  mint(ll x=0):x(x%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 {
    mint res(*this);
    return res+=a;
  }
  mint operator-(const mint a) const {
    mint res(*this);
    return res-=a;
  }
  mint operator*(const mint a) const {
    mint res(*this);
    return res*=a;
  }
  mint pow(ll t) const {
    if (!t) return 1;
    mint a = pow(t>>1);
    a *= a;
    if (t&1) a *= *this;
    return a;
  }
  
  mint inv() const {
    return pow(mod-2);
  }
  mint& operator/=(const mint a) {
    return (*this) *= a.inv();
  }
  mint operator/(const mint a) const {
    mint res(*this);
    return res/=a;
  }
};

mint dp[5000][5000];

int p[5000];
int par[5000];
int num[5000];
bool tree[5000];

int find(int i) {
  if (par[i] < 0) {return i;}
  else {par[i] = find(par[i]); return par[i];}
}

void merge(int u, int v) {
  int r_u, r_v;
  r_u = find(u); r_v = find(v);
  if (r_u == r_v) return;
  if (par[r_u] < par[r_v]) {
    par[r_u] = min(par[r_u], par[r_v]-1); par[r_v] = r_u;
    num[r_u] += num[r_v];
    if (tree[r_v]) tree[r_u] = true;
  } else {
    par[r_v] = min(par[r_v], par[r_u]-1); par[r_u] = r_v;
    num[r_v] += num[r_u];
    if (tree[r_u]) tree[r_v] = true;
  }
  return;
}

bool same(int u, int v) {
  return find(u) == find(v);
}

int main() {
  int n;
  cin >> n;
  rep(i, n) {
    cin >> p[i]; if (p[i] > 0) p[i]--;
    par[i] = -1;
    num[i] = 1;
    tree[i] = (p[i] == -1 ? true : false);
  }
  rep(i, n) {
    if (p[i] != -1) merge(i, p[i]);
  }
/*
  rep(i, n) {
    cout << par[i] << " " << num[i] << " " << (tree[i] ? 1 : 0) << endl;
  }
*/
  set<int> c, t;

  rep(i, n) {
    int r = find(i);
    if (tree[r]) t.insert(r);
    else c.insert(r);
  }
  set<int>::iterator ite;
/*
  cout << "c" << endl;
  for (ite = c.begin(); ite != c.end(); ite++) cout << *ite << " "; cout << endl;
  cout << "t" << endl;
  for (ite = t.begin(); ite != t.end(); ite++) cout << *ite << " "; cout << endl;
*/
  vector<int> numt;
  for (ite=t.begin(); ite!=t.end(); ite++) {
    numt.push_back(num[*ite]);
  }
  int m = numt.size();
/*
  rep(i, m) cout << numt[i] << " "; cout << endl;
*/
  rep(i, m) rep(j, m) dp[i][j] = 0;
  if (m > 0) dp[0][0] = numt[0] - 1;
  for (int i=1; i<m; i++) dp[i][0] = dp[i-1][0] + mint(numt[i] - 1);
  for (int i=1; i<m; i++) {
    for (int j=1; j<=i; j++) {
      dp[i][j] = dp[i-1][j] + (dp[i-1][j-1] + (j == 1 ? i : 0)) * mint(numt[i]);
    }
  }
/*
  rep(i, m) {rep(j, m) cout << dp[i][j].x << " "; cout << endl;}
*/
  vector<mint> a;
  if (m > 0) a.push_back(mint(n-1).pow(m-1));
  rep(i, m-1) a.push_back((a[i] / mint(n-1)) * mint(i+1));
/*
  rep(i, m) cout << a[i].x << " "; cout << endl;
*/
  mint cc = 0;
  for (int i=0; i<m; i++) cc += dp[m-1][i] * a[i];
  //cout << c.size() << " " << a[0].x * mint(n-1).x << " " << cc.x << endl;
 mint ans = mint(n - c.size()) * mint(n-1).pow(m) - (m > 0 ? cc : 0);
  cout << ans.x << endl;

  return 0;
}

/*
*/


Measured memory_bytes: 199300