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

remainder=[0]*2019
s=''
ans=0
for i in range(N-1,-1,-1):
  s=S[i]+s
  T=int(s)
  n=T%2019
  remainder[n]+=1
  if remainder[n]!=1:
    ans+=remainder[n]-1
ans+=remainder[0]
print(ans)

Fixed submission:
S=input()
N=len(S)
M=2019

remainder=[0]*M
val=0
for i in range(N):
  val=(int(S[N-1-i])*pow(10,i,M)+val)%M
  remainder[val]+=1

ans=remainder[0]
for i in range(2019):
  ans+=remainder[i]*(remainder[i]-1)//2
print(ans)

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

remainder=[0]*2019
s=''
for i in range(N-1,-1,-1):
  s=S[i]+s
  T=int(s)
  n=T%2019
  remainder[n]+=1

ans=remainder[0]
for i in range(2019):
  ans+=remainder[i]*(remainder[i]-1)//2
print(ans)

Fixed submission:
S=input()
N=len(S)
M=2019

remainder=[0]*M
val=0
for i in range(N):
  val=(int(S[N-1-i])*pow(10,i,M)+val)%M
  remainder[val]+=1

ans=remainder[0]
for i in range(2019):
  ans+=remainder[i]*(remainder[i]-1)//2
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
S=list(map(int,list(input())))
N=len(S)

remainder=[0]*2019
val=0
ans=0
for i in range(1,N+1):
  val+=S[-i]*(10**(i-1))
  n=val%2019
  remainder[n]+=1
  if remainder[n]!=1:
    ans+=remainder[n]-1

ans+=remainder[0]
print(ans)

Fixed submission:
S=input()
N=len(S)
M=2019

remainder=[0]*M
val=0
for i in range(N):
  val=(int(S[N-1-i])*pow(10,i,M)+val)%M
  remainder[val]+=1

ans=remainder[0]
for i in range(2019):
  ans+=remainder[i]*(remainder[i]-1)//2
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
S=list(map(int,list(input())))
N=len(S)

remainder=[0]*2019
val=0
for i in range(1,N+1):
  val+=S[-i]*(10**(i-1))
  remainder[val%2019]+=1

ans=remainder[0]
for i in range(2019):
  if remainder[i]<=1:
    continue
  else:
    ans+=remainder[i]*(remainder[i]-1)//2
print(ans)

Fixed submission:
S=input()
N=len(S)
M=2019

remainder=[0]*M
val=0
for i in range(N):
  val=(int(S[N-1-i])*pow(10,i,M)+val)%M
  remainder[val]+=1

ans=remainder[0]
for i in range(2019):
  ans+=remainder[i]*(remainder[i]-1)//2
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
S=list(map(int,list(input())))
N=len(S)

remainder=[0]*2019
s=[0]*N
for i in range(1,N+1):
  s[i-1]=S[-i]*(10**(i-1))

val=0
for i in range(N):
  val+=s[i]
  remainder[val%2019]+=1

ans=remainder[0]
for i in range(2019):
  if remainder[i]<=1:
    continue
  else:
    ans+=remainder[i]*(remainder[i]-1)//2
print(ans)

Fixed submission:
S=input()
N=len(S)
M=2019

remainder=[0]*M
val=0
for i in range(N):
  val=(int(S[N-1-i])*pow(10,i,M)+val)%M
  remainder[val]+=1

ans=remainder[0]
for i in range(2019):
  ans+=remainder[i]*(remainder[i]-1)//2
print(ans)

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

remainder=[0]*2019
S=list(map(int,list(S)))
S=S[::-1]
for i in range(N):
  S[i]=S[i]*(10**i)

val=0
for i in range(N):
  val+=S[i]
  remainder[val%2019]+=1

ans=remainder[0]
for i in range(2019):
  if remainder[i]<=1:
    continue
  else:
    ans+=remainder[i]*(remainder[i]-1)//2
print(ans)

Fixed submission:
S=input()
N=len(S)
M=2019

remainder=[0]*M
val=0
for i in range(N):
  val=(int(S[N-1-i])*pow(10,i,M)+val)%M
  remainder[val]+=1

ans=remainder[0]
for i in range(2019):
  ans+=remainder[i]*(remainder[i]-1)//2
print(ans)

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

remainder=[0]*2019
for i in range(1,N+1):
  s=S[-i:]
  s=int(s)
  remainder[s%2019]+=1

ans=remainder[0]
for i in range(2019):
  if remainder[i]<=1:
    continue
  else:
    ans+=remainder[i]*(remainder[i]-1)//2
print(ans)

Fixed submission:
S=input()
N=len(S)
M=2019

remainder=[0]*M
val=0
for i in range(N):
  val=(int(S[N-1-i])*pow(10,i,M)+val)%M
  remainder[val]+=1

ans=remainder[0]
for i in range(2019):
  ans+=remainder[i]*(remainder[i]-1)//2
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
def comb(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result

S=input()
N=len(S)

remainder=[0]*2019
for i in range(1,N+1):
  s=S[-i:]
  s=int(s)
  remainder[s%2019]+=1

ans=remainder[0]
for i in range(2019):
  if remainder[i]<=1:
    continue
  else:
    ans+=comb(remainder[i],2)
print(ans)

Fixed submission:
S=input()
N=len(S)
M=2019

remainder=[0]*M
val=0
for i in range(N):
  val=(int(S[N-1-i])*pow(10,i,M)+val)%M
  remainder[val]+=1

ans=remainder[0]
for i in range(2019):
  ans+=remainder[i]*(remainder[i]-1)//2
print(ans)

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

ans=0
for i in range(N-3):
    for j in range(i+4,N+1):
        s=S[i:j]
        s=int(s)
        if s%2019==0:
            ans+=1
print(ans)

Fixed submission:
S=input()
N=len(S)
M=2019

remainder=[0]*M
val=0
for i in range(N):
  val=(int(S[N-1-i])*pow(10,i,M)+val)%M
  remainder[val]+=1

ans=remainder[0]
for i in range(2019):
  ans+=remainder[i]*(remainder[i]-1)//2
print(ans)