Problem p02702 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
typedef long long ll;
typedef unsigned long long ull;
typedef long double LD;
using namespace std;
const int INF = 2147483647;
const int INF2 = 0x3f3f3f3f;
const ll INF64 = 1e18;
const double INFD = 1e30;
const double EPS = 1e-9;
const double PI = acos(-1);
const int MOD = 998244353;
template <typename T> inline T read() {
  T X = 0, w = 0;
  char ch = 0;
  while (!isdigit(ch)) {
    w |= ch == '-';
    ch = getchar();
  }
  while (isdigit(ch))
    X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
  return w ? -X : X;
}
const int MAXN = 600;
int n, m, k;
int dr[4] = {1, 0, -1, 0};
int dc[4] = {0, 1, 0, -1};
int dr2[8] = {1, 1, 1, -1, -1, -1, 0, 0};
int dc2[8] = {1, 0, -1, 1, 0, -1, 1, -1};
int CASE = 1;
char str[MAXN];
int pref[MAXN];
int tens[MAXN];
void extgcd(ll a, ll b, ll &d, ll &x, ll &y) {
  if (!b) {
    d = a;
    x = 1;
    y = 0;
  } else {
    extgcd(b, a % b, d, y, x);
    y -= x * (a / b);
  }
}

inline ll getInv(ll x, ll mod) {
  ll g, a, b;
  extgcd(x, mod, g, a, b);
  return (a % mod + mod) % mod;
}
int main() {
#ifdef LOCALLL
  freopen("in", "r", stdin);
  freopen("out", "w", stdout);
#endif
  scanf("%s", str + 1);
  n = strlen(str + 1);
  map<int, int> moddd;
  moddd[0] = 1;
  ll ans = 0;
  tens[0] = 1;
  for (int i = 1; i <= n; i++)
    tens[i] = tens[i - 1] * 10 % 2019;
  for (int i = 1; i <= n; i++) {
    pref[i] = (pref[i - 1] * 10 + str[i] - '0') % 2019;
  }
  for (int i = 1; i <= n; i++)
    pref[i] = pref[i] * getInv(tens[i], 2019) % 2019;
  for (int i = 1; i <= n; i++) {
    ans += moddd[pref[i]];
    moddd[pref[i]]++;
  }
  printf("%lld", ans);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
typedef long long ll;
typedef unsigned long long ull;
typedef long double LD;
using namespace std;
const int INF = 2147483647;
const int INF2 = 0x3f3f3f3f;
const ll INF64 = 1e18;
const double INFD = 1e30;
const double EPS = 1e-9;
const double PI = acos(-1);
const int MOD = 998244353;
template <typename T> inline T read() {
  T X = 0, w = 0;
  char ch = 0;
  while (!isdigit(ch)) {
    w |= ch == '-';
    ch = getchar();
  }
  while (isdigit(ch))
    X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
  return w ? -X : X;
}
const int MAXN = 500005;
int n, m, k;
int dr[4] = {1, 0, -1, 0};
int dc[4] = {0, 1, 0, -1};
int dr2[8] = {1, 1, 1, -1, -1, -1, 0, 0};
int dc2[8] = {1, 0, -1, 1, 0, -1, 1, -1};
int CASE = 1;
char str[MAXN];
int pref[MAXN];
int tens[MAXN];
void extgcd(ll a, ll b, ll &d, ll &x, ll &y) {
  if (!b) {
    d = a;
    x = 1;
    y = 0;
  } else {
    extgcd(b, a % b, d, y, x);
    y -= x * (a / b);
  }
}

inline ll getInv(ll x, ll mod) {
  ll g, a, b;
  extgcd(x, mod, g, a, b);
  return (a % mod + mod) % mod;
}
int main() {
#ifdef LOCALLL
  freopen("in", "r", stdin);
  freopen("out", "w", stdout);
#endif
  scanf("%s", str + 1);
  n = strlen(str + 1);
  map<int, int> moddd;
  moddd[0] = 1;
  ll ans = 0;
  tens[0] = 1;
  for (int i = 1; i <= n; i++)
    tens[i] = tens[i - 1] * 10 % 2019;
  for (int i = 1; i <= n; i++) {
    pref[i] = (pref[i - 1] * 10 + str[i] - '0') % 2019;
  }
  for (int i = 1; i <= n; i++)
    pref[i] = pref[i] * getInv(tens[i], 2019) % 2019;
  for (int i = 1; i <= n; i++) {
    ans += moddd[pref[i]];
    moddd[pref[i]]++;
  }
  printf("%lld", ans);
  return 0;
}