Buggy python submission (verdict: Time Limit Exceeded):
S = input()
n = len(S)

cnt = [0] * 2019
a = int(S[n-1])
cnt[a%2019] += 1

for i in S[n-1::-1]:
  a = int(i+str(a))
  mod = a%2019
  cnt[mod] += 1

ans=cnt[0]
for i in cnt:
  ans += int(i*(i-1)/2)

print(ans)

Fixed submission:
S = input()[::-1]
n = len(S)

cnt = [0] * 2019
cnt[0] = 1
m = 0
d = 1
for i in S:
  m += int(i) * d
  m %= 2019
  cnt[m] += 1
  d *= 10
  d %= 2019

ans=0
for i in cnt:
  ans += int(i*(i-1)/2)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
S = input().strip()
n = len(S)

A=[]
n = 0
for i in range(len(S)):
  if int(S[i:])%2019!=0:
    A.append(int(S[i:])%2019)
  else:
    n += 1

from collections import Counter
cnt = Counter(A)
for v in cnt.values():
    n += v*(v-1)/2
print(int(n))


Fixed submission:
S = input()[::-1]
n = len(S)

cnt = [0] * 2019
cnt[0] = 1
m = 0
d = 1
for i in S:
  m += int(i) * d
  m %= 2019
  cnt[m] += 1
  d *= 10
  d %= 2019

ans=0
for i in cnt:
  ans += int(i*(i-1)/2)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
S = input()
n = len(S)

A=[]
for i in range(len(S)):
  A.append(int(S[i:])%2019)

B=set(A)
b=0
for i in B:
  if i!=0:
    a = A.count(i)
    b += a*(a-1)/2
  else:
    b += 1

print(int(b))

Fixed submission:
S = input()[::-1]
n = len(S)

cnt = [0] * 2019
cnt[0] = 1
m = 0
d = 1
for i in S:
  m += int(i) * d
  m %= 2019
  cnt[m] += 1
  d *= 10
  d %= 2019

ans=0
for i in cnt:
  ans += int(i*(i-1)/2)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
S = input()

L = 0
length = len(S)+1
for i in range(length):
  for j in range(length):
    if i<j:
      if int(S[i:j])>2018:
        if int(S[i:j])%2019==0:
          L += 1
print(L)

Fixed submission:
S = input()[::-1]
n = len(S)

cnt = [0] * 2019
cnt[0] = 1
m = 0
d = 1
for i in S:
  m += int(i) * d
  m %= 2019
  cnt[m] += 1
  d *= 10
  d %= 2019

ans=0
for i in cnt:
  ans += int(i*(i-1)/2)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
S = input()

L = 0
for i in range(len(S)+1):
  for j in range(len(S)+1):
    if i<j:
      if int(S[i:j])>2018:
        if int(S[i:j])%2019==0:
          L += 1
print(L)

Fixed submission:
S = input()[::-1]
n = len(S)

cnt = [0] * 2019
cnt[0] = 1
m = 0
d = 1
for i in S:
  m += int(i) * d
  m %= 2019
  cnt[m] += 1
  d *= 10
  d %= 2019

ans=0
for i in cnt:
  ans += int(i*(i-1)/2)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
S = input()

L = []
for i in range(len(S)+1):
  for j in range(len(S)+1):
    if i<j:
      if int(S[i:j])>2018:
        L.append(S[i:j])

B = list(map(int,L))
A = list(filter(lambda x:x%2019==0,B))
print(len(A))



Fixed submission:
S = input()[::-1]
n = len(S)

cnt = [0] * 2019
cnt[0] = 1
m = 0
d = 1
for i in S:
  m += int(i) * d
  m %= 2019
  cnt[m] += 1
  d *= 10
  d %= 2019

ans=0
for i in cnt:
  ans += int(i*(i-1)/2)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
S = input()

L = []
for i in range(len(S)+1):
  for j in range(len(S)+1):
    if i<j:
      L.append(S[i:j])
B = list(map(int,L))
A = list(filter(lambda x:x%2019==0,B))
print(len(A))

Fixed submission:
S = input()[::-1]
n = len(S)

cnt = [0] * 2019
cnt[0] = 1
m = 0
d = 1
for i in S:
  m += int(i) * d
  m %= 2019
  cnt[m] += 1
  d *= 10
  d %= 2019

ans=0
for i in cnt:
  ans += int(i*(i-1)/2)
print(ans)