#include <bits/stdc++.h>

using namespace std;

const long long MOD = (long long) 1e9 + 7;

class Dsu {
public:
  int n;
  vector<int> par, rnk;

  Dsu (int _n) : n(_n) {
    par = rnk = vector<int> (n, 0);
    iota(par.begin(), par.end(), 0);
  }

  int getPar (int a) {
    return par[a] == a ? a : par[a] = getPar(par[a]);
  }

  void unite (int a, int b) {
    int x = getPar(a), y = getPar(b);
    if (x == y) return;
    if (rnk[x] > rnk[y]) swap(x, y);
    par[x] = y;
    if (rnk[x] == rnk[y]) rnk[y]++;
  }
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  Dsu dsu(n);
  vector<int> bad(n, 0);
  int tot = 0;
  for (int i = 0; i < n; i++) {
    int num;
    cin >> num;
    if (num == -1) {
      bad[i] = 1;
      tot++;
    } else {
      num--;
      dsu.unite(i, num);
    }
  }
  vector<vector<int>> parts(n);
  for (int i = 0; i < n; i++) {
    int x = dsu.getPar(i);
    parts[x].push_back(i);
  }
  vector<pair<int, int>> dat;
  vector<int> cycs;
  cycs.reserve(n);
  dat.reserve(n);
  int mnn = 0;
  for (int i = 0; i < n; i++) {
    int sz = parts[i].size();
    if (!sz) continue;
    int cur = 0;
    for (int pos : parts[i]) {
      if (bad[pos]) cur++;
    }
    dat.emplace_back(sz, cur);
    if (cur) cycs.push_back(sz);
    mnn += sz - 1;
  }
  int sz = dat.size();
  vector<long long> pows(tot + 1);
  pows[0] = 1;
  for (int i = 1; i <= tot; i++) {
    pows[i] = (pows[i - 1] * (n - 1)) % MOD;
  }
  long long ans = (pows[tot] * mnn) % MOD;
  for (int i = 0; i < sz; i++) {
    for (int j = 0; j < sz; j++) {
      if (i == j) continue;
      if (dat[i].second)
        (ans += (pows[tot - 1] * dat[j].first) % MOD) %= MOD;
    }
  }
  sz = cycs.size();
  vector<vector<long long>> dp(sz + 1, vector<long long> (sz + 1, 0));
  dp[0][0] = 1;
  for (int i = 0; i < sz; i++) {
    for (int j = 0; j <= sz; j++) {
      (dp[i + 1][j] += dp[i][j]) %= MOD;
      if (j + 1 <= sz) (dp[i + 1][j + 1] += (dp[i][j] * cycs[i]) % MOD) %= MOD;
    }
  }
  long long fact = 1;
  for (int i = 2; i <= sz; i++) {
    ans -= (pows[tot - i] * ((fact * dp[sz][i]) % MOD)) % MOD;
    if (ans < 0) ans += MOD;
    (fact *= i) %= MOD;
  }
  cout << ans << '\n';
  return 0;
}
