#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <random>
#include <iomanip>
#include <functional>
#include <cassert>

using namespace std;

typedef long long ll;

const int md = 1e9 + 7;

int mul(int a, int b) {
  return ((ll)a * b) % md;
}

void add(int &a, int b) {
  a += b;
  if (a >= md) a -= md;
}

int sub(int a, int b) {
  a -= b;
  if (a < 0) a += md;
  return a;
}

int power(int a, ll b) {
  int res = 1;
  while (b > 0) {
    if (b & 1) res = mul(res, a);
    a = mul(a, a);
    b >>= 1;
  }
  return res;
}

int main() {
  ios_base::sync_with_stdio(false); cin.tie(0);
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
#endif
  int n;
  cin >> n;
  vector <int> fac(n + 1);
  fac[0] = 1;
  for (int i = 1; i <= n; ++i) fac[i] = mul(fac[i - 1], i);
  vector <int> p(n);
  for (int i = 0; i < n; ++i) {
    cin >> p[i];
    if (p[i] != -1) --p[i];
  }
  vector <int> par(n);
  vector <int> sz(n, 1);
  for (int i = 0; i < n; ++i) par[i] = i;
  function <int(int)> get = [&] (int a) {
    return (a == par[a] ? a : par[a] = get(par[a]));
  };

  auto join = [&] (int a, int b) {
    a = get(a);
    b = get(b);
    if (a != b) {
      par[a] = b;
      sz[b] += sz[a];
    }
  };

  for (int i = 0; i < n; ++i) {
    if (p[i] != -1) {
      join(i, p[i]);
    }
  }

  int cmp = 0, fr = 0;
  vector <int> szs;
  for (int i = 0; i < n; ++i) {
    if (p[i] == -1) {
      ++fr;
      szs.push_back(sz[get(i)]);
    }
    if (get(i) == i) {
      ++cmp;
    }
  }
  int ans = mul(sub(cmp, fr), power(n - 1, fr));

  vector <int> coef(fr + 1);
  coef[0] = 1;
  for (int i = 0; i < (int)szs.size(); ++i) {
    for (int j = fr; j > 0; --j) {
      add(coef[j], mul(coef[j - 1], szs[i]));
    }
  } 
  for (int k = 2; k <= fr; ++k) {
    add(ans, mul(coef[k], mul(power(n - 1, fr - k), fac[k - 1])));  
  }
  for (int s : szs) {
    add(ans, mul(s - 1, power(n - 1, fr - 1)));
  }
  ans = md - ans;
  add(ans, mul(n, power(n - 1, fr)));
  cout << ans << '\n';
}


