#include <bits/stdc++.h>

using namespace std;
using int64 = long long;

constexpr int DEBUG = 0;

// Verified: AOJ-DSL1A
class UFSet {
 private:
  vector<int> ranks;
  vector<int> prevs;

 public:
  UFSet(int n) : ranks(n), prevs(n) {
    for (int i = 0; i < n; i++) prevs[i] = i;
  }

  // Returns a root of a tree which x belongs to.
  int Find(int x) {
    if (x != prevs[x]) prevs[x] = Find(prevs[x]);
    return prevs[x];
  }

  // Merges a set which x blongs to and a set which y belongs to.
  void Union(int x, int y) {
    int x_root = Find(x);
    int y_root = Find(y);
    if (ranks[x_root] > ranks[y_root]) {
      prevs[y_root] = prevs[x_root];
    } else if (ranks[x_root] < ranks[y_root]) {
      prevs[x_root] = prevs[y_root];
    } else if (prevs[x_root] != prevs[y_root]) {
      prevs[y_root] = prevs[x_root];
      ranks[x_root]++;
    }
  }
};

constexpr int64 P = 1000000007;

struct FiniteField {
 private:
  int64 x;
  static inline int64 Normalize(int64 x) {
    if (0 <= x && x < P) return x; x %= P; if (x < 0) x += P; return x;
  }
 public:
  FiniteField(int64 x) : x(Normalize(x)) {}
  FiniteField() : x(0) {}
  int64 Value() const { return x; }
  inline FiniteField operator+(FiniteField o) const {
    FiniteField r(*this); r += o; return r;
  }
  inline FiniteField operator-(FiniteField o) const {
    FiniteField r(*this); r -= o; return r;
  }
  inline FiniteField operator* (FiniteField o) const {
    FiniteField r(*this); r *= o; return r;
  }
  inline FiniteField operator/ (FiniteField o) const {
    FiniteField r(*this); r /= o; return r;
  }
  inline void operator+= (FiniteField o) { x = (x + o.x) % P; }
  inline void operator-= (FiniteField o) { x = (x + P - o.x) % P; }
  inline void operator*= (FiniteField o) { x = (x * o.x) % P; }
  void operator/=(FiniteField o) {
    int64 p = P - 2; while (p) { if (p % 2) { *this *= o; } o *= o; p /= 2; }
  }
};

ostream& operator<<(ostream& s, const FiniteField& v) { s << v.Value(); return s; }

template<typename T> ostream& operator<<(ostream& s, const vector<T>& v) {
  int len = v.size();
  s << "[";
	for (int i = 0; i < len; i++) {
    if (i > 0) s << ", ";
		s << v[i];
  }
  s << "]";
  return s;
}

template<typename T>
vector<vector<T>> MakeVector2D(int d1, int d2, T default_value) {
  return vector<vector<T>>(d1, vector<T>(d2, default_value));
}

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

  int n;
  cin >> n;

  vector<FiniteField> powers(n + 1);
  powers[0] = 1;
  for (int i = 1; i <= n; i++) {
    powers[i] = powers[i - 1] * (n - 1);
  }

  vector<FiniteField> factorials(n + 1);
  factorials[0] = 1;
  for (int i = 1; i <= n; i++) {
    factorials[i] = factorials[i - 1] * i;
  }

  vector<int> xs(n);
  int free_count = 0;
  for (int i = 0; i < n; i++) {
    cin >> xs[i];
    if (xs[i] > 0) xs[i]--;
    
    if (xs[i] < 0) {
      free_count++;
    }
  }

  UFSet uf_set(n);
  for (int i = 0; i < n; i++) {
    if (xs[i] >= 0) uf_set.Union(i, xs[i]);
  }

  vector<int> component_to_size(n);
  vector<int> component_to_edge_count(n);
  for (int i = 0; i < n; i++) {
    component_to_size[uf_set.Find(i)]++;
    if (xs[i] >= 0) component_to_edge_count[uf_set.Find(i)]++;
  }

  vector<int> cs;
  int cycle_count = 0;  // Components with a cycle.
  for (int i = 0; i < n; i++) {
    if (component_to_size[i] == 0) continue;
    if (component_to_size[i] == component_to_edge_count[i]) {
      cycle_count++;
    } else {
      cs.push_back(component_to_size[i]);
    }
  }

  if (DEBUG) cout << "cs: " << cs << endl;
  if (DEBUG) cout << "cycle_count: " << cycle_count << endl;

  auto dp = MakeVector2D(cs.size() + 1, cs.size() + 1, FiniteField(0));
  dp[0][0] = 1;
  for (int i = 0; i < cs.size(); i++) {
    for (int j = 0; j <= i; j++) {
      dp[i + 1][j] += dp[i][j];
      dp[i + 1][j + 1] += dp[i][j] * cs[i];
    }
  }
  if (DEBUG) cout << "dp[cs.size()]: " << dp[cs.size()] << endl;

  FiniteField cycle_patterns = 0;
  for (int i = 1; i <= cs.size(); i++) {
    if (i == 1) {
      cycle_patterns += factorials[i - 1] * powers[free_count - i] * (dp[cs.size()][i] - cs.size());
    } else {
      cycle_patterns += factorials[i - 1] * powers[free_count - i] * dp[cs.size()][i];
    }
  }

  cycle_patterns += powers[free_count] * cycle_count;

  if (DEBUG) cout << "cycle_patterns: " << cycle_patterns << endl;

  FiniteField ans = powers[free_count] * n - cycle_patterns;
  cout << ans << endl;
}

Measured memory_bytes: 198956