Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
#intの場合
N = int(input()) #ここで標準入力

#List にしたい時
#intの場合
huga = list(map(int, input().split()))
ans = 1
for number in huga:
    ans = ans * number


if ans > 10**18:
    print('-1')
else:
    print(ans)


Fixed submission:
#intの場合
N = int(input()) #ここで標準入力

#List にしたい時
#intの場合
huga = list(map(int, input().split()))
if 0 in huga:
    print(0)
else:
    ans = 1
    flg = 0
    for number in huga:
        ans = ans * number
        if ans > 10**18:
            flg = 1
            break
    if flg == 0:
        print(ans)
    else:
        print('-1')

Buggy python submission (verdict: Time Limit Exceeded):
#intの場合
N = int(input()) #ここで標準入力

#List にしたい時
#intの場合
huga = list(map(int, input().split()))
ans = 1
for number in huga:
    ans = ans * number
    

if ans > 10**18:
    print(-1)
else:
    print(ans)


Fixed submission:
#intの場合
N = int(input()) #ここで標準入力

#List にしたい時
#intの場合
huga = list(map(int, input().split()))
if 0 in huga:
    print(0)
else:
    ans = 1
    flg = 0
    for number in huga:
        ans = ans * number
        if ans > 10**18:
            flg = 1
            break
    if flg == 0:
        print(ans)
    else:
        print('-1')

Buggy python submission (verdict: Time Limit Exceeded):
#intの場合
N = int(input()) #ここで標準入力

#List にしたい時
#intの場合
huga = list(map(int, input().split()))
ans = 1
for number in huga:
    ans = ans * number

if ans > 10**18:
    print(-1)
else:
    print(ans)

Fixed submission:
#intの場合
N = int(input()) #ここで標準入力

#List にしたい時
#intの場合
huga = list(map(int, input().split()))
if 0 in huga:
    print(0)
else:
    ans = 1
    flg = 0
    for number in huga:
        ans = ans * number
        if ans > 10**18:
            flg = 1
            break
    if flg == 0:
        print(ans)
    else:
        print('-1')

Buggy python submission (verdict: Runtime Error):
#intの場合
N = int(input()) #ここで標準入力
print (type(N),N) 
#<class 'int'> 20

#List にしたい時
#intの場合
huga = list(map(int, input().split()))
print(type(huga),huga)
# <class 'list'> [12, 34]
print(type(huga[0]),huga[0])
# <class 'int'> 12

ans = 1

for int i in huga:
    ans = ans * i

print(ans)

Fixed submission:
#intの場合
N = int(input()) #ここで標準入力

#List にしたい時
#intの場合
huga = list(map(int, input().split()))
if 0 in huga:
    print(0)
else:
    ans = 1
    flg = 0
    for number in huga:
        ans = ans * number
        if ans > 10**18:
            flg = 1
            break
    if flg == 0:
        print(ans)
    else:
        print('-1')
Buggy python submission (verdict: Runtime Error):
S = input()
rev = S[::-1]

l = int(len(S)/2)
front = S[0:l]
back = rev[0:l]

cnt = 0
moji = 0

for s in front:
    if front[cnt] == back[cnt]:
        moji += 1
    cnt += 1

ans = len - moji
print(ans)

Fixed submission:
S = input() #ここで標準入力
rev = S[::-1]

l = int(len(S)/2)
front = S[0:l]
back = rev[0:l]

cnt = 0
moji = 0

for s in front:
    if front[cnt] == back[cnt]:
        moji += 1
    cnt += 1

ans = l - moji
print(ans)
Buggy python submission (verdict: Runtime Error):
n, k = map(int, input().split())
ans = []
for _ in range(k):
    d = list(int(input()))
    a = list(map(int, input().split()))
    ans += a
print(n-len(set(ans)))

Fixed submission:
n, k = map(int, input().split())
ans = []
for _ in range(k):
    d = int(input())
    a = list(map(int, input().split()))
    ans += a
print(n-len(set(ans)))
Buggy python submission (verdict: Runtime Error):
n, m = map(int, input().split())
# 高さ入力
h = list(int, input())
a = [0]*m
b = [0]*m
for i in range(m):
    a[i], b[i] = map(int, input().split())
list = []
for i in range(m):
    if h[a[i]-1] > h[b[i]-1]:
        list.append(a[i])
    elif h[a[i]-1] == h[b[i]-1]:
        list.append(a[i])
        list.append(b[i])
    else:
        list.append(b[i])
# 答えを記録する用配列
listans = [0]*n
for i in range(len(list)):
    listans[list[i] - 1] += 1
ans = 0
for i in range(n):
    if listans[i] == 0:
        ans += 1
print(ans)

Fixed submission:
n, m = [int(i) for i in input().split()]
# 高さ入力
h = [int(i) for i in input().split()]
# 条件を満たす地点を記録
list = [[]for i in range(n)]
for i in range(m):
    a, b = [int(i) for i in input().split()]
    list[a-1].append(h[b-1])
    list[b-1].append(h[a-1])
ans = 0
for i in range(n):
    if len(list[i]) == 0:
        ans += 1
    elif h[i] > max(list[i]):
        ans += 1
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
def pow_r(x, n):
    if n == 0: 
        return 1
    if n % 2 == 0: 
        return pow_r(x ** 2, n // 2)
    else: 
        return x * pow_r(x ** 2, (n - 1) // 2)


x = int(input())
for a in range(-1000, 1000):
    for b in range(-1000, 1000):
        if x == (pow_r(a, 5)) - (pow_r(b, 5)):
            print(a, b)
            exit()



Fixed submission:
def pow_r(x, n):
    if n == 0:
        return 1
    if n % 2 == 0:
        return pow_r(x ** 2, n // 2)
    else:
        return x * pow_r(x ** 2, (n - 1) // 2)


x = int(input())
for a in range(-100, 1000):
    for b in range(-100, 1000):
        if x == (pow_r(a, 5)) - (pow_r(b, 5)):
            print(a, b)
            exit()

Buggy python submission (verdict: Time Limit Exceeded):
def pow_r(x, n):
    if n == 0:  # exit case
        return 1
    if n % 2 == 0:  # standard case ① n is even
        return pow_r(x ** 2, n // 2)
    else:  # standard case ② n is odd
        return x * pow_r(x ** 2, (n - 1) // 2)


x = int(input())
for a in range(-1000, 1000):
    for b in range(-1000, 1000):
        if x == (pow_r(a, 5)) - (pow_r(b, 5)):
            print(a, b)
            exit()



Fixed submission:
def pow_r(x, n):
    if n == 0:
        return 1
    if n % 2 == 0:
        return pow_r(x ** 2, n // 2)
    else:
        return x * pow_r(x ** 2, (n - 1) // 2)


x = int(input())
for a in range(-100, 1000):
    for b in range(-100, 1000):
        if x == (pow_r(a, 5)) - (pow_r(b, 5)):
            print(a, b)
            exit()
Buggy python submission (verdict: Runtime Error):
# ここから入力
from itertools import combinations_with_replacement
n, m, q = map(int, input().split())
lst = []
for i in range(q):
    a = list(map(int, input().split()))
    lst.append(a)
# ここまで入力
x = 0
# 重複組合せ
for i in combinations_with_replacement(range(1, m + 1), n):
    s = 0
    for j in a:
        if i[j[1] - 1] - i[j[0] - 1] == j[2]:
            s += j[3]
    if x < s:
        x = s
print(x)

Fixed submission:
# ここから入力
from itertools import combinations_with_replacement
from itertools import accumulate
n, m, q = map(int, input().split())
a = []
for i in range(q):
    a.append(list(map(int, input().split())))
# ここまで入力
# 重複組合せ
x = 0
for i in combinations_with_replacement(range(1, m + 1), n):
    ans = []
    for j in a:
        if i[j[1] - 1] - i[j[0] - 1] == j[2]:
            ans.append(j[3])
    if x < sum(ans):
        x = sum(ans)
print(x)
Buggy python submission (verdict: Runtime Error):
from collections import Counter
n = int(input())
s = [input() for i in range(n)]
c = Counter(s)
c = list(s.items())
print(len(c))

Fixed submission:
from collections import Counter
n = int(input())
s = [input() for i in range(n)]
c = Counter(s)
c = list(c.items())
print(len(c))

Buggy python submission (verdict: Runtime Error):
import collections
n = int(input())
for i in range(n):
    a = int(input())
  b=collections.Counter(a)
  print(len(b))


Fixed submission:
from collections import Counter
n = int(input())
s = [input() for i in range(n)]
c = Counter(s)
c = list(c.items())
print(len(c))
Buggy python submission (verdict: Runtime Error):
s = list(input())
n = len(s)
ans = 1
# 区間は2以上の長さ
if n % 2 != 0:
    for i in range(1, n):
        a = s[n // 2]
        if s[n // 2 - 1 - i] != a or s[n // 2 + i] != a:
            print(n // 2 + i)
            exit()
else:
    for i in range(1, (n - 1) // 2 + 1):
        b = s[(n-1)//2]
        if s[(n - 1) // 2 - i] != b or s[(n - 1) // 2 + i] != b:
            print((n-1)//2+i)
            exit()
print(n)

Fixed submission:
s = list(input())
n = len(s)
if n % 2 == 0:
    for i in range(n//2):
        a = s[n // 2]
        if s[n // 2 - 1 - i] != a or s[n // 2 + i] != a:
            print(n // 2 + i)
            exit()
else:
    for i in range(1, (n - 1) // 2 + 1):
        b = s[(n-1)//2]
        if s[(n - 1) // 2 - i] != b or s[(n - 1) // 2 + i] != b:
            print((n-1)//2+i)
            exit()
print(n)

Buggy python submission (verdict: Runtime Error):
s = input()
n = len(s)
# 区間は2以上の長さ
if n % 2 != 0:
    for i in range(1, n):
        a = s[n // 2]
        if s[n // 2 - 1 - i] != a or s[n // 2 + i] != a:
            print(n // 2 + i)
            exit()
else:
    for i in range(1, (n - 1) // 2 + 1):
        b = s[(n-1)//2]
        if s[(n-1)//2-i] != b or s[(n-1)//2+i] != b:
            print((n - 1) / 2 + i)
            exit()
print(n)

Fixed submission:
s = list(input())
n = len(s)
if n % 2 == 0:
    for i in range(n//2):
        a = s[n // 2]
        if s[n // 2 - 1 - i] != a or s[n // 2 + i] != a:
            print(n // 2 + i)
            exit()
else:
    for i in range(1, (n - 1) // 2 + 1):
        b = s[(n-1)//2]
        if s[(n - 1) // 2 - i] != b or s[(n - 1) // 2 + i] != b:
            print((n-1)//2+i)
            exit()
print(n)

Buggy python submission (verdict: Runtime Error):
s = input()
n = len(s)
ans = 1
# 区間は2以上の長さ
if n % 2 != 0:
    for i in range(1, n):
        a = s[n // 2]
        if s[n // 2 - 1 - i] != a or s[n // 2 + i] != a:
            print(n // 2 + i)
            exit()

else:
    for i in range(1, (n - 1) // 2 + 1):
        b = s[(n-1)//2]
        if s[(n-1)//2-i] != b or s[(n-1)//2+i] != b:
            print((n - 1) / 2 + i)
            exit()



Fixed submission:
s = list(input())
n = len(s)
if n % 2 == 0:
    for i in range(n//2):
        a = s[n // 2]
        if s[n // 2 - 1 - i] != a or s[n // 2 + i] != a:
            print(n // 2 + i)
            exit()
else:
    for i in range(1, (n - 1) // 2 + 1):
        b = s[(n-1)//2]
        if s[(n - 1) // 2 - i] != b or s[(n - 1) // 2 + i] != b:
            print((n-1)//2+i)
            exit()
print(n)
Buggy python submission (verdict: Runtime Error):
k = int(input())
s = input()
if len(s) =< k:
    print(s)
else:
    s1 = s[0:k]
    s1 = s1 + '...'
    print(s1)

Fixed submission:
k = int(input())
s = input()
if len(s) <= k:
    print(s)
else:
    s1 = s[0:k]
    s1 = s1 + '...'
    print(s1)
Buggy python submission (verdict: Runtime Error):
n,m = map(int,input().split())
h = [int(x) for x in input().split()]
a = [0] * m
b = [0] * m
for i in range(m):
    a[i],b[i] = map(int,input().split())
ind = []
for i in range(m):
    if h[a[i]-1] < h[b[i]-1]:
        ind.append(a[i])
    elif h[a[i]-1] == h[b[i]-1]:
        ind.append(a[i])
        ind.append(b[i])
    else:
        ind.append(b[i])
ind2 = [0] * n
for i in range(n):
    ind2[ind[i]-1] += 1
ans = 0
for i in range(n):
    if ind2[i] == 0:
        ans += 1
print(ans)

Fixed submission:
n,m = map(int,input().split())
h = [int(x) for x in input().split()]
a = [0] * m
b = [0] * m
for i in range(m):
    a[i],b[i] = map(int,input().split())
ind = []
for i in range(m):
    if h[a[i]-1] < h[b[i]-1]:
        ind.append(a[i])
    elif h[a[i]-1] == h[b[i]-1]:
        ind.append(a[i])
        ind.append(b[i])
    else:
        ind.append(b[i])
ind2 = [0] * n
for i in range(len(ind)):
    ind2[ind[i]-1] += 1
ans = 0
for i in range(n):
    if ind2[i] == 0:
        ans += 1
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
a = int(input())
b = list(map(int,input().split()))
for i in range(a):
    c = 0
    for num in range(len(b)):
        if i+1 == b[num]:
           c = c + 1
    print(c)
    



Fixed submission:
a = int(input())
b = list(map(int,input().split()))
c = [0]*a
for i in b:
    c[i-1] += 1
for i in c:
    print(i)
Buggy python submission (verdict: Time Limit Exceeded):
def gcd(x,y):
    if x <= y:
        z = x
        x = y
        y = z
    if y == 0:
        return x
    else:
        return gcd(y,x%y)

n = int(input())
s = 0
for i in range(1,n+1):
    for j in range(1,n+1):
        a = gcd(i,j)
        for k in range(1,n+1):
            b = gcd(a,k)
            s += b
print(s)



Fixed submission:
import math
n = int(input())
s = 0
for i in range(1,n+1):
    for j in range(1,n+1):
        a = math.gcd(i,j)
        for k in range(1,n+1):
            b = math.gcd(a,k)
            s += b
print(s)

Buggy python submission (verdict: Time Limit Exceeded):
def gcd(x,y):
    if x <= y:
        z = x
        x = y
        y = z
    if y == 0:
        return x
    else:
        return gcd(y,x%y)

n = int(input())
s = 0
for i in range(n):
    for j in range(n):
        for k in range(n):
            a = gcd(i+1,j+1)
            b = gcd(a,k+1)
            s += b
print(s)



Fixed submission:
import math
n = int(input())
s = 0
for i in range(1,n+1):
    for j in range(1,n+1):
        a = math.gcd(i,j)
        for k in range(1,n+1):
            b = math.gcd(a,k)
            s += b
print(s)
Buggy python submission (verdict: Time Limit Exceeded):
from sys import stdin
n = int(input())
s = stdin.readline()
rind = []
gind = []
bind = []
ans = 0
for i in range(n):
    if s[i] == "R":
        rind.append(i+1)
    elif s[i] == "G":
        gind.append(i+1)
    else:
        bind.append(i+1)
for i in range(len(rind)):
    for j in range(len(gind)):
        for k in range(len(bind)):
            if (rind[i]+gind[j]!=2*bind[k])and(gind[j]+bind[k]!=2*rind[i])and(bind[k]+rind[i]!=2*gind[j]):
                ans += 1
print(ans)


Fixed submission:
from sys import stdin
n = int(input())
s = stdin.readline()
r = 0
g = 0 
b = 0
for i in range(n):
    if s[i] == "R":
        r += 1
    elif s[i] == "G":
        g += 1
    else:
        b += 1
count1 = r*g*b
count2 = 0
for i in range(0,n-2):
    for j in range(i+1,n-1):
        if 2*j-i<=n-1:
            if (s[i] != s[j])and(s[i] != s[2*j-i])and(s[j] != s[2*j-i]):
               count2 += 1
print(count1-count2)

Buggy python submission (verdict: Runtime Error):
from sys import stdin
n = int(input())
s = stdin.readline()
rind = []
gind = []
bind = []
ans = 0
for i in range(n):
    if s[i] == "R":
        rind.append(i+1)
    elif s[i] == "G":
        gind.append(i+1)
    else:
        bind.append(i+1)
for i in range(len(rind)):
    for j in range(len(gind)):
        for k in range(len(bind)):
            if (rind[i]+gind[j]!=2*bind[k])and(gind[j]+bind[k]!=rind[i])and(bind[k]+rind[i]!=gin[j]):
                ans += 1
print(ans)


Fixed submission:
from sys import stdin
n = int(input())
s = stdin.readline()
r = 0
g = 0 
b = 0
for i in range(n):
    if s[i] == "R":
        r += 1
    elif s[i] == "G":
        g += 1
    else:
        b += 1
count1 = r*g*b
count2 = 0
for i in range(0,n-2):
    for j in range(i+1,n-1):
        if 2*j-i<=n-1:
            if (s[i] != s[j])and(s[i] != s[2*j-i])and(s[j] != s[2*j-i]):
               count2 += 1
print(count1-count2)

Buggy python submission (verdict: Time Limit Exceeded):
from sys import stdin
n = int(input())
s = stdin.readline()
rind = []
gind = []
bind = []
ans = 0
for i in range(n):
    if s[i] == "R":
        rind.append(i+1)
    elif s[i] == "G":
        gind.append(i+1)
    else:
        bind.append(i+1)
for i in range(len(rind)):
    for j in range(len(gind)):
        for k in range(len(bind)):
            ind = [rind[i],gind[j],bind[k]]
            a = sorted(ind)
            if a[0] + a[2] != 2*a[1]:
                ans += 1
print(ans)


Fixed submission:
from sys import stdin
n = int(input())
s = stdin.readline()
r = 0
g = 0 
b = 0
for i in range(n):
    if s[i] == "R":
        r += 1
    elif s[i] == "G":
        g += 1
    else:
        b += 1
count1 = r*g*b
count2 = 0
for i in range(0,n-2):
    for j in range(i+1,n-1):
        if 2*j-i<=n-1:
            if (s[i] != s[j])and(s[i] != s[2*j-i])and(s[j] != s[2*j-i]):
               count2 += 1
print(count1-count2)
Buggy python submission (verdict: Time Limit Exceeded):
n,k = map(int,input().split())
while n >= abs(n-k):
    n = abs(n-k)
print(n)

Fixed submission:
n,k = map(int,input().split())
if n > k:
    a = n // k
    n = n - a * k
while n >= abs(n-k):
    n = abs(n-k)
print(n)
Buggy python submission (verdict: Runtime Error):
a, b = int(input()).split()

print(a*b)

Fixed submission:
a, b = input().split()

print(int(a)*int(b))

Buggy python submission (verdict: Runtime Error):
a, b = int(input())

print(a*b)

Fixed submission:
a, b = input().split()

print(int(a)*int(b))

Buggy python submission (verdict: Runtime Error):
A = int(input())
B = int(input())
print(A*B)


Fixed submission:
A, B = map(int, input().split())
print(A*B)
Buggy python submission (verdict: Time Limit Exceeded):
import itertools

N = int(input())
A = list(map(int, input().split()))
# L = [(i, i + v, v - i) for i, v in enumerate(A)]
Ai = dict()
Aj = dict()
for i, v in enumerate(A):
    ans = 0
    ai = i + v
    aj = i - v
    if ai in Ai:
        Ai[ai].append(i)
    else:
        Ai[ai] = [i]
    if aj in Aj:
        Aj[aj].append(i)
    else:
        Aj[aj] = [i]
ans = 0
intersection_keys = Ai.keys() & Aj.keys()
for key in intersection_keys:
    L = itertools.product(Ai[key], Aj[key])
    ans += len([True for i, j in L if j > i])

print(ans)

Fixed submission:
N = int(input())
A = list(map(int, input().split()))

Ai = dict()
ans = 0
for i, v in enumerate(A):
    ai = i + v
    if ai in Ai:
        Ai[ai].append(i)
    else:
        Ai[ai] = [i]
for i, v in enumerate(A):
    aj = i - v
    if aj in Ai:
        ans += len(Ai[aj])
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = list(map(int, input().split()))
# L = [(i, i + v, v - i) for i, v in enumerate(A)]
Ai = dict()
Aj = dict()
for i, v in enumerate(A):
    ans = 0
    ai = i + v
    aj = i - v
    if ai in Ai:
        Ai[ai].append(i)
    else:
        Ai[ai] = [i]
    if aj in Aj:
        Aj[aj].append(i)
    else:
        Aj[aj] = [i]
ans = 0
intersection_keys = Ai.keys() & Aj.keys()
for key in intersection_keys:
    Ai_list = Ai[key]
    Aj_list = Aj[key]
    for i in Ai_list:
        ans += len([j for j in Aj_list if j > i])

print(ans)

Fixed submission:
N = int(input())
A = list(map(int, input().split()))

Ai = dict()
ans = 0
for i, v in enumerate(A):
    ai = i + v
    if ai in Ai:
        Ai[ai].append(i)
    else:
        Ai[ai] = [i]
for i, v in enumerate(A):
    aj = i - v
    if aj in Ai:
        ans += len(Ai[aj])
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = list(map(int, input().split()))
ans = 0
for i in range(N-1):
    I = A[i]
    for j in range(i + A[i], N):
        if abs(i - j) == A[i] + A[j]:
            ans += 1
print(ans)

Fixed submission:
N = int(input())
A = list(map(int, input().split()))

Ai = dict()
ans = 0
for i, v in enumerate(A):
    ai = i + v
    if ai in Ai:
        Ai[ai].append(i)
    else:
        Ai[ai] = [i]
for i, v in enumerate(A):
    aj = i - v
    if aj in Ai:
        ans += len(Ai[aj])
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = list(map(int, input().split()))
ans = 0
for i in range(N-1):
    for j in range(i+1, N):
        if abs(i - j) == A[i] + A[j]:
            ans += 1
print(ans)

Fixed submission:
N = int(input())
A = list(map(int, input().split()))

Ai = dict()
ans = 0
for i, v in enumerate(A):
    ai = i + v
    if ai in Ai:
        Ai[ai].append(i)
    else:
        Ai[ai] = [i]
for i, v in enumerate(A):
    aj = i - v
    if aj in Ai:
        ans += len(Ai[aj])
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
S = input()[::-1]
ans = 0
dp = [0] * 2019
dp[0] = 1
val = ''
m = 0
x_10 = 1
for s in S:
    m = (m + int(s) * x_10) % 2019
    dp[m] += 1
    x_10 *= 10
for i in dp:
    ans += i * (i-1) // 2
print(ans)


Fixed submission:
S = input()[::-1]
ans = 0
dp = [0] * 2019
dp[0] = 1
val = ''
m = 0
x_10 = 1
for s in S:
    m = (m + int(s) * x_10) % 2019
    dp[m] += 1
    x_10 = (x_10 * 10) % 2019
for i in dp:
    ans += i * (i-1) // 2
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
S = input()[::-1]
S_len = len(S)  # <= 200000
ans = 0
dp = [0] * 2019
dp[0] = 1
val = ''
for s in S:
    val = s + val
    dp[int(val) % 2019] += 1
for i in dp:
    ans += i * (i-1) // 2
print(ans)


Fixed submission:
S = input()[::-1]
ans = 0
dp = [0] * 2019
dp[0] = 1
val = ''
m = 0
x_10 = 1
for s in S:
    m = (m + int(s) * x_10) % 2019
    dp[m] += 1
    x_10 = (x_10 * 10) % 2019
for i in dp:
    ans += i * (i-1) // 2
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
S = input()[::-1]
S_len = len(S)  # <= 200000
ans = 0
dp = [0] * 2019
dp[0] = 1
for i in range(1, S_len+1):
    dp[int(S[:i][::-1]) % 2019] += 1
for i in dp:
    ans += i * (i-1) // 2
print(ans)


Fixed submission:
S = input()[::-1]
ans = 0
dp = [0] * 2019
dp[0] = 1
val = ''
m = 0
x_10 = 1
for s in S:
    m = (m + int(s) * x_10) % 2019
    dp[m] += 1
    x_10 = (x_10 * 10) % 2019
for i in dp:
    ans += i * (i-1) // 2
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
S = input()[::-1]
S_len = len(S)  # <= 200000
ans = 0
for i in range(S_len+1):
    tmp = S[i:i+3]
    j = i + 3
    while j < S_len:
        tmp += S[j]
        if int(tmp[::-1]) % 2019 == 0:
            ans += 1
        j += 1

print(ans)


Fixed submission:
S = input()[::-1]
ans = 0
dp = [0] * 2019
dp[0] = 1
val = ''
m = 0
x_10 = 1
for s in S:
    m = (m + int(s) * x_10) % 2019
    dp[m] += 1
    x_10 = (x_10 * 10) % 2019
for i in dp:
    ans += i * (i-1) // 2
print(ans)