#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define sf scanf
#define pf printf
#define pb push_back
#define mp make_pair
#define PI ( acos(-1.0) )
#define IN freopen("C.in","r",stdin)
#define OUT freopen("output.txt","w",stdout)
#define FOR(i,a,b) for(i=a ; i<=b ; i++)
#define DBG pf("Hi\n")
#define INF 1000000000
#define i64 long long int
#define ui64 unsigned long long int
#define eps (1e-8)
#define xx first
#define yy second
#define ln 17
#define off 2

using namespace __gnu_pbds;
using namespace std ;

typedef tree< i64, null_type, less<i64>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
typedef pair<i64, i64> pii;

#define mod 1000000007LL

#define maxn 1000005
#define log 20

class numberTheory{

public:
    numberTheory(){}

    pii extendedEuclid(i64 a, i64 b) { // returns x, y | ax + by = gcd(a,b)

        if(b == 0) return pii( a >= 0 ? 1 : -1 , 0LL);
        else {
            pii d = extendedEuclid(b, a % b);
            return pii(d.yy, d.xx - d.yy * (a / b));
        }

    }

    i64 modularInverse(i64 a, i64 n) {
        pair<i64,i64> ret = extendedEuclid(a, n);
        return ((ret.xx % n) + n) % n;
    }


    i64 bigMod(i64 a, i64 n , i64 m)
    {
        if(n==0) return 1 ;
        i64 ret = bigMod(a,n/2,m) ;
        ret = (ret*ret)%m ;
        if(n%2) ret = (ret*a)%m ;
        return ret ;
    }
};

const i64 N = 200000 ;

i64 fact[N+5] , invFact[N+5] ;

i64 C(i64 n, i64 r)
{
    if( n<0 || r<0 || r>n ) return 0 ;
    i64 ret = (fact[n]*invFact[r])%mod ;
    return (ret*invFact[n-r])%mod ;
}

void preprocess()
{
    numberTheory nt ;

    fact[0] = 1 ;
    for(i64 x=1 ; x<=N ; x++) fact[x] = (fact[x-1]*x)%mod ;

    invFact[N] = nt.modularInverse(fact[N],mod) ;

    for(i64 x=N-1 ; x>=0 ; x--) invFact[x] = (invFact[x+1]*(x+1) )%mod ;
}

bool isCycle[maxn] ;
int sub[maxn] ;
int cycleId[maxn] ;

i64 dp[5005][5005] ;
int a[maxn] ;

int main()
{
    preprocess() ;

    int n ;

    scanf("%d",&n) ;

    for(int i=1; i<=n ; i++)
    {
        scanf("%d",&a[i]) ;
    }

    for(int i=1 ; i<=n ; i++)
    {
        if(a[i]==-1) continue ;
        int cur = i ;
        int mn = i ;

//        printf("--- %d\n",i) ;

        for(int j=1 ; j<=n+1 ; j++)
        {
      //      printf("%d\n",cur) ;
            if( a[cur] == -1 )
            {
//                sub[cur]++ ;
                break ;
            }
            cur = a[cur] ;
            mn = min(mn,cur) ;
            if( cur == i )
            {
                isCycle[i] = true ;
                cycleId[i] = mn ;
//                sub[ mn ]++ ;
                break ;
            }
        }
    }

    for(int i=1 ; i<=n ; i++)
    {
        int cur = i ;
        for(int j=1 ; j<=n+5 ; j++)
        {
            if( a[cur] == -1 )
            {
                sub[cur]++ ;
                break ;
            }
            cur = a[cur] ;
            if( isCycle[cur] )
            {
                sub[cycleId[cur]]++ ;
                break ;
            }
        }
    }

    for(int i=1 ; i<=n ; i++)
    {
    //    printf("%d %d %d %d\n" , i ,isCycle[i], sub[i] , cycleId[i] ) ;
    }

    vector <i64> incomp ;

    for(int i=1 ; i<=n ; i++)
    {
        if( a[i]==-1 ) incomp.pb( sub[i] ) ;
    }

    int k = (int)incomp.size() ;

    numberTheory nt ;

    i64 ans = 0 ;

    for(int i=1 ; i<=n ; i++)
    {
        if( a[i]==-1 || ( isCycle[i] && cycleId[i]==i ) )
        {
     //       printf("-- %d %d %d %d\n",i,n-1,k,sub[i]) ;
            ans += (1LL*nt.bigMod(n-1,k,mod)*(sub[i]-1))%mod ;
            ans %= mod ;
       //     printf("-- %lld\n",ans) ;
        }

        if( a[i]==-1 )
        {
            ans += (1LL*nt.bigMod( n-1,k-1,mod )*(n-sub[i]))%mod ;
            ans %= mod ;
        }
    }
//    printf("-- %lld\n",ans) ;

    dp[0][0] = 1 ;

    for(int i=1; i<=k ; i++)
    {
        dp[i][0] = 1 ;
        for(int j=1 ; j<=i; j++)
        {
            dp[i][j] = ( dp[i-1][j] + 1LL*dp[i-1][j-1]*incomp[i-1] )%mod ;
        }
    }

    for(int i=2; i<=k ; i++)
    {
        i64 ret = (dp[k][i]*fact[i-1])%mod ;
        ret = (ret*nt.bigMod(n-1,k-i,mod))%mod ;
        ans -= ret ;
        ans %= mod ;
    }

    ans = (ans%mod + mod)%mod ;

    printf("%lld\n",ans) ;

    return 0 ;
}


Measured memory_bytes: 123660