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

#define all(x) (x).begin(), (x).end()
#define sz(x) static_cast<int>((x).size())

using LL = long long;
template<class T, size_t D>
struct vec : vector<vec<T, D - 1>> {
  template<class... Args>
  vec(size_t n = 0, Args... args)
    : vector<vec<T, D - 1>>(n, vec<T, D - 1>(args...)) {}
};
template<class T>
struct vec<T, 1> : vector<T> {
  template<class... Args>
  vec(Args... args)
    : vector<T>(args...) {}
};

template<class T>
inline bool asMn(T& a, const T& b) { return a > b ? a = b, true : false; }
template<class T>
inline bool asMx(T& a, const T& b) { return a < b ? a = b, true : false; }
inline int nex(int i, int n) { return i == n - 1 ? 0 : i + 1; }
inline int pre(int i, int n) { return !i ? n - 1 : i - 1; }

mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count());

template<int MOD>
struct ModInt {
  int a;
  ModInt(int a_ = 0) {
    a = a_ < MOD ? (a_ < 0 ? a_ + MOD : a_) : a_ - MOD;
  }
  friend ostream& operator<<(ostream& os, const ModInt& o) {
    os << o.a;
    return os;
  }

  ModInt operator+(const ModInt& o) const { return ModInt(a + o.a); }
  ModInt operator-(const ModInt& o) const { return ModInt(a - o.a); }
  ModInt operator*(const ModInt& o) const { return ModInt(static_cast<int>(static_cast<LL>(a) * o.a % MOD)); }
  ModInt operator/(const ModInt& o) const { return *this * o.inv(); }
  ModInt& operator+=(const ModInt& o) { return *this = *this + o; }
  ModInt& operator-=(const ModInt& o) { return *this = *this - o; }
  ModInt& operator*=(const ModInt& o) { return *this = *this * o; }
  ModInt& operator/=(const ModInt& o) { return *this = *this / o; }
  ModInt inv() const { return pow(MOD - 2); }
  template<class T>
  ModInt pow(T exp) const {
    ModInt ret(1), c = *this;
    for (; exp; exp >>= 1, c *= c) {
      if (exp & 1) {
        ret *= c;
      }
    }
    return ret;
  }
};
using Mint = ModInt<static_cast<int>(1e9 + 7)>;

struct DSU {
  int nSet;
  vec<int, 1> par, car;
  DSU(int nSet_)
    : nSet(nSet_) {
    par = vec<int, 1>(nSet); iota(all(par), 0);
    car = vec<int, 1>(nSet, 1);
  }

  int findPar(int i) {
    return i == par[i] ? i : par[i] = findPar(par[i]);
  }
  void unite(int i, int j) {
    i = findPar(i); j = findPar(j);
    if (i == j) {
      return ;
    }
    if (car[i] > car[j]) {
      swap(i, j);
    }
    --nSet;
    par[i] = j;
    car[j] += car[i];
  }
};

int main() {
  ios_base::sync_with_stdio(0); cin.tie(0);

  int n; cin >> n;
  vec<int, 1> p(n);
  DSU dsu(n);
  for (int i = 0; i < n; ++i) {
    cin >> p[i];
    if (~p[i]) {
      --p[i];
      dsu.unite(i, p[i]);
    }
  }

  int k = static_cast<int>(count(all(p), -1));

  Mint ans = Mint(n) * Mint(n - 1).pow(k);
  vec<bool, 1> marked(n);
  for (int i = 0; i < n; ++i) {
    if (!~p[i]) {
      marked[dsu.findPar(i)] = true;
    }
  }
  vec<Mint, 1> f(k + 1); f[0] = Mint(1);
  for (int i = 0; i < n; ++i) {
    if (dsu.findPar(i) == i) {
      if (!marked[i]) {
        ans -= Mint(n - 1).pow(k);
      } else {
        ans -= Mint(dsu.car[i] - 1) * Mint(n - 1).pow(k - 1);
        for (int j = k; j; --j) {
          f[j] += f[j - 1] * Mint(dsu.car[i]);
        }
      }
    }
  }
  Mint fact(1);
  for (int i = 2; i <= k; fact *= Mint(i), ++i) {
    ans -= f[i] * Mint(n - 1).pow(k - i) * fact;
  }

  cout << ans << '\n';

  return 0;
}

Measured memory_bytes: 3684