Language: C++

#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<int(n);++i)
#define all(a) (a).begin(),(a).end()
typedef long long ll;
#define rep1(i,n) for(int i=1;i<=int(n);++i)
#define lb(s,x) (lower_bound(all(s),x)-s.begin())
#define ub(s,x) (upper_bound(all(s),x)-s.begin())
#define uniq(a) a.erase(unique(all(a)),a.end())
#define bit(k) (1LL<<(k))
const int INF = 1e9;

#define debug(x) cerr<<(#x)<<": "<<(x)<<endl
template<class T,class U>
ostream& operator<<(ostream& o, const pair<T,U> &p){
    o<<"("<<p.first<<", "<<p.second<<")";
    return o;
}
#define print(v) { cerr<<#v<<": [ "; for(auto _ : v) cerr<<_<<", "; cerr<<"]"<<endl; }

class modint {
  public:
    using ll = long long;
    ll num;
    static const ll MOD = 1e9+7;
    static vector<modint> factorial;

    modint(): num(0) {};
    modint(ll n_): num((n_ + MOD) % MOD) {}
    modint(const modint &r): num(r.num) {}
    operator ll() const { return (num+MOD)%MOD; }
    friend istream& operator>>(istream& is, modint &r){
        ll num_;
        is >> num_;
        r = num_;
        return is;
    }
    modint operator+(const modint &r) const { return modint(num + r.num); }
    modint operator-(const modint &r) const { return modint(num - r.num); }
    modint operator*(const modint &r) const { return modint(num * r.num); }

    template<typename T>
    modint operator^(const T &r) const {
        if(r == 0) return 1;
        return (((*this)*(*this)) ^ (r/2)) * modint(r&1 ? num : 1);
    }
    modint operator/(const modint &r) const { return num * (r^(MOD-2)); }

    modint operator+=(const modint &r) { return *this = *this + r; }
    modint operator-=(const modint &r) { return *this = *this - r; }
    modint operator*=(const modint &r) { return *this = *this * r; }
    modint operator/=(const modint &r) { return *this = *this / r; }

    template<typename T>
    modint operator^=(const T &r) { return *this = *this ^ r; }

    static modint pow(const modint x, ll r) {
        return x^r;
    }

    static modint fact(int n){
        if((int)factorial.size() <= n) {
            int size = factorial.size();
            factorial.resize(n+1);
            for (int i = size; i <= n; i++) {
                factorial[i] = fact(i-1) * modint(i);
            }
        }
        return factorial[n];
    }

    static modint C(ll n,ll r){
        return n >= r ? fact(n)/(fact(n-r)*fact(r)) : modint(0);
    }
};
vector<modint> modint::factorial = {1};
#define fact(n) modint::fact(n)
#define C(n,r) modint::C(n,r)
#define H(a,b) C(a+b, a)


struct UnionFind{
    vector<int> par;
    vector<int> sizes;
    UnionFind(int n):par(n),sizes(n,1){ rep(i,n)par[i]=i; }

    //find root of x
    int find(int x){
        if(x!=par[x]) par[x] = find(par[x]);
        return par[x];
    }

    void unite(int x,int y){
        x = find(x);
        y = find(y);
        if(x == y) return;
        if(sizes[x] < sizes[y]) swap(x, y);
        par[y] = x;
        sizes[x] += sizes[y];
    }

    bool same(int x,int y){ return find(x) == find(y); }
    int size(int x){ return sizes[find(x)]; }
};

int main(void){
    int n;
    cin >> n;
    vector<ll> p(n);
    UnionFind uf(n);
    rep(i,n) {
        cin >> p[i];
        if (p[i] != -1) {
            uf.unite(i+1, p[i]);
        }
    }

    modint k = count(all(p), -1);
    modint w = uf.size() - k;
    modint res = modint::pow(w,k) * modint(n-w);
    vector<modint> p(k), ap(k);
    p[0] = modint::pow(w,k);
    ap[0] = p[0];
    rep1(i, (k+1)/2){
        p[i] = modint::pow(w+i,k) - ap[i-1];
        ap[i] = p[i] + ap[i-1];
        res += p[i]*modint(n-w-i);
    }
    cout << res <<endl;

    return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:119:23: error: no matching function for call to 'UnionFind::size()'
  119 |     modint w = uf.size() - k;
      |                ~~~~~~~^~
a.cc:103:9: note: candidate: 'int UnionFind::size(int)'
  103 |     int size(int x){ return sizes[find(x)]; }
      |         ^~~~
a.cc:103:9: note:   candidate expects 1 argument, 0 provided
a.cc:121:20: error: conflicting declaration 'std::vector<modint> p'
  121 |     vector<modint> p(k), ap(k);
      |                    ^
a.cc:109:16: note: previous declaration as 'std::vector<long long int> p'
  109 |     vector<ll> p(n);
      |                ^
a.cc:124:15: error: ambiguous overload for 'operator+' (operand types are 'modint' and 'int')
  124 |     rep1(i, (k+1)/2){
      |              ~^~
      |              | |
      |              | int
      |              modint
a.cc:6:38: note: in definition of macro 'rep1'
    6 | #define rep1(i,n) for(int i=1;i<=int(n);++i)
      |                                      ^
a.cc:124:15: note: candidate: 'operator+(modint::ll {aka long long int}, int)' (built-in)
  124 |     rep1(i, (k+1)/2){
      |              ~^~
a.cc:6:38: note: in definition of macro 'rep1'
    6 | #define rep1(i,n) for(int i=1;i<=int(n);++i)
      |                                      ^
a.cc:38:12: note: candidate: 'modint modint::operator+(const modint&) const'
   38 |     modint operator+(const modint &r) const { return modint(num + r.num); }
      |            ^~~~~~~~
a.cc:125:29: error: ambiguous overload for 'operator+' (operand types are 'modint' and 'int')
  125 |         p[i] = modint::pow(w+i,k) - ap[i-1];
      |                            ~^~
      |                            | |
      |                            | int
      |                            modint
a.cc:125:29: note: candidate: 'operator+(modint::ll {aka long long int}, int)' (built-in)
  125 |         p[i] = modint::pow(w+i,k) - ap[i-1];
      |                            ~^~
a.cc:38:12: note: candidate: 'modint modint::operator+(const modint&) const'
   38 |     modint operator+(const modint &r) const { return modint(num + r.num); }
      |            ^~~~~~~~