Problem p02702 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
#define ll long long
#define ii pair<ll, ll>
#define iii pair<ii, ll>
#define fi first
#define se second
#define endl '\n'
#define debug(x) cout << #x << " is " << x << endl;

#define rep(x, start, end)                                                     \
  for (auto x = (start) - ((start) > (end)); x != (end) - ((start) > (end));   \
       ((start) < (end) ? x++ : x--))
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()

ll MAX(ll a) { return a; }
ll MIN(ll a) { return a; }
template <typename... Args> ll MAX(ll a, Args... args) {
  return max(a, MAX(args...));
}
template <typename... Args> ll MIN(ll a, Args... args) {
  return min(a, MIN(args...));
}

#define indexed_set                                                            \
  tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>

string s;

long long qexp(long long b, long long p, int m) {
  long long res = 1;
  while (p) {
    if (p & 1)
      res = (res * b) % m;
    b = (b * b) % m;
    p >>= 1;
  }
  return res;
}

int arr[2500];
int cnt[2500];

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  cin >> s;

  int val = 0;
  int pow = 1;

  rep(x, s.size(), 0) {
    val += pow * (s[x] - '0');

    val %= 2019;

    pow *= 10;
    pow %= 2019;

    cnt[val]++;
    arr[x] = val;

    // cout<<val<<endl;
  }

  ll ans = 0;

  val = 0;
  pow = 1;
  rep(x, s.size(), 0) {
    // cout<<val<<endl;
    ans += cnt[val];

    val += pow * (s[x] - '0');
    val %= 2019;

    pow *= 10;
    pow %= 2019;

    cnt[arr[x]]--;
  }

  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
#define ll long long
#define ii pair<ll, ll>
#define iii pair<ii, ll>
#define fi first
#define se second
#define endl '\n'
#define debug(x) cout << #x << " is " << x << endl;

#define rep(x, start, end)                                                     \
  for (auto x = (start) - ((start) > (end)); x != (end) - ((start) > (end));   \
       ((start) < (end) ? x++ : x--))
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()

ll MAX(ll a) { return a; }
ll MIN(ll a) { return a; }
template <typename... Args> ll MAX(ll a, Args... args) {
  return max(a, MAX(args...));
}
template <typename... Args> ll MIN(ll a, Args... args) {
  return min(a, MIN(args...));
}

#define indexed_set                                                            \
  tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>

string s;

long long qexp(long long b, long long p, int m) {
  long long res = 1;
  while (p) {
    if (p & 1)
      res = (res * b) % m;
    b = (b * b) % m;
    p >>= 1;
  }
  return res;
}

int arr[200005];
int cnt[2500];

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  cin >> s;

  int val = 0;
  int pow = 1;

  rep(x, s.size(), 0) {
    val += pow * (s[x] - '0');

    val %= 2019;

    pow *= 10;
    pow %= 2019;

    cnt[val]++;
    arr[x] = val;

    // cout<<val<<endl;
  }

  ll ans = 0;

  val = 0;
  pow = 1;
  rep(x, s.size(), 0) {
    // cout<<val<<endl;
    ans += cnt[val];

    val += pow * (s[x] - '0');
    val %= 2019;

    pow *= 10;
    pow %= 2019;

    cnt[arr[x]]--;
  }

  cout << ans << endl;
}