Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict
d = defaultdict(int)
S=input()
S=S[::-1]
_=''
MOD=2019
for s in S:
  _=s+_
  d[int(_)%MOD]+=1
ans=0
for k in d.values():
  ans=ans+k*(k-1)//2
print(ans+d[0])

Fixed submission:
from collections import defaultdict
d = defaultdict(int)
S=input()
S=S[::-1]
i=1
x=0
MOD=2019
for s in S:
  x+=(int(s)*i)%MOD
  x%=MOD
  d[x]+=1
  i*=10
  i%=MOD
ans=0
for k in d.values():
  ans=ans+k*(k-1)//2
print(ans+d[0])

Buggy python submission (verdict: Time Limit Exceeded):
S=input()
N=len(S)
if N<4:
  print(0)
  exit(0)
M=2019
X=S
ans=0
x=[]
for l in range(N-3):
  for r in range(l+3,N):
    if int(X[l:r+1])%M==0:
      ans+=1
      x.append([l,r])
      break
def search(a,x,j):
  while j<len(x):
    if a[1]+1==x[j][0]:
      return 1+search(x[j],x,j+1)
    j+=1
  return 0
for i,a in enumerate(x):
  ans+=search(a,x,i+1)
print(ans)




Fixed submission:
from collections import defaultdict
d = defaultdict(int)
S=input()
S=S[::-1]
i=1
x=0
MOD=2019
for s in S:
  x+=(int(s)*i)%MOD
  x%=MOD
  d[x]+=1
  i*=10
  i%=MOD
ans=0
for k in d.values():
  ans=ans+k*(k-1)//2
print(ans+d[0])

Buggy python submission (verdict: Time Limit Exceeded):
S=input()
N=len(S)
if N<4:
  print(0)
  exit(0)
M=2019
X=[0]+[int(S[i])*pow(10,N-1-i)%M for i in range(N)]
import itertools
X = list(itertools.accumulate(X))
ans=0
x=[]
for l in range(N-2):
  for r in range(l+4,N+1):
    if (X[l]-X[r])%M==0:
      ans+=1
      x.append([l,r])
      break
def search(a,x,j):
  while j<len(x):
    if a[1]==x[j][0]:
      return 1+search(x[j][1],x,j+1)
    j+=1
  return 0
  
for i,a in enumerate(x):
  ans+=search(a,x,i+1)
print(ans)



Fixed submission:
from collections import defaultdict
d = defaultdict(int)
S=input()
S=S[::-1]
i=1
x=0
MOD=2019
for s in S:
  x+=(int(s)*i)%MOD
  x%=MOD
  d[x]+=1
  i*=10
  i%=MOD
ans=0
for k in d.values():
  ans=ans+k*(k-1)//2
print(ans+d[0])

Buggy python submission (verdict: Time Limit Exceeded):
S=input()
N=len(S)
if N<4:
  print(0)
  exit(0)
M=2019
X=[0]+[int(S[i])*pow(10,N-1-i)%M for i in range(N)]
import itertools
X = list(itertools.accumulate(X))
ans=0
x=[]
def search(l,r,X,N):
  _=0
  while r<N+1:
    if (X[l]-X[r])%M==0:
      _+=1
    r+=1
  return _
  
for l in range(N-2):
  if l in x:
    continue
  for r in range(l+4,N+1):
    if (X[l]-X[r])%M==0:
      x.append(r)
      _=search(r,r+4,X,N)
      ans+=(1+_+_)
      
print(ans)



Fixed submission:
from collections import defaultdict
d = defaultdict(int)
S=input()
S=S[::-1]
i=1
x=0
MOD=2019
for s in S:
  x+=(int(s)*i)%MOD
  x%=MOD
  d[x]+=1
  i*=10
  i%=MOD
ans=0
for k in d.values():
  ans=ans+k*(k-1)//2
print(ans+d[0])

Buggy python submission (verdict: Time Limit Exceeded):
S=input()
N=len(S)
if N<4:
  print(0)
  exit(0)
M=2019
ans=0
for l in range(N-3):
  for r in range(l+3,N):
    if int(S[l:r+1])%M==0:
      ans+=1
print(ans)

Fixed submission:
from collections import defaultdict
d = defaultdict(int)
S=input()
S=S[::-1]
i=1
x=0
MOD=2019
for s in S:
  x+=(int(s)*i)%MOD
  x%=MOD
  d[x]+=1
  i*=10
  i%=MOD
ans=0
for k in d.values():
  ans=ans+k*(k-1)//2
print(ans+d[0])

Buggy python submission (verdict: Time Limit Exceeded):
S=input()
N=len(S)
if N<4:
  print(0)
  exit(0)
M=2019
X=[0]+[int(S[i])*pow(10,N-1-i)%M for i in range(N)]
import itertools
X = list(itertools.accumulate(X))
ans=0
for l in range(N-2):
  for r in range(l+4,N+1):
    if (X[l]-X[r])%M==0:
      ans+=1
      
print(ans)

Fixed submission:
from collections import defaultdict
d = defaultdict(int)
S=input()
S=S[::-1]
i=1
x=0
MOD=2019
for s in S:
  x+=(int(s)*i)%MOD
  x%=MOD
  d[x]+=1
  i*=10
  i%=MOD
ans=0
for k in d.values():
  ans=ans+k*(k-1)//2
print(ans+d[0])