// Dmitry _kun_ Sayutin (2020)

#include <bits/stdc++.h>

using std::cin;
using std::cout;
using std::cerr;

using std::vector;
using std::map;
using std::array;
using std::set;
using std::string;
using std::queue;

using std::pair;
using std::make_pair;

using std::tuple;
using std::make_tuple;
using std::get;

using std::min;
using std::abs;
using std::max;
using std::swap;

using std::unique;
using std::sort;
using std::generate;
using std::reverse;
using std::min_element;
using std::max_element;

#ifdef LOCAL
#define LASSERT(X) assert(X)
#else
#define LASSERT(X) {}
#endif

template <typename T>
T input() {
    T res;
    cin >> res;
    LASSERT(cin);
    return res;
}

template <typename IT>
void input_seq(IT b, IT e) {
    std::generate(b, e, input<typename std::remove_reference<decltype(*b)>::type>);
}

#define SZ(vec)         int((vec).size())
#define ALL(data)       data.begin(),data.end()
#define RALL(data)      data.rbegin(),data.rend()
#define TYPEMAX(type)   std::numeric_limits<type>::max()
#define TYPEMIN(type)   std::numeric_limits<type>::min()

const int mod = 1000 * 1000 * 1000 + 7;

int mult(int a, int b) {
    return (int64_t(a) * b) % mod;
}

int add(int a, int b) {
    return (a + b >= mod ? a + b - mod : a + b);
}

int sub(int a, int b) {
    return (a >= b ? a - b : mod + a - b);
}

int main() {
    std::iostream::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    // code here
    int n = input<int>();

    vector<int> p(n);
    for (int& elem: p)
        elem = input<int>() - 1;

    int avg_ans = n;

    vector<vector<int>> gr(n);
    for (int i = 0; i < n; ++i)
        if (p[i] != -2) {
            gr[i].push_back(p[i]);
            gr[p[i]].push_back(i);
        }

    vector<int> cid(n, -1);
    int curcid = 0;
    vector<vector<int>> lst;
    vector<char> is_open;
    
    std::function<void(int)> dfs = [&](int v) {
        cid[v] = curcid;
        lst[cid[v]].push_back(v);
        
        for (int u: gr[v])
            if (cid[u] == -1)
                dfs(u);

        if (p[v] == -2)
            is_open[curcid] = true;
    };
    
    for (int v = 0; v < n; ++v)
        if (cid[v] == -1) {
            lst.push_back(vector<int> {});
            is_open.push_back(false);
            dfs(v);

            curcid++;

            if (not is_open.back())
                --avg_ans;
        }

    int rev = 1;
    int A = n - 1;
    
    for (int pw = mod - 2; pw; A = mult(A, A), pw /= 2) {
        if (pw % 2)
            rev = mult(rev, A);
    }

    vector<int> data(n + 1, 0);
    data[0] = 1;

    auto upd = [&](int val) {
        for (int i = n; i > 0; --i) {
            data[i] = add(data[i], mult(data[i - 1], mult(val, max(1, i - 1))));
        }
    };
    
    for (int i = 0; i < curcid; ++i) {
        if (not is_open[i])
            continue;
        
        avg_ans = sub(avg_ans, mult(SZ(lst[i]) - 1, rev)); // with this probability we have self-loop
        
        upd(mult(SZ(lst[i]), rev));
    }

    for (int i = 2; i <= n; ++i)
        avg_ans = sub(avg_ans, data[i]);
    
    for (int i = 0; i < n; ++i)
        if (p[i] == -2)
            avg_ans = mult(avg_ans, n - 1);

    cout << avg_ans << "\n";
    
    return 0;
}


Measured memory_bytes: 4100