Language: C++

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <queue>
#include <bitset>
#include <stack>
#include <functional>

#ifdef LOCAL
    #define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
    #define eprintf(...) 42
#endif

#define rep_(i, a_, b_, a, b, ...) for (int i = (a), i##_len = (b); i < i##_len; ++i)
#define rep(i, ...) rep_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define reprev_(i, a_, b_, a, b, ...) for (int i = (b-1), i##_min = (a); i >= i##_min; --i)
#define reprev(i, ...) reprev_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define all(x) (x).begin(), (x).end()
template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; }
template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; }
// template <class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair <int,int> P;
typedef long double ld;

struct UnionFind {
    vector< int > data;
    UnionFind(int sz) {
        data.assign(sz, -1);
    }
    bool unite(int x, int y) {
        x = find(x), y = find(y);
        if(x == y) return (false);
        if(data[x] > data[y]) swap(x, y);
        data[x] += data[y];
        data[y] = x;
        return (true);
    }
    int find(int k) {
        if(data[k] < 0) return (k);
        return (data[k] = find(data[k]));
    }    
    int size(int k) {
        return (-data[find(k)]);
    }
};

constexpr int MOD = 1000000007;

// + - * / ^
int mod(int a)
{
    int res = a % MOD;
    if(res < 0) {
        return res + MOD;
    }
    return res;
}
int mul_mod(int a, int b)
{
    ll res = ((ll)a * (ll)b) % MOD;
    return mod((int)res);
}
int pow_mod(int a, int b)
{
    ll res = 1;
    while (b > 0) {
        if(b & 1) {
            res = res * (ll)a % (ll)MOD;
        }
        a = mul_mod(a, a);
        b >>= 1;
    }
    return (int)res;
}
int inv_mod(int a)
{
    return pow_mod(a, MOD - 2);
}
int div_mod(int a, int b)
{
    return mul_mod(a, inv_mod(b));
}

// ! C
constexpr int FAC_MAX = 3000001;
ll fac[FAC_MAX], finv[FAC_MAX], inv[FAC_MAX];
void com_init()
{
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (int i = 2; i < FAC_MAX; i++){
        fac[i] = fac[i - 1] * i % MOD;
        inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
        finv[i] = finv[i - 1] * inv[i] % MOD;
    }
}
ll com_mod(int a, int b){
    if (a < b) return 0;
    if (a < 0 || b < 0) return 0;
    return fac[a] * (finv[b] * finv[a - b] % MOD) % MOD;
}

int main (void)
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    com_init();
    int n; cin >> n;
    vector<int> a(n); rep (i, n) cin >> a[i];
    UnionFind uf(n);
    int k = 0;
    rep (i, n) {
        if (a[i] > 0) uf.unite(i, a[i] - 1);
        else k++;
    }
    set<int> st; rep (i, n) st.insert(uf.find(i));
    int ren = st.size();
    vector<int> q(n);
    vector<int> f(n); rep (i, n) f[i] = uf.find(i);
    vector<int> p(n); rep (i, n) p[i] = uf.size(i) ;
    rep (i, n) {
        if (a[i] != -1) continue;
        rep (j, i + 1, n) {
            if (f[i] != f[j] && a[i]) q[j]++;
        }
    }
    ll mult = pow_mod(n - 1, k);
    ll ans = mul_mod(n - ren, mult);
    rep (i, n) {
        if (a[i] != -1) continue;
        eprintf("%d : %d %d\n", i, p[i], q[i]);
        ll minus = mul_mod(mult, div_mod(p[i] - 1, n - 1));
        rep (i, q[i] + 1) {
            ll gue = mod(minus + div_mod(fac[p[i] + q[i] - 1], fac[p[i] - 1]));
            gue = mul_mod(gue, com_mod(q, i));
            minus = mod(minus + mul_mod(i, mul_mod(gue, pow_mod(n - (p[i] + q[i] - i), i))));
        }
        ans = mod(ans + mult - minus);
    }
    cout << ans << "\n";
    return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:156:40: error: cannot convert 'std::vector<int>' to 'int'
  156 |             gue = mul_mod(gue, com_mod(q, i));
      |                                        ^
      |                                        |
      |                                        std::vector<int>
a.cc:118:16: note:   initializing argument 1 of 'll com_mod(int, int)'
  118 | ll com_mod(int a, int b){
      |            ~~~~^