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

#include <algorithm>
#include <bitset>
#include <chrono>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, x, n) for (int i = x; i < (n); i++)
#define vint(a, n)                                                             \\
  vint a(n);                                                                   \\
  rep(i, n) cin >> a[i];
#define vll(a, n)                                                              \\
  vll a(n);                                                                    \\
  rep(i, n) cin >> a[i];
#define ALL(n) begin(n), end(n)
#define RALL(n) rbegin(n), rend(n)
#define MOD (1000000007)
// #define MOD (998244353)
#define INF (2e9)
#define INFL (2e18)

typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
using vint = vector<int>;
using vll = vector<ll>;
using vbool = vector<bool>;
template <class T> using arr = vector<vector<T>>;
template <class T> int popcount(T &a) {
  int c = 0;
  rep(i, 8 * (int)sizeof(a)) {
    if ((a >> i) & 1)
      c++;
  }
  return c;
}
template <class T> void pr(T x) { cout << x << endl; }
template <class T> void prvec(vector<T> &a) {
  rep(i, a.size() - 1) { cout << a[i] << " "; }
  pr(a[a.size() - 1]);
}
template <class T> void prarr(arr<T> &a) {
  rep(i, a.size()) if (a[i].empty()) pr("");
  else prvec(a[i]);
}
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  string s;
  cin >> s;
  int n = s.size();
  vint a(2019);
  vint md(n);
  md[0] = 1;
  FOR(i, 1, n) {
    md[i] = md[i - 1] * 10;
    md[i] %= 2019;
  }
  a[0]++;
  // prvec(md);
  reverse(ALL(s));

  ll t = 0;
  rep(i, n) {
    // pr(t);
    t += (s[i] - '0') * md[i];
    t %= 2019;
    a[t]++;
  }

  ll ans = 0;
  rep(i, 2019) { ans += a[i] * (a[i] - 1) / 2; }
  pr(ans);
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <chrono>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, x, n) for (int i = x; i < (n); i++)
#define vint(a, n)                                                             \\
  vint a(n);                                                                   \\
  rep(i, n) cin >> a[i];
#define vll(a, n)                                                              \\
  vll a(n);                                                                    \\
  rep(i, n) cin >> a[i];
#define ALL(n) begin(n), end(n)
#define RALL(n) rbegin(n), rend(n)
#define MOD (1000000007)
// #define MOD (998244353)
#define INF (2e9)
#define INFL (2e18)

typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
using vint = vector<int>;
using vll = vector<ll>;
using vbool = vector<bool>;
template <class T> using arr = vector<vector<T>>;
template <class T> int popcount(T &a) {
  int c = 0;
  rep(i, 8 * (int)sizeof(a)) {
    if ((a >> i) & 1)
      c++;
  }
  return c;
}
template <class T> void pr(T x) { cout << x << endl; }
template <class T> void prvec(vector<T> &a) {
  rep(i, a.size() - 1) { cout << a[i] << " "; }
  pr(a[a.size() - 1]);
}
template <class T> void prarr(arr<T> &a) {
  rep(i, a.size()) if (a[i].empty()) pr("");
  else prvec(a[i]);
}
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  string s;
  cin >> s;
  int n = s.size();
  vint a(2019);
  vint md(2019);
  md[0] = 1;
  FOR(i, 1, 2019) {
    md[i] = md[i - 1] * 10;
    md[i] %= 2019;
  }
  a[0]++;
  // prvec(md);
  reverse(ALL(s));

  ll t = 0;
  rep(i, n) {
    // pr(t);
    t += (s[i] - '0') * md[i];
    t %= 2019;
    a[t]++;
  }

  ll ans = 0;
  rep(i, 2019) { ans += a[i] * (a[i] - 1) / 2; }
  pr(ans);
  return 0;
}