Language: C++

// #define _GLIBCXX_DEBUG // for STL debug (optional)
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
#include <bitset>
using namespace std;
using ll = long long int;
using int64 = long long int;
 
template<typename T> void chmax(T &a, T b) {a = max(a, b);}
template<typename T> void chmin(T &a, T b) {a = min(a, b);}
template<typename T> void chadd(T &a, T b) {a = a + b;}
 
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const int INF = 1LL << 29;
const ll LONGINF = 1LL << 60;
const ll MOD = 1000000007LL;

// #pragma once

/**
 * @brief Union-Find
 * @docs ./docs/union_find.md
 */

#include <algorithm>
#include <vector>

struct UnionFind {
private:
    const int n;
    int size_;
    vector<int> uf;
public:
    // 初期化 UnionFind uni(n) のように宣言すれば良い
    UnionFind(int _n) : n(_n), size_(_n), uf(_n, -1) {}
    // find (木の根を求める)
    int find(int x) {return (uf[x] < 0) ? x : uf[x] = find(uf[x]);}
    // x と y が同じ集合に属するかどうか
    bool same(int x, int y) {return find(x) == find(y);}
    // x が属する集合の要素数
    int size(int x) {return -uf[find(x)];}
    // 集合はいくつあるか
    int size()      {return size_;}
    // x と y の属する集合を併合
    bool unite(int x, int y) {
        x = find(x); y = find(y);
        if(x == y) return false;
        size_--;
        if(-uf[x] < -uf[y]) swap(x, y);
        uf[x] += uf[y]; uf[y] = x;
        return true;
    }
};

void merge(vector<ll> &dp, vector<ll> &sum, pair<int, int> e) {
    int sz, ch; tie(sz, ch) = e;

    vector<ll> n_dp(dp.size() + 1), n_sum(sum.size() + 1);
    for(int i=0; i<dp.size(); i++) {
        (n_dp[i] += dp[i] * dp.size() % MOD) %= MOD;
        (n_dp[i+1] += dp[i] * (sz - 1) % MOD) %= MOD;
    }
    swap(n_dp, dp);
    swap(n_sum, sum);
}

int main() {
    int N; scanf("%d", &N);
    UnionFind uf(N);

    vector<int> todo;
    for(int i=0; i<N; i++) {
        int v; scanf("%d", &v);
        if(v >= 0) {
            v--;
            uf.unite(i, v);
        }
        else todo.emplace(i);
    }

    int base = 0;
    vector<int> checked(N), inc_todo(N);
    for(int i=0; i<N; i++) {
        int v = uf.find(i);
        if(checked[v]) continue;
        checked[v] = true;
        base += uf.size(v) - 1;
    }
    for(auto v : todo) {
        v = uf.find(v);
        inc_todo[v] = true;
    }

    fill(checked.begin(), checked.end(), false);
    vector< pair<int, int> > info;
    for(int i=0; i<N; i++) {
        if(checked[i]) continue;
        info.emplace_back(uf.size(i), inc_todo[i]);
    }

    vector<ll> dp(1, 1), sum(1, 0);
    for(auto e : info) merge(dp, sum, e);

    ll ans = 0;
    for(size_t i=0; i<dp.size(); i++) {
        (ans += 1LL * i * dp[i]) %= MOD;
    }
    cout << ans << endl;
    return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:101:26: error: no matching function for call to 'std::vector<int>::emplace(int&)'
  101 |         else todo.emplace(i);
      |              ~~~~~~~~~~~~^~~
In file included from /usr/include/c++/14/vector:66,
                 from /usr/include/c++/14/queue:63,
                 from a.cc:9:
/usr/include/c++/14/bits/stl_vector.h:1348:9: note: candidate: 'template<class ... _Args> std::vector<_Tp, _Alloc>::iterator std::vector<_Tp, _Alloc>::emplace(const_iterator, _Args&& ...) [with _Args = {_Args ...}; _Tp = int; _Alloc = std::allocator<int>]'
 1348 |         emplace(const_iterator __position, _Args&&... __args)
      |         ^~~~~~~
/usr/include/c++/14/bits/stl_vector.h:1348:9: note:   template argument deduction/substitution failed:
a.cc:101:27: note:   cannot convert 'i' (type 'int') to type 'std::vector<int>::const_iterator'
  101 |         else todo.emplace(i);
      |                           ^