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

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

#define fi first
#define se second
#define repl(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define each(itr, v) for (auto itr : v)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define mmax(x, y) (x > y ? x : y)
#define mmin(x, y) (x < y ? x : y)
#define maxch(x, y) x = mmax(x, y)
#define minch(x, y) x = mmin(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt __builtin_popcount

#define INF 1e16
#define mod 1000000007

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

struct segtree {
  ll dat[(1 << 19) - 1];
  ll size;

  void init(ll n_) {
    size = 1;
    while (size < n_)
      size *= 2;
    for (int i = 0; i < 2 * size - 1; i++)
      dat[i] = 1;
  }

  void update(ll k, ll a) {
    k += size - 1;
    dat[k] = a;
    while (k > 0) {
      k = (k - 1) / 2;
      dat[k] = lcm(dat[k * 2 + 1], dat[k * 2 + 2]);
    }
  }

  ll subquery(ll a, ll b, ll k, ll l, ll r) {
    if (r <= a || b <= l)
      return 1;
    if (a <= l && r <= b)
      return dat[k];
    else {
      return lcm(subquery(a, b, k * 2 + 1, l, (l + r) / 2),
                 subquery(a, b, k * 2 + 2, (l + r) / 2, r));
    }
  }

  ll query(ll a, ll b) { return subquery(a, b, 0, 0, size); }
};

ll mod_pow(ll a, ll n) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

ll n, q;
ll sum[211111];
ll p[211111];
segtree sgt;

int main() {
  cin.sync_with_stdio(false);
  cin >> n >> q;
  rep(i, n) {
    cin >> p[i];
    p[i]--;
  }
  sgt.init(n);
  rep(i, n) {
    ll cnt = 1, crt = p[i];
    ll s = p[i] + 1;
    while (crt != i) {
      crt = p[crt];
      cnt++;
      s += crt + 1;
    }
    sgt.update(i, cnt);
    cnt %= mod;
    s %= mod;
    sum[i + 1] = s * mod_pow(cnt, mod - 2) % mod;
  }
  rep(i, n) { sum[i + 1] = (sum[i + 1] + sum[i]) % mod; }
  rep(hoge, q) {
    ll l, r;
    cin >> l >> r;
    l--;
    ll lc = sgt.query(l, r) % mod;
    cout << (((sum[r] - sum[l] + mod) % mod) * lc) % mod << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

#define fi first
#define se second
#define repl(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define each(itr, v) for (auto itr : v)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define mmax(x, y) (x > y ? x : y)
#define mmin(x, y) (x < y ? x : y)
#define maxch(x, y) x = mmax(x, y)
#define minch(x, y) x = mmin(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt __builtin_popcount

#define INF 1e16
#define mod 1000000007

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

struct segtree {
  ll dat[(1 << 19) - 1];
  int size;

  void init(int n_) {
    size = 1;
    while (size < n_)
      size *= 2;
    for (int i = 0; i < 2 * size - 1; i++)
      dat[i] = 1;
  }

  void update(int k, ll a) {
    k += size - 1;
    dat[k] = a;
    while (k > 0) {
      k = (k - 1) / 2;
      dat[k] = lcm(dat[k * 2 + 1], dat[k * 2 + 2]);
    }
  }

  ll subquery(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l)
      return 1;
    if (a <= l && r <= b)
      return dat[k];
    else {
      return lcm(subquery(a, b, k * 2 + 1, l, (l + r) / 2),
                 subquery(a, b, k * 2 + 2, (l + r) / 2, r));
    }
  }

  ll query(int a, int b) { return subquery(a, b, 0, 0, size); }
};

ll mod_pow(ll a, ll n) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

ll n, q;
ll sum[11111];
ll p[11111];
segtree sgt;

int main() {
  cin.sync_with_stdio(false);
  cin >> n >> q;
  rep(i, n) {
    cin >> p[i];
    p[i]--;
  }
  sgt.init(n);
  rep(i, n) {
    ll cnt = 1, crt = p[i];
    ll s = p[i] + 1;
    while (crt != i) {
      crt = p[crt];
      cnt++;
      s += crt + 1;
    }
    cnt %= mod;
    s %= mod;
    sum[i + 1] = s * mod_pow(cnt, mod - 2) % mod;
    sgt.update(i, cnt);
  }
  rep(i, n) { sum[i + 1] = (sum[i + 1] + sum[i]) % mod; }
  rep(hoge, q) {
    ll l, r;
    cin >> l >> r;
    l--;
    ll lc = sgt.query(l, r);
    cout << ((sum[r] - sum[l] + mod * 2) % mod) * lc % mod << endl;
  }
  return 0;
}