There are N towns numbered 1, 2, \cdots, N.

Some roads are planned to be built so that each of them connects two distinct towns bidirectionally. Currently, there are no roads connecting towns.

In the planning of construction, each town chooses one town different from itself and requests the following: roads are built so that the chosen town is reachable from itself using one or more roads.

These requests from the towns are represented by an array P_1, P_2, \cdots, P_N. If P_i = -1, it means that Town i has not chosen the request; if 1 \leq P_i \leq N, it means that Town i has chosen Town P_i.

Let K be the number of towns i such that P_i = -1. There are (N-1)^K ways in which the towns can make the requests. For each way to make requests, find the minimum number of roads needed to meet all the requests, and print the sum of those (N-1)^K numbers, modulo (10^9+7).

Constraints

* 2 \leq N \leq 5000
* P_i = -1 or 1 \leq P_i \leq N.
* P_i \neq i
* All values in input are integers.

Input

Input is given from Standard Input in the following format:


N
P_1 P_2 \cdots P_N


Output

For each way to make requests, find the minimum number of roads needed to meet all the requests, and print the sum of those (N-1)^K numbers, modulo (10^9+7).

Examples

Input

4
2 1 -1 3


Output

8


Input

2
2 1


Output

1


Input

10
2 6 9 -1 6 9 -1 -1 -1 -1


Output

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

typedef long long ll;
typedef long double ld;
#define all(x) (x).begin(), (x).end()

template<class T, class TL, class TR>
bool inside(T x, TL l, TR r) {
  return x >= l && x <= r;
}

template<class T, class U> 
bool uin(T& x, U y) {
  if (y < x) {
    x = y;
    return true;
  }
  return false;
}

template<class T, class U>
bool uax(T& x, U y) {
  if (y > x) {
    x = y;
    return true;
  }
  return false;
}

void unsync_io() {
  ios::sync_with_stdio(false);
  cin.tie(0);
}

const int N = (int) (5e3 + 7);
const int mod = (int) (1e9 + 7);

int bin(int x, int y) {
  int res = 1;
  while (y != 0) {
    if (y & 1)
      res = (ll) res * x % mod;
    x = (ll) x * x % mod;
    y >>= 1;
  }
  return res;
}

vector<int> g[N];
int col[N];
bool used[N];
int dp[N][N];

void dfs(int u, int color) {
  col[u] = color;
  used[u] = true;
  for (int v : g[u])
    if (!used[v])
      dfs(v, color);
}

void solve() {
  int n;
  cin >> n;
  vector<int> p(n);
  int bad = 0;
  for (int i = 0; i < n; i++) {
    cin >> p[i];
    if (p[i] != -1) {
      p[i]--;
      g[i].push_back(p[i]);
      g[p[i]].push_back(i);
    } else
      bad++;
  }
  int cnt = 0;
  for (int i = 0; i < n; i++)
    if (!used[i])
      dfs(i, cnt++);
  vector<int> sz(cnt, 0);
  vector<bool> good(cnt, false);
  for (int i = 0; i < n; i++) {
    sz[col[i]]++;
    if (p[i] == -1)
      good[col[i]] = true;
  }
  int total = bin(n - 1, bad);
  int ans = (ll) n * total % mod;
  vector<int> good_sz;
  for (int i = 0; i < cnt; i++) {
    if (good[i])
      good_sz.push_back(sz[i]);
    else {
      ans -= total;
      if (ans < 0)
        ans += mod;
    }
  }
  int k = good_sz.size();
  dp[0][0] = 1;
  for (int i = 0; i < k; i++) {
    for (int j = 0; j <= k; j++) {
      dp[i + 1][j] += dp[i][j];
      if (dp[i + 1][j] >= mod)
        dp[i + 1][j] -= mod;
      if (j != k) {
        dp[i + 1][j + 1] += (ll) dp[i][j] * good_sz[i] % mod;
        if (dp[i + 1][j + 1] >= mod)
          dp[i + 1][j + 1] -= mod;
      }
    }
  }
  for (int i = 0; i < k; i++) {
    ans -= (ll) (good_sz[i] - 1) * bin(n - 1, bad - 1) % mod;
    if (ans < 0)
      ans += mod;
  }
  int cur = 1;
  for (int len = 2; len <= k; len++) {
    cur = (ll) cur * (len - 1) % mod;
    int delta = (ll) dp[k][len] * bin(n - 1, bad - len) % mod * cur % mod;
    ans -= delta;
    if (ans < 0)
      ans += mod;
  }
  cout << ans << '\n';
}

void run() {
  solve();
}

int main() {
  unsync_io();
  auto start = chrono::high_resolution_clock::now();
  run();
  auto end = chrono::high_resolution_clock::now();
  cerr << "Execution time: " << chrono::duration_cast<chrono::milliseconds>(end - start).count() << '\n';
  return 0;
}
