#include <bits/stdc++.h>

using namespace std;


#ifdef zxc

#include <sys/resource.h>
#include "debug.h"

#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

using ll = int64_t;
using ld = long double;
const ld EPS = 1e-9;
//const ll MOD = 924844033;
const ld PI = 3.141592653589793;
const int maxn = 300001;
const ll MOD = 1e9 + 7;

void fast_io() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
}

signed main() {
#ifdef zxc
    struct rlimit rl;
    const rlim_t kStackSize = 512L * 1024L * 1024L;
    assert(!getrlimit(RLIMIT_STACK, &rl));
    rl.rlim_cur = kStackSize;
    assert(!setrlimit(RLIMIT_STACK, &rl));
    freopen("../kolya_input.txt", "r", stdin);
//    freopen("../kolya_output.txt", "w", stdout);
#else
    //    mt19937 mt(std::chrono::high_resolution_clock::now().time_since_epoch().count());
    //            freopen("", "r", stdin);
    //        freopen("hack.out", "w", stdout);
#endif


    function<void(int)> solve = [](int t) {
        int n;
        cin >> n;
        vector<int> p(n + 1);
        vector<vector<int>> inv_p(n + 1);
        vector<vector<int>> g(n + 1);

        for (int i = 0; i < n; ++i) {
            cin >> p[i + 1];
        }
        for (int i = 1; i <= n; ++i) {
            if (p[i] + 1) {
                inv_p[p[i]].push_back(i);
                g[p[i]].push_back(i);
                g[i].push_back(p[i]);
            }

        }
        vector<int> used(n + 1);
        vector<int> cmp;
        vector<int> d(n + 1);

        function<void(int)> dfs = [&](int v) {
            used[v] = 1;
            cmp.push_back(v);
            for (int to:g[v]) {
                if (!used[to]) {
                    dfs(to);
                }
            }
        };

        function<void(int)> bfs = [&](int root) {
            queue<int> q;
            q.push(root);

            fill(d.begin(), d.end(), 0);
            d[root] = 1;
            while (!q.empty()) {
                int v = q.front();
                q.pop();
                for (int to:inv_p[v]) {
                    d[to] = d[v] + 1;
                    q.push(to);
                }
            }
        };
        int cycles_cnt = 0;
        int non_cycles_cnt = 0;
        vector<int> dp(n + 1);
        dp[0] = 1;
        vector<int> ndp(n + 1);
        vector<int> cnt(n + 1);
        ll ans = 0;
        for (int i = 1; i <= n; ++i) {
            if (!used[i]) {
                cmp.clear();
                dfs(i);
                debug(cmp);
                auto it = find_if(cmp.begin(), cmp.end(), [&](int v) { return p[v] == -1; });
                if (it == cmp.end()) { // cycle
                    ++cycles_cnt;
                    continue;
                }
                ++non_cycles_cnt;

                copy(dp.begin(), dp.end(), ndp.begin());
                for (int was_cnt = 0; was_cnt < n; ++was_cnt) {
                    auto &res = ndp[was_cnt + 1];
                    res += dp[was_cnt] * cmp.size() % MOD;
                    if (res >= MOD) {
                        res -= MOD;
                    }
                }

                dp.swap(ndp);
                debug(dp);
            }
        }
        vector<ll> fac(n + 1, 1);
        for (int i = 1; i <= n; ++i) {
            fac[i] = fac[i - 1] * i % MOD;
        }

        vector<ll> pw(n + 1, 1);
        for (int i = 1; i <= n; ++i) {
            pw[i] = pw[i - 1] * (n - 1) % MOD;
        }
        for (int i = 1; i <= non_cycles_cnt; ++i) {
            ans -= dp[i] * fac[i - 1] % MOD * pw[non_cycles_cnt - i] % MOD;
        }
        ((ans %= MOD) += MOD) %= MOD;
        ll init = n - cycles_cnt;
        for (int i = 1; i <= n; ++i) {
            if (p[i] == -1) {
                init *= (n - 1);
                init %= MOD;
                ans += pw[non_cycles_cnt - 1];
            }
        }
        ans = init + ans;
        ((ans %= MOD) += MOD) %= MOD;
        cout << ans;
    };


    fast_io();
    cout.precision(9);
    cout << fixed;
    auto start = chrono::steady_clock::now();
//    int t;
//    cin >> t;
    for (int i = 1; i <= 1; ++i)solve(i);
    auto end = chrono::steady_clock::now();

    debug("time", chrono::duration_cast<chrono::milliseconds>(end - start).count() / 1000.0);

}


Measured memory_bytes: 4316