// -----------------------------------
// author   : MatsuTaku
// country  : Japan
// created  : 05/30/20 16:02:55
// -----------------------------------

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

template <typename T, T MOD>
class modint {
 private:
  T val_;

 public:
  constexpr modint() : val_(0) {}
  constexpr modint(T v) : val_(v%MOD) {}
  constexpr modint& operator=(T v) { val_ = v%MOD; return *this; }

  constexpr T val() const { return val_; }
  constexpr modint operator+(modint x) const { return (val() + x.val())%MOD; }
  constexpr modint operator+=(modint x) { return *this = *this + x; }
  constexpr modint operator++() { return *this += 1; }
  constexpr modint operator++(int) { modint c = *this; ++(*this); return c; }
  constexpr modint operator-(modint x) const { return (val() + MOD - x.val())%MOD; }
  constexpr modint operator-=(modint x) { return *this = *this - x; }
  constexpr modint operator--() { return *this -= 1; }
  constexpr modint operator--(int) { modint c = *this; --(*this); return c; }
  constexpr modint operator*(modint x) const { return (val() * x.val())%MOD; }
  constexpr modint operator*=(modint x) { return *this = *this * x; }
  friend constexpr modint pow(modint x, T p) {
    modint t = 1;
    modint u = x;
    while (p) {
      if (p & 1)
        t *= u;
      u *= u;
      p >>= 1;
    }
    return t;
  }
  constexpr modint operator/(modint x) const { return *this * pow(modint{x}, MOD-2); }
  constexpr modint operator/=(modint x) { return *this = *this / x; }

  constexpr bool operator==(modint x) const { return val() == x.val(); }
  constexpr bool operator!=(modint x) const { return val() != x.val(); }
  constexpr bool operator<(modint x) const { return val() < x.val(); };
  constexpr bool operator<=(modint x) const { return val() <= x.val(); };
  constexpr bool operator>(modint x) const { return val() > x.val(); };
  constexpr bool operator>=(modint x) const { return val() >= x.val(); };

  friend std::ostream& operator<<(std::ostream& os, const modint& x) {
    return os << x.val();
  }
  friend std::istream& operator>>(std::istream& is, modint& x) {
    return is >> x.val_;
  }

};

class union_find {
 private:
  vector<int> par_, cnt_;

 public:
  explicit union_find(size_t size) : par_(size), cnt_(size, 1) {
    iota(par_.begin(), par_.end(), 0);
  }

  int root(int u) {
    if (u == par_[u])
      return u;
    return par_[u] = root(par_[u]);
  }

  bool are_union(int u, int v) {
    return root(u) == root(v);
  }

  void unite(int u, int v) {
    if (are_union(u,v))
      return;
    if (cnt_[root(u)] < cnt_[root(v)])
      swap(u,v);
    cnt_[root(u)] += cnt_[root(v)];
    par_[root(v)] = par_[root(u)];
  }

  int count(int u) {
    return cnt_[root(u)];
  }

};

using mint = modint<ll, (ll)1e9+7>;

int main() {
  cin.tie(nullptr); ios::sync_with_stdio(false);

  int n; cin>>n;
  vector<ll> P(n);
  for (auto& p:P) {
    cin>>p;
    if (p > 0)
      p--;
  }
  union_find uf(n);
  int cuk = 0;
  for (int i = 0; i < n; i++) {
    if (P[i] == -1) {
      cuk++;
    } else {
      uf.unite(i, P[i]);
    }
  }
  map<int,int> es;
  for (int i = 0; i < n; i++) {
    es[uf.root(i)] += P[i] != -1 ? 1 : 0;
  }

  vector<int> A;
  int closed = 0;
  for (auto [u,e]:es) {
    if (uf.count(u) == e) {
      closed++;
    } else {
      A.push_back(uf.count(u));
    }
  }
  auto ct = A.size();
  vector<mint> dp(ct+1);
  dp[0] = 1;
  for (int i = 0; i < ct; i++) {
    for (int j = i+1; j >= 1; j--) {
      dp[j] += dp[j-1] * A[i];
    }
  }

  array<mint, 5001> fact;
  fact[0] = 1;
  for (int i = 1; i <= 5000; i++)
    fact[i] = fact[i-1]*i;

  mint ans = pow(mint(n-1), cuk) * (n-closed);
  for (auto a:A) {
    ans -= pow(mint(n-1), ct-1) * (a-1);
  }
  for (int i = 2; i <= ct; i++) {
    ans -= pow(mint(n-1), ct-i) * dp[i] * fact[i-1];
  }
  cout << ans << endl;

  return 0;
}


Measured memory_bytes: 4036