Language: C++

#include <bits/stdc++.h>

using namespace std;

namespace {

    typedef double real;
    typedef long long ll;

    template<class T> ostream& operator<<(ostream& os, const vector<T>& vs) {
        if (vs.empty()) return os << "[]";
        os << "[" << vs[0];
        for (int i = 1; i < vs.size(); i++) os << " " << vs[i];
        return os << "]";
    }
    template<class T> istream& operator>>(istream& is, vector<T>& vs) {
        for (auto it = vs.begin(); it != vs.end(); it++) is >> *it;
        return is;
    }

    const ll MOD = ll(1e9 + 7);

    int N, Q;
    vector<int> P;
    vector<int> L, R;
    vector<int> X;
    void input() {
        cin >> N >> Q;
        P.clear(); P.resize(N + 1, -1);
        for (int i = 1; i <= N; i++) cin >> P[i];
        L.clear(); L.resize(Q, -1);
        R.clear(); R.resize(Q, -1);
        for (int i = 0; i < Q; i++) {
            cin >> L[i] >> R[i];
        }
    }


    void stupid(int k) {
        X.clear(); X.resize(N + 1, -1);
        for (int i = 1; i <= N; i++) {
            X[i] = i;
        }
        ll ret = 0;
        while (true) {
            vector<int> Y(N + 1, -1);
            for (int i = 1; i <= N; i++) {
                Y[i] = P[ X[i] ];
            }
            X = Y;
            for (int i = L[k]; i <= R[k]; i++) {
                ret += X[i];
                ret %= MOD;
            }
            bool flag = true;
            for (int i = L[k]; i <= R[k]; i++) {
                if (X[i] != i) {
                    flag = false;
                    break;
                }
            }
            if (flag) {
                break;
            }
        }
        cout << ret << endl;
    }

    ll lcm(ll a, ll b) {
        return a / __gcd(a, b) * b;
    }

    void solve() {
        /* for (int k = 0; k < Q; k++) { stupid(k); } */

        X.clear(); X.resize(N + 1, -1);
        for (int i = 1; i <= N; i++) {
            X[i] = i;
        }

        // ??????????£???¨index??????????????????????????¨??????????±?????????§???        vector<int> C(N + 1, 0);
        vector<int> S(N + 1, 0);
        for (int k = 1; k <= N; k++) {
            int o = X[k];
            int x = X[k];
            while (true) {
                x = P[x];
                C[k]++;
                S[k] = (S[k] + x) % MOD;
                if (o == x) break;
            }
            assert(C[k] <= 40);
        }

        for (int q = 0; q < Q; q++) {
            int l = L[q], r = R[q];
            ll x = 1;
            for (int i = l; i <= r; i++) {
                x = lcm(x, C[i]);
            }
            ll s = 0;
            for (int i = l; i <= r; i++) {
                s = (s + S[i] * ((x / C[i]) % MOD)) % MOD;
            }
            cout << s << endl;
        }
    }
}

int main() {
    input(); solve();
    return 0;
}

Compiler output:
a.cc: In function 'void {anonymous}::solve()':
a.cc:88:17: error: 'C' was not declared in this scope
   88 |                 C[k]++;
      |                 ^
In file included from /usr/include/c++/14/cassert:44,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:106,
                 from a.cc:1:
a.cc:92:20: error: 'C' was not declared in this scope
   92 |             assert(C[k] <= 40);
      |                    ^
a.cc:99:28: error: 'C' was not declared in this scope
   99 |                 x = lcm(x, C[i]);
      |                            ^
a.cc:103:39: error: 'C' was not declared in this scope
  103 |                 s = (s + S[i] * ((x / C[i]) % MOD)) % MOD;
      |                                       ^