Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict
S = input()[::-1]
mod = 2019
d = defaultdict(int)
s = ""
for i in range(len(S)):
    s = S[i] + s
    d[int(s) % mod] += 1
ans = 0
for k, v in d.items():
    ans += v * (v - 1) // 2
    if k == 0:
        ans += v
print(ans)

Fixed submission:
import collections
S = input()[::-1]
N = len(S)
mod = 2019

ans = 0
x = [0] * N
x[0] = int(S[0]) % mod
ruijo = 10

for i in range(1, N):
    x[i] = (ruijo * int(S[i]) + x[i - 1]) % mod
    if x[i] % mod == 0:
        ans += 1
    ruijo *= 10
    ruijo %= mod
    
d = collections.Counter(x)

for i in d.values():
    ans += i * (i - 1) // 2
    
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict
S = input()[::-1]
mod = 2019
d = defaultdict(int)
s = ""
for i in range(len(S)):
    s += S[i]
    d[int(s[::-1]) % mod] += 1
ans = 0
for k, v in d.items():
    ans += v * (v - 1) // 2
    if k == 0:
        ans += v
print(ans)

Fixed submission:
import collections
S = input()[::-1]
N = len(S)
mod = 2019

ans = 0
x = [0] * N
x[0] = int(S[0]) % mod
ruijo = 10

for i in range(1, N):
    x[i] = (ruijo * int(S[i]) + x[i - 1]) % mod
    if x[i] % mod == 0:
        ans += 1
    ruijo *= 10
    ruijo %= mod
    
d = collections.Counter(x)

for i in d.values():
    ans += i * (i - 1) // 2
    
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict
S = input()[::-1]
mod = 2019
d = defaultdict(int)
s = ""
for i in range(len(S)):
    s = S[i] + s
    d[int(s) % mod] += 1
ans = 0
for k, v in d.items():
    ans += v * (v - 1) // 2
    if k == 0:
        ans += v
print(ans)

Fixed submission:
import collections
S = input()[::-1]
N = len(S)
mod = 2019

ans = 0
x = [0] * N
x[0] = int(S[0]) % mod
ruijo = 10

for i in range(1, N):
    x[i] = (ruijo * int(S[i]) + x[i - 1]) % mod
    if x[i] % mod == 0:
        ans += 1
    ruijo *= 10
    ruijo %= mod
    
d = collections.Counter(x)

for i in d.values():
    ans += i * (i - 1) // 2
    
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import collections
S = input()[::-1]
N = len(S)
mod = 2019

ans = 0
x = [0] * N
x[0] = int(S[0]) % mod

for i in range(1, N):
    x[i] = ((10 ** i) * int(S[i]) + x[i - 1]) % mod
    if x[i] % mod == 0:
        ans += 1
    
d = collections.Counter(x)

for i in d.values():
    ans += i * (i - 1) // 2
    
print(ans)

Fixed submission:
import collections
S = input()[::-1]
N = len(S)
mod = 2019

ans = 0
x = [0] * N
x[0] = int(S[0]) % mod
ruijo = 10

for i in range(1, N):
    x[i] = (ruijo * int(S[i]) + x[i - 1]) % mod
    if x[i] % mod == 0:
        ans += 1
    ruijo *= 10
    ruijo %= mod
    
d = collections.Counter(x)

for i in d.values():
    ans += i * (i - 1) // 2
    
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import collections
S = input()[::-1]
N = len(S)
mod = 2019

x = [0] * N
x[0] = int(S[0]) % mod

for i in range(1, N):
    x[i] = ((10 ** i) * int(S[i]) + x[i - 1]) % mod

d = collections.Counter(x)
target = [v for k, v in d.items() if v > 1]

ans = 0
ans += sum([t * (t - 1) // 2 for t in target])

if 0 in d.keys():
    ans += d[0] * (d[0] - 1) // 2 + d[0]
    
print(ans)

Fixed submission:
import collections
S = input()[::-1]
N = len(S)
mod = 2019

ans = 0
x = [0] * N
x[0] = int(S[0]) % mod
ruijo = 10

for i in range(1, N):
    x[i] = (ruijo * int(S[i]) + x[i - 1]) % mod
    if x[i] % mod == 0:
        ans += 1
    ruijo *= 10
    ruijo %= mod
    
d = collections.Counter(x)

for i in d.values():
    ans += i * (i - 1) // 2
    
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
S = input()
N = len(S)
ans = 0
for i in range(0, N - 3):
    j = i + 4
    while j <= N:
        if int(S[i: j]) % 2019 == 0:
            ans += 1
            break
        else:
           j += 1
print(ans)

Fixed submission:
import collections
S = input()[::-1]
N = len(S)
mod = 2019

ans = 0
x = [0] * N
x[0] = int(S[0]) % mod
ruijo = 10

for i in range(1, N):
    x[i] = (ruijo * int(S[i]) + x[i - 1]) % mod
    if x[i] % mod == 0:
        ans += 1
    ruijo *= 10
    ruijo %= mod
    
d = collections.Counter(x)

for i in d.values():
    ans += i * (i - 1) // 2
    
print(ans)

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

Fixed submission:
import collections
S = input()[::-1]
N = len(S)
mod = 2019

ans = 0
x = [0] * N
x[0] = int(S[0]) % mod
ruijo = 10

for i in range(1, N):
    x[i] = (ruijo * int(S[i]) + x[i - 1]) % mod
    if x[i] % mod == 0:
        ans += 1
    ruijo *= 10
    ruijo %= mod
    
d = collections.Counter(x)

for i in d.values():
    ans += i * (i - 1) // 2
    
print(ans)