#include <iostream>
#include <array>
#include <algorithm>
#include <vector>
#include <bitset>
#include <set>
#include <unordered_set>
#include <cmath>
#include <complex>
#include <deque>
#include <iterator>
#include <numeric>
#include <map>
#include <unordered_map>
#include <queue>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <limits>
#include <iomanip>
#include <functional>
#include <cassert>
using namespace std;

using ll=long long;
template<class T> using V = vector<T>;
template<class T, class U> using P = pair<T, U>;
using vll = V<ll>;
using vvll = V<vll>;
#define rep(i, k, n) for (ll i=k; i<(ll)n; ++i)
#define REP(i, n) rep(i, 0, n)
#define ALL(v) v.begin(),v.end()
template < class T > inline bool chmax(T& a, T b) {if (a < b) { a=b; return true; } return false; }
template < class T > inline bool chmin(T& a, T b) {if (a > b) { a=b; return true; } return false; }
#define DEBUG_VLL(vec) REP(sz, vec.size()) std::cerr<<vec[sz]<<(sz==vec.size()-1?'\n':' ');

const long long MOD = 1000000007;
const long long HIGHINF = (long long)1e18;
const int INF = (int)1e9;

class ModInt {
public:
    long long x;
    constexpr ModInt(const long long x=0) : x((x+MOD)%MOD) {}
    constexpr ModInt& operator+=(const ModInt rhs) {
        x += rhs.x;
        if (x >= MOD) x -= MOD;
        return *this;
    }
    constexpr ModInt operator+(const ModInt rhs) const {
        return ModInt(*this) += rhs; 
    }
    constexpr ModInt& operator-=(const ModInt& rhs) {
        x -= rhs.x;
        if (x < 0) x += MOD;
        return *this;
    }
    constexpr ModInt operator-(const ModInt rhs) const {
        return ModInt(*this) -= rhs; 
    }
    constexpr ModInt& operator*=(const ModInt& rhs) {
        x = x * rhs.x % MOD;
        return *this;
    }
    constexpr ModInt operator*(const ModInt rhs) const {
        return ModInt(*this) *= rhs; 
    }
    constexpr ModInt& operator/=(const ModInt& rhs) {
        ModInt div = powmod(rhs, MOD - 2);
        (x *= div.x) %= MOD;
        return *this;
    }
    constexpr ModInt operator/(const ModInt rhs) const {
        return ModInt(*this) /= rhs;
    }
    constexpr ModInt powmod(ModInt m, long long p) {
        if (p == 0) return ModInt(1);
        ModInt tmp = powmod(m, p / 2);
        if (p & 1) return tmp * tmp * m;
        else return tmp * tmp;
    }
    constexpr ModInt& operator++() {
        x += 1;
        return *this;
    }
    constexpr ModInt operator++(int) {
        ModInt tmp(*this);
        operator++();
        return tmp;
    }
    constexpr ModInt& operator--() {
        x -= 1;
        return *this;
    }
    constexpr ModInt operator--(int) {
        ModInt tmp(*this);
        operator--();
        return tmp;
    }

    friend ostream& operator<<(ostream& os, const ModInt &rhs) {
        os << rhs.x;
        return os;
    }
    friend istream& operator>>(istream& is, ModInt& rhs) {
        is >> rhs.x;
        return is;
    }
};
bool operator==(const ModInt& lhs, const ModInt& rhs) {
    return lhs.x == rhs.x;
}
bool operator!=(const ModInt& lhs, const ModInt& rhs) {
    return !(lhs == rhs);
}
ModInt powmod(ModInt m, long long p) {
    if (p == 0) return ModInt(1);
    ModInt tmp = powmod(m, p / 2);
    if (p & 1) return tmp * tmp * m;
    else return tmp * tmp;
}

using modi = ModInt;

V<int> p;

V< V<int> > edges;
V<int> color;
V<bool> nocyc;
int dfs(int i, int par, int c) {
    int ret = 1;
    color[i] = c;
    if (p[i] == -1) nocyc.emplace_back(true);
    for (int e: edges[i]) {
        if (e == par || color[e] != -1) continue;
        ret += dfs(e, i, c);
    }
    return ret;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n; cin >> n;
    edges.resize(n);
    color.resize(n, -1);
    p.resize(n);
    int k = 0;  // -1 の個数 = 閉路を持たない連結成分の個数
    for (int i = 0; i < n; i++) {
        cin >> p[i]; 
        if (p[i] != -1) {
            p[i]--;
            edges[i].emplace_back(p[i]);
            edges[p[i]].emplace_back(i);
        } else k++;
    }

    int c = 0;  // 連結成分の個数
    V<int> consz;
    for (int i = 0; i < n; i++) {
        if (color[i] == -1) {
            int sz = dfs(i, -1, c);
            c++;
            if (nocyc.size() == c) {
                consz.emplace_back(sz);
            } else nocyc.emplace_back(true);
        }
    }

    modi ans = modi(n) * powmod(modi(n - 1), k);  // 全部の決め方

    V< V<modi> > dp(k + 1, V<modi>(k + 1, modi(0)));
    // dp[i][j]: i 個目まで見た時に j 個選んだときの係数
    for (int j = 0; j <= k; j++) dp[0][j] = modi(1);
    for (int i = 0; i < k; i++) {
        for (int j = 0; j <= i; j++) {
            dp[i + 1][j] += dp[i][j];
            dp[i + 1][j + 1] += dp[i][j] * modi(consz[i]);
        }
    }

    V<modi> fac(k + 1, modi(1));
    for (int i = 2; i <= k; i++) fac[i] = fac[i - 1] * modi(i);

    for (int i = 1; i <= k; i++) {
        if (i == 1) ans -= (dp[k][i] - modi(k)) * fac[i - 1] * powmod(modi(n - 1), k - i);
        else ans -= dp[k][i] * fac[i - 1] * powmod(modi(n - 1), k - i);
    }

    ans -= modi(c - k) * powmod(modi(n - 1), k);
    cout << ans << '\n';
    return 0;
}


Measured memory_bytes: 199068