// #include "pch.h"
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <bitset>
#include <climits>
#include <string>
#include <cmath>
#include <bitset>
#include <complex>
#include <functional>
#include <ctime>
#include <cassert>
#include <fstream>
#include<unordered_map>
#include <stack>
#include <random>

typedef long long ll;
typedef std::pair<int, int> Pii;
typedef std::pair<long long, long long> Pll;
typedef std::pair<double, double> Pdd;

#define rip(i, n, s) for (int i = (s);i < (int)( n ); i++)
#define all(a) a.begin(), a.end()
#define MM << " " <<

template<typename T>
using MaxHeap = std::priority_queue<T>;
template<typename T>
using MinHeap = std::priority_queue<T, std::vector<T>, std::greater<T>>;

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

template<typename T>
void vdeb(std::vector<T> &da) {
    for (int i = 0;i < da.size();i++) {
        if (i == da.size() - 1) std::cout << da[i];
        else std::cout << da[i] << ' ';
    }
    std::cout << std::endl;
}
template<typename T>
void vdeb(std::vector<std::vector<T>> &da) {
    for (int i = 0;i < da.size();i++) {
        std::cout << i << ' ';
        vdeb(da[i]);
    }
    std::cout << std::endl;
}

long long pow(long long n, long long p, long long k) {//n^k(mod p)
    if (!k) return 1;
    long long a = pow(n,p, k>>1);
    a = a * a%p;
    if (k & 1) a = a * n%p;
    return a;
}
void euclid(long long &a, long long &b, long long p) { // a>=b A*b+B*(a-a/b*b)=1
    if (a == 1) {
        a = 1;
    }
    else {
        long long A = b, B = a % b;
        euclid(A, B, p);
        b = (A - (p + a / b) % p * B % p + p) % p;
        a = B;
    }
}
long long rev(long long n, long long p) {// n*x-p*y=1
    //long long q = p;
    //euclid(p, n, p);
    //return n % q;
    return pow(n,p,p-2);
}
long long bino(long long n, long long m, long long p) {//nCm(mod p)
    long long ans = 1, div = 1;
    for(int i = 0;i < m; i++){
        ans = (n - i) * ans % p;
        div = div * (i +1) % p;
    }
    return ans * rev(div, p) % p;
}
struct modint {
    long long num;
    long long p;
    modint() {
        num = 0;
        p = 1000000007;
    }
    modint(long long x) {
        p = 1000000007;
        num = x % p;
    }
    modint inv()const{return rev(num, p);}
    modint operator-() const{return modint(p-num);}
    modint& operator+=(const modint &other){
        num = (num + other.num) % p;
        return *this;
    }
    modint& operator-=(const modint &other){
        num = (num - other.num + p) % p;
        return *this;
    }
    modint& operator*=(const modint &other){
        num = (num * other.num) % p;
        return *this;
    }
    modint& operator/=(const modint &other){
        (*this) *= other.inv();
        return *this;
    }
    modint& operator+=(const long long &other){
        num = (num + other) % p;
        return *this;
    }
    modint& operator-=(const long long &other){
        num = (num - other + p) % p;
        return *this;
    }
    modint& operator*=(const long long &other){
        num = (num * other) % p;
        return *this;
    }
    modint& operator/=(const long long &other){
        (*this) *= rev(other, p);
        return *this;
    }
    modint& operator++(){return *this += 1;}
    modint& operator--(){return *this -= 1;}
    modint& operator=(const long long &other){return (*this) = modint(other);}
    modint operator+(const modint &other) const{return modint(*this) += other;}
    modint operator-(const modint &other) const{return modint(*this) -= other;}
    modint operator*(const modint &other) const{return modint(*this) *= other;}
    modint operator/(const modint &other) const{return modint(*this) /= other;}
    modint operator+(const long long &other) const{return modint(*this) += other;}
    modint operator-(const long long &other) const{return modint(*this) -= other;}
    modint operator*(const long long &other) const{return modint(*this) *= other;}
    modint operator/(const long long &other) const{return modint(*this) /= other;}
    bool operator==(const modint &other) const{return num == other.num;}
};
std::istream& operator>>(std::istream &is, const modint x) {
    std::cin >> x.num;
    return is;
}
std::ostream& operator<<(std::ostream &os, const modint &x){
    std::cout << x.num;
    return os;
}

struct UnionFind{
    int size;
    std::vector<int> par;
    std::vector<int> rank;
    UnionFind(int x){
        size=x;
        par=std::vector<int> (x);
        rank=std::vector<int> (x,1);
        for (int i=0;i<x;i++) par[i]=i;
    }
    bool isRoot(int x) {
        return par[x] == x;
    }
    int root(int x){
        if(isRoot(x)) return x;
        else return par[x]=root(par[x]);
    }
    void unite(int x,int y){
        x=root(x);y=root(y);
        if(x==y) return;
        if(rank[x]<rank[y]) {
            par[x]=y;
            rank[y] += rank[x];
        }
        else {
            par[y] = x;
            rank[x] += rank[y];
        }
    }
    bool check(int x,int y){
        return root(x)==root(y);
    }
    int rsize(int x) {
        return rank[root(x)];
    }
    void initialize(){
        for (int i=0;i<size;i++){
            par[i]=i;rank[i]=1;
        }
    }
};

using namespace std;


int main() {
    int n, k = 0; cin >> n;
    vector<int> da(n);
    set<int> st;
    UnionFind uf(n);
    rip(i,n,0) {
        cin >> da[i];
        if(da[i] == -1) ++k;
        else {
            --da[i];
            uf.unite(i, da[i]);
        }
    }
    vector<vector<modint>> dp(n+1, vector<modint>(n+1));
    dp[0][0] += 1;
    rip(i,n,0) {
        st.insert(uf.root(i));
        if(da[i] == -1) {
            modint adder(uf.rsize(i));
            rip(j, i+2, 0) {
                dp[i+1][j] = dp[i][j];
                if(j > 0) dp[i+1][j] += dp[i][j-1] * adder;
            }
        }
        else {
            rip(j,i+1,0) dp[i+1][j] = dp[i][j];
        }
    }
    modint ans(pow(n-1, dp[0][0].p, k));
    dp[n][1] -= k;
    // vdeb(dp);
    ans *= n-(st.size() - k);
    modint pp(1),kk(1);
    rip(i,k,0) {
        dp[n][k-i] *= kk;
        dp[n][i+1] *= pp;
        kk *= n-1;
        pp *= i+1;
    }
    rip(i,k,0) {
        ans -= dp[n][i+1];
    }
    cout << ans << endl;
}

Measured memory_bytes: 394804