Problem p01718 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

#define int long long
#define rep(i, n) for (int i = 0; i < (n); i++)
#define INF ((long long)1e18)
#define MOD ((int)1e9 + 7)
#define endl "\\n"

#define yn(f) ((f) ? "Yes" : "No")
#define YN(f) ((f) ? "YES" : "NO")

#define MAX 220000
#define NUM 50

int n;
pair<int, int> dat[2 * MAX - 1];

int init(int n_) {
  n = 1;

  while (n < n_)
    n *= 2;

  for (int i = 0; i < 2 * n - 1; i++)
    dat[i] = make_pair(0, 1);

  return n;
}

void update(int k, pair<int, int> a) {
  k += n - 1;
  dat[k] = a;
  while (k > 0) {
    k = (k - 1) / 2;
    dat[k].second = ((dat[k * 2 + 1].second) /
                     __gcd(dat[k * 2 + 1].second, dat[k * 2 + 2].second)) *
                    dat[k * 2 + 2].second;
    dat[k].first = (dat[k].second / dat[k * 2 + 1].second) % MOD *
                       dat[k * 2 + 1].first % MOD +
                   (dat[k].second / dat[k * 2 + 2].second) % MOD *
                       dat[k * 2 + 2].first % MOD;
    dat[k].first %= MOD;
  }
}

pair<int, int> query(int a, int b, int k, int l, int r) {
  if (r <= a || b <= l) {
    return make_pair(0, 1);
  }

  if (a <= l && r <= b) {
    return dat[k];
  } else {
    pair<int, int> vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
    pair<int, int> vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
    pair<int, int> res;

    res.second = (vl.second / __gcd(vl.second, vr.second)) * vr.second;
    res.first = (res.second / vl.second) % MOD * vl.first % MOD +
                (res.second / vr.second) % MOD * vr.first % MOD;
    res.first %= MOD;

    return res;
  }
}
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);

  int l, r;
  int N, Q;
  int period, sum;
  pair<int, int> ans;
  vector<int> permutation;

  cin >> N >> Q;

  permutation.resize(N + 1);
  for (int i = 1; i <= N; i++) {
    cin >> permutation[i];
  }

  init(N + 5);

  for (int i = 1, num; i <= N; i++) {
    num = i;
    sum = 0;
    for (int j = 1; j < NUM; j++) {
      sum += num;
      sum %= MOD;

      num = permutation[num];

      period = j;
      if (num == i)
        break;
    }
    update(i, make_pair(sum, period));
  }

  for (int i = 0; i < Q; i++) {
    cin >> l >> r;
    ans = query(l, r + 1, 0, 0, n);
    cout << ans.first << endl;
    ;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

#define int __int128
#define rep(i, n) for (int i = 0; i < (n); i++)
#define INF ((long long)1e18)
#define MOD ((int)1e9 + 7)
#define endl "\\n"

#define yn(f) ((f) ? "Yes" : "No")
#define YN(f) ((f) ? "YES" : "NO")

#define MAX 120000
#define NUM 50

int n;
pair<int, int> dat[2 * MAX - 1];

int init(int n_) {
  n = 1;

  while (n < n_)
    n *= 2;

  for (int i = 0; i < 2 * n - 1; i++)
    dat[i] = make_pair(0, 1);

  return n;
}

void update(int k, pair<int, int> a) {
  k += n - 1;
  dat[k] = a;
  while (k > 0) {
    k = (k - 1) / 2;
    dat[k].second = ((dat[k * 2 + 1].second) /
                     __gcd(dat[k * 2 + 1].second, dat[k * 2 + 2].second)) *
                    dat[k * 2 + 2].second;
    dat[k].first = (dat[k].second / dat[k * 2 + 1].second) % MOD *
                       dat[k * 2 + 1].first % MOD +
                   (dat[k].second / dat[k * 2 + 2].second) % MOD *
                       dat[k * 2 + 2].first % MOD;
    dat[k].first %= MOD;
  }
}

pair<int, int> query(int a, int b, int k, int l, int r) {
  if (r <= a || b <= l) {
    return make_pair(0, 1);
  }

  if (a <= l && r <= b) {
    return dat[k];
  } else {
    pair<int, int> vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
    pair<int, int> vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
    pair<int, int> res;

    res.second = (vl.second / __gcd(vl.second, vr.second)) * vr.second;
    res.first = (res.second / vl.second) % MOD * vl.first % MOD +
                (res.second / vr.second) % MOD * vr.first % MOD;
    res.first %= MOD;

    return res;
  }
}
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);

  long long l, r;
  long long N, Q;
  int period, sum;
  pair<long long, long long> ans;
  vector<long long> permutation;

  cin >> N >> Q;

  permutation.resize(N + 1);
  for (int i = 1; i <= N; i++) {
    cin >> permutation[i];
  }

  init(N + 5);

  for (int i = 1, num; i <= N; i++) {
    num = i;
    sum = 0;
    for (int j = 1; j < NUM; j++) {
      sum += num;
      sum %= MOD;

      num = permutation[num];

      period = j;
      if (num == i)
        break;
    }
    update(i, make_pair(sum, period));
  }

  for (int i = 0; i < Q; i++) {
    cin >> l >> r;
    ans = query(l, r + 1, 0, 0, n);
    cout << ans.first << endl;
    ;
  }
  return 0;
}