Buggy python submission (verdict: Time Limit Exceeded):
S = input()
Sn = [0] * 2019
Sn[0] = 1
ans = 0
for i in range(len(S)-1, -1, -1):
  S_i = int(S[i:])
  p = S_i%2019
  ans += Sn[p]
  Sn[p] += 1

print(ans)

Fixed submission:
S = input()
Smod = [0] * 2019
Smod[0] = 1
ans = 0
S_i = 0
prevmod = 0
k = 1
for i in range(len(S)-1, -1, -1):
  S_i = (int(S[i]) * k + prevmod) % 2019
  ans += Smod[S_i]
  Smod[S_i] += 1
  prevmod = S_i
  k = (k * 10) % 2019
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import collections
S = input()
Smod = [0] * 2019
Smod[0] = 1
ans = 0
for i in range(len(S)-1, -1, -1):
  S_i = int(S[i:])
  ans += Smod[S_i%2019]
  Smod[S_i%2019] += 1

print(ans)

Fixed submission:
S = input()
Smod = [0] * 2019
Smod[0] = 1
ans = 0
S_i = 0
prevmod = 0
k = 1
for i in range(len(S)-1, -1, -1):
  S_i = (int(S[i]) * k + prevmod) % 2019
  ans += Smod[S_i]
  Smod[S_i] += 1
  prevmod = S_i
  k = (k * 10) % 2019
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
def cmb(n, r, p):
    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)
    return fact[n] * factinv[r] * factinv[n-r] % p

p = 10 ** 9 + 7
N = 10 ** 6  # N は必要分だけ用意する
fact = [1, 1]  # fact[n] = (n! mod p)
factinv = [1, 1]  # factinv[n] = ((n!)^(-1) mod p)
inv = [0, 1]  # factinv 計算用
 
for i in range(2, N + 1):
    fact.append((fact[-1] * i) % p)
    inv.append((-inv[p % i] * (p // i)) % p)
    factinv.append((factinv[-1] * inv[-1]) % p)


import collections
S = input()
Smod = list()
for i in range(len(S)-1, -1, -1):
  S_i = int(S[i:])
  Smod.append(S_i%2019)
c = collections.Counter(Smod)

ans = 0
for amari, kosuu in c.items():
  if(amari == 0):
    ans += cmb(kosuu+1, 2, p)
  elif(kosuu >= 2):
    ans += cmb(kosuu, 2, p)
print(ans)

Fixed submission:
S = input()
Smod = [0] * 2019
Smod[0] = 1
ans = 0
S_i = 0
prevmod = 0
k = 1
for i in range(len(S)-1, -1, -1):
  S_i = (int(S[i]) * k + prevmod) % 2019
  ans += Smod[S_i]
  Smod[S_i] += 1
  prevmod = S_i
  k = (k * 10) % 2019
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
def cmb(n, r, p):
    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)
    return fact[n] * factinv[r] * factinv[n-r] % p

p = 10 ** 9 + 7
N = 10 ** 6  # N は必要分だけ用意する
fact = [1, 1]  # fact[n] = (n! mod p)
factinv = [1, 1]  # factinv[n] = ((n!)^(-1) mod p)
inv = [0, 1]  # factinv 計算用
 
for i in range(2, N + 1):
    fact.append((fact[-1] * i) % p)
    inv.append((-inv[p % i] * (p // i)) % p)
    factinv.append((factinv[-1] * inv[-1]) % p)


import collections
S = input()
Smod = list()
for i in range(len(S)-1, -1, -1):
  S_i = int(S[i:])
  Smod.append(S_i%2019)
c = collections.Counter(Smod)

ans = 0
for amari, kosuu in c.items():
  if(amari == 0):
    ans += cmb(kosuu+1, 2, p)
  elif(kosuu >= 2):
    ans += cmb(kosuu, 2, p)
print(ans)

Fixed submission:
S = input()
Smod = [0] * 2019
Smod[0] = 1
ans = 0
S_i = 0
prevmod = 0
k = 1
for i in range(len(S)-1, -1, -1):
  S_i = (int(S[i]) * k + prevmod) % 2019
  ans += Smod[S_i]
  Smod[S_i] += 1
  prevmod = S_i
  k = (k * 10) % 2019
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
S = input()
ans = 0
for i in range(len(S)):
  for j in range(i+1, len(S)):
    S_n = S[i:j+1]
    if(int(S_n) % 2019 == 0):
      ans += 1
      
print(ans)

Fixed submission:
S = input()
Smod = [0] * 2019
Smod[0] = 1
ans = 0
S_i = 0
prevmod = 0
k = 1
for i in range(len(S)-1, -1, -1):
  S_i = (int(S[i]) * k + prevmod) % 2019
  ans += Smod[S_i]
  Smod[S_i] += 1
  prevmod = S_i
  k = (k * 10) % 2019
print(ans)