#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
using WGraph = vector<vector<pair<int, ll>>>;

template<class T>inline bool chmax(T &a, const T &b) { if (b > a) { a = b; return true; } return false; }
template<class T>inline bool chmin(T &a, const T &b) { if (b < a) { a = b; return true; } return false; }

constexpr int dx[4] = {-1, 0, 0, 1}, dy[4] = {0, -1, 1, 0};
constexpr int INF = 1e9;
constexpr int MOD = 1e9 + 7;
constexpr long long LINF = 1e18;
constexpr double EPS = 1e-10;
constexpr double PI = M_PI;

class UnionFind {
    private :
        std::vector<int> par;
    public :
        UnionFind(int n) {
            par.resize(n);
            for (int i=0; i<n; i++) par.at(i) = i;
        }
        void unite(int x, int y) {
            int xPar = find(x);
            int yPar = find(y);
            if (xPar == yPar) return;

            par.at(xPar) = yPar;
        }
        int find(int x) {
            if (par.at(x) == x) return x;
            else return par.at(x) = find(par.at(x));
        }
        bool same(int x, int y) { return find(x) == find(y); }
};

template<int mod>
class ModInt {
    private:
        long long val;
    public:
        constexpr ModInt(const long long v = 0) : val(v%mod) { if (val < 0) val += mod; }
        constexpr ModInt(const ModInt &v) : val(v.val) {}
        constexpr ModInt &operator=(const long long v) { val = v; return *this; }
        constexpr ModInt &operator=(const ModInt &v) { if (this != &v) val = v.val; return *this; }
        constexpr ModInt operator+() const { return ModInt(val); }
        constexpr ModInt operator-() const { return ModInt(-val); }
        constexpr ModInt operator+(const ModInt &v) const { return ModInt(*this) += v; }
        constexpr ModInt operator-(const ModInt &v) const { return ModInt(*this) -= v; }
        constexpr ModInt operator*(const ModInt &v) const { return ModInt(*this) *= v; }
        constexpr ModInt operator/(const ModInt &v) const { return ModInt(*this) /= v; }
        constexpr ModInt &operator+=(const ModInt &v) { val += v.val; if (val >= mod) val -= mod; return *this; }
        constexpr ModInt &operator-=(const ModInt &v) { val -= v.val; if (val < 0) val += mod; return *this; }
        constexpr ModInt &operator*=(const ModInt &v) { val *= v.val; if (val >= mod) val %= mod; return *this; }
        constexpr ModInt &operator/=(const ModInt &v) { val *= v.inverse(); if (val >= mod) val %= mod; return *this; }
        constexpr ModInt &operator++() { ++val; if (val >= mod) val -= mod; return *this; }
        constexpr ModInt &operator--() { --val; if (val < 0) val += mod; return *this; }
        constexpr ModInt operator++(int) { ModInt tmp(*this); operator++(); return tmp; }
        constexpr ModInt operator--(int) { ModInt tmp(*this); operator--(); return tmp; }
        constexpr bool operator==(const ModInt &v) const { return val == v.val; }
        constexpr bool operator!=(const ModInt &v) const { return !(*this == v); }
        friend std::istream &operator>>(std::istream &s, ModInt &v) { long long tmp; s >> tmp; v = ModInt(tmp); return s; }
        friend std::ostream &operator<<(std::ostream &s, const ModInt &v) { return s << v.val; }
        constexpr ModInt inverse() const {
            long long n = val, res = 1;
            while (n > 1) {
                res *= (mod - mod / n);
                res %= mod;
                n = mod % n;
            }
            return ModInt(res);
        }
        friend constexpr ModInt modpow(ModInt v, long long n) {
            ModInt res(1);
            for ( ; n>0; n>>=1) {
                if (n & 1) res *= v;
                v *= v;
            }
            return res;
        }
};

void solve() {
    ll n;
    cin >> n;
    vector<int> p(n);
    for (int i=0; i<n; ++i) {
        cin >> p.at(i);
        if (p.at(i) > 0) --p.at(i);
    }

    UnionFind uf(n);
    for (int i=0; i<n; ++i) {
        if (p.at(i) != -1) uf.unite(i, p.at(i));
    }
    vector<pair<int, bool>> cnt(n, {0, false});
    for (int i=0; i<n; ++i) {
        ++cnt.at(uf.find(i)).first;
        if (p.at(i) == -1) cnt.at(uf.find(i)).second = true;
    }
    vector<ll> groups;
    int loops = 0;
    for (auto p : cnt) {
        if (p.first > 0) {
            if (p.second) groups.push_back(p.first);
            else ++loops;
        }
    }

    int k = (int)groups.size();
    vector<vector<ModInt<MOD>>> dp(k+1, vector<ModInt<MOD>>(k+1));
    for (int i=0; i<k+1; ++i) dp.at(i).at(0) = 1;
    for (int i=1; i<k+1; ++i) {
        for (int j=1; j<k+1; ++j) {
            dp.at(i).at(j) = dp.at(i-1).at(j) + dp.at(i-1).at(j-1) * groups.at(i-1);
        }
    }
    ModInt<MOD> res(n - loops);
    for (int i=0; i<k; ++i) {
        res *= n-1;
    }
    ModInt<MOD> fact(1);
    for (int i=1; i<k+1; ++i) {
        dp.back().at(i) *= fact;
        if (i == 1) dp.back().at(i) -= k;
        fact *= i;
    }
    ModInt<MOD> power = 1;
    for (int i=k; i>0; --i) {
        dp.back().at(i) *= power;
        power *= n-1;
    }
    for (int i=1; i<k+1; ++i) {
        res -= dp.back().at(i);
    }
    cout << res << '\n';
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(20);

    solve();

    return 0;
}


Measured memory_bytes: 198992