Buggy python submission (verdict: Time Limit Exceeded):
import os

import math
import sys

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7


# MOD = 998244353
def mod_invs(max, mod):
    """
    逆元 0, 1/1, 1/2, 1/3, ..., 1/max
    :param int max:
    :param int mod:
    """
    invs = [1] * (max + 1)
    invs[0] = 0
    for x in range(2, max + 1):
        invs[x] = (-(mod // x) * invs[mod % x]) % mod
    return invs


def get_factors(n):
    """
    素因数分解
    :param int n:
    :rtype: list of int
    """
    if n <= 1:
        return []

    ret = []
    while n > 2 and n % 2 == 0:
        ret.append(2)
        n //= 2
    i = 3
    while i <= math.sqrt(n):
        if n % i == 0:
            ret.append(i)
            n //= i
        else:
            i += 2
    ret.append(n)
    return ret


S = sys.stdin.buffer.readline().decode().rstrip()
N = len(S)
MOD = 2019

hist = []
mod_counts = [0] * MOD
power = 1
n = 0
for i in reversed(range(N)):
    n += int(S[i]) * power
    n %= MOD
    mod_counts[n] += 1
    hist.append(n)
    power *= 10
    power %= MOD

ans = 0
m = 0
power = 1
for i in reversed(range(N)):
    # m = int(S[i + 1:] or 0) % MOD
    ans += mod_counts[m]
    mod_counts[hist[::-1][i]] -= 1

    m += int(S[i]) * power
    m %= MOD
    power *= 10
    power %= MOD
print(ans)



def test(S, mod):
    ans = 0
    for l in range(len(S)):
        for r in range(l + 1, len(S) + 1):
            n = int(S[l:r])
            if n % mod == 0:
                print(n)
                ans += 1
    print(ans)


# test(S, 2019)
# print()
# test(S, 2019 // 3)


Fixed submission:
import os

import math
import sys

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7


# MOD = 998244353
def mod_invs(max, mod):
    """
    逆元 0, 1/1, 1/2, 1/3, ..., 1/max
    :param int max:
    :param int mod:
    """
    invs = [1] * (max + 1)
    invs[0] = 0
    for x in range(2, max + 1):
        invs[x] = (-(mod // x) * invs[mod % x]) % mod
    return invs


def get_factors(n):
    """
    素因数分解
    :param int n:
    :rtype: list of int
    """
    if n <= 1:
        return []

    ret = []
    while n > 2 and n % 2 == 0:
        ret.append(2)
        n //= 2
    i = 3
    while i <= math.sqrt(n):
        if n % i == 0:
            ret.append(i)
            n //= i
        else:
            i += 2
    ret.append(n)
    return ret


S = sys.stdin.buffer.readline().decode().rstrip()
N = len(S)
MOD = 2019

hist = []
mod_counts = [0] * MOD
power = 1
n = 0
for i in reversed(range(N)):
    n += int(S[i]) * power
    n %= MOD
    mod_counts[n] += 1
    hist.append(n)
    power *= 10
    power %= MOD

ans = 0
m = 0
power = 1
for i in reversed(range(N)):
    # m = int(S[i + 1:] or 0) % MOD
    ans += mod_counts[m]
    mod_counts[hist[~i]] -= 1

    m += int(S[i]) * power
    m %= MOD
    power *= 10
    power %= MOD
print(ans)



def test(S, mod):
    ans = 0
    for l in range(len(S)):
        for r in range(l + 1, len(S) + 1):
            n = int(S[l:r])
            if n % mod == 0:
                print(n)
                ans += 1
    print(ans)


# test(S, 2019)
# print()
# test(S, 2019 // 3)