#define _CRT_SECURE_NO_WARNINGS
#pragma comment(linker, "/STACK:256000000")
#define _USE_MATH_DEFINES
#include<iostream>
#include<vector>
#include<string>
#include<stack>
#include<algorithm>
#include<cmath>
#include<set>
#include<queue>
#include<sstream>
#include<utility>
#include<map>
#include<ctime>
#include<cstdio>
#include<cassert>
#include<functional>
#include<unordered_map>
#include<deque>
#include<cmath>





using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef pair<char, char> pcc;
typedef pair<double, double> pdd;

#define show(x) cerr << x
#define debug(x) show(#x << ": " << (x) << endl)

const long double PI = 3.14159265358979323846;
const long double eps = 1e-5;
const ll INF = numeric_limits<int>::max();
const ll LINF = numeric_limits<ll>::max();
const ll mod = 1000 * 1000 * 1000 + 7;

vector<vector<int> > g;
vector<int> cols;

int dfs(int pos, int c) {
    int res = 1;
    cols[pos] = c;
    for (auto to:g[pos]) {
        if (cols[to] == 0)
            res += dfs(to, c);
    }
    return res;

}

void solve() {
    ll n;
    cin >> n;
    g.resize(n);
    vector<int> p(n);
    vector<int> fr(n, 0);
    int k = 0;
    for (int i = 0; i < n; ++i) {
        cin >> p[i];
        if (p[i] == -1) {
            fr[i] = 1;
            ++k;
            continue;
        }
        --p[i];
        g[i].push_back(p[i]);
        g[p[i]].push_back(i);
    }
    cols.assign(n, 0);
    vector<int> sz;
    int c = 1;
    for (int i = 0; i < n; ++i) {
        if (cols[i] == 0) {
            sz.push_back(dfs(i, c));
            ++c;
        }
    }
    /*for (int i = 0; i < n; ++i) {
        cerr << cols[i] << " ";
    }
    cerr << endl;*/
    c -= 1;
    vector<int> used(c, 1);
    for (int i = 0; i < n; ++i) {
        if (fr[i]) {
            used[cols[i] - 1] = 0;
        }
    }
    
    ll res = 0;
    vector<ll> mul(k + 1);
    mul[0] = 1;
    for (int i = 0; i < k; ++i) {
        mul[i + 1] = (mul[i] * (n - 1)) % mod;
    }
    ll act = 0;
    ll sm = 0;
    res = ((n - c) * mul[k]) % mod;
    ll m = 0;
    vector<ll> pr(k + 1, 0);
    pr[0] = 1;
    int id = 0;
    for (int i = 0; i < c; ++i) {
        if (used[i] == 0) {
            res += (mul[k - 1] * (n - sz[i])) % mod;
            res %= mod;
            //m = (m * (n - 1 + sz[i])) % mod;
            for (int j = k + 1; j >= 1; --j) {
                pr[j] = (pr[j] + pr[j - 1] * (sz[i])) % mod;
            }
            ++id;
        }
    }
    ll f = 1;
    for (int i = 2; i <= k; ++i) {
        f = (f * (i - 1)) % mod;
        m += ((pr[i] * mul[k - i]) % mod) * f;
        m %= mod;
    }
    res = (res - m + mod) % mod;
    cout << res << endl;
    /*for (int i = 0; i < c; ++i) {
        ll add = 1;
        for (int j = 0; j < act; ++j) {
            add = (add * (n - 1 - sz[i])) % mod;
        }
        if (used[i] == 0) {
            add = (add * (n - sm - 1)) % mod;
            ++act;
        }
        sm += sz[i];
        add = (add * mul[k - act]) % mod;
        res = (res + add) % mod;
        cerr << res << endl;
    }
    res = (mul[k] * n - res + mod) % mod;
    cout << res << endl;*/
}


//#define LOCAL

int main() {
    ios_base::sync_with_stdio(0);cin.tie(nullptr);cout.tie(nullptr);
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
#endif
    solve();



#ifdef LOCAL
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif	
    return 0;
}


Measured memory_bytes: 3560