Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter
from collections import defaultdict

if __name__ == '__main__':
    MOD = 2019
    strS = input()
    S = [int(s) for s in strS]
    N = len(S)

    mods = defaultdict(int)
    for idx, s in enumerate(S[::-1]):
        if idx == 0:
            tmp_mod = s % MOD
        else:
            tmp_mod = (prv_mod + s * 10 ** idx) % MOD

        mods[tmp_mod] += 1
        prv_mod = tmp_mod

    ans = 0
    for key, value in mods.items():
        if value >= 2:
            ans += value * (value - 1) // 2
        elif key == 0:
            ans += 1
    print(ans)


Fixed submission:
from collections import defaultdict

if __name__ == '__main__':
    MOD = 2019
    strS = input()
    S = [int(s) for s in strS]
    N = len(S)

    mods = defaultdict(int)
    mods[0] += 1
    pow10_mod = 1
    prv_mod = 0
    for idx, s in enumerate(S[::-1]):
        tmp_mod = (prv_mod + s * pow10_mod) % MOD

        mods[tmp_mod] += 1
        pow10_mod = (pow10_mod * 10) % MOD
        prv_mod = tmp_mod

    ans = 0
    for key, value in mods.items():
        if value >= 2:
            ans += value * (value - 1) // 2

    print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter

if __name__ == '__main__':
    MOD = 2019
    strS = input()
    S = [int(s) for s in strS]
    N = len(S)

    mods = []
    for idx, s in enumerate(S[::-1]):
        if idx == 0:
            mods.append(s % MOD)
        else:
            mods.append((mods[idx - 1] + s * 10 ** idx) % MOD)

    mods_cnt = Counter(mods)

    ans = 0
    for key, value in mods_cnt.items():
        if value >= 2:
            ans += value * (value - 1) // 2
        elif key == 0:
            ans += 1
    print(ans)


Fixed submission:
from collections import defaultdict

if __name__ == '__main__':
    MOD = 2019
    strS = input()
    S = [int(s) for s in strS]
    N = len(S)

    mods = defaultdict(int)
    mods[0] += 1
    pow10_mod = 1
    prv_mod = 0
    for idx, s in enumerate(S[::-1]):
        tmp_mod = (prv_mod + s * pow10_mod) % MOD

        mods[tmp_mod] += 1
        pow10_mod = (pow10_mod * 10) % MOD
        prv_mod = tmp_mod

    ans = 0
    for key, value in mods.items():
        if value >= 2:
            ans += value * (value - 1) // 2

    print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
if __name__ == '__main__':
    MOD = 2019

    S = [int(s) for s in input()]
    N = len(S)
    dp = [[float('inf') for _ in range(N + 1)] for _ in range(N + 1)]

    dp[0][0] = S[0] % MOD

    for i in range(N):
        for j in range(i, N):
            if i == j:
                dp[i][j] = S[i] % MOD
            else:
                dp[i][j] = (dp[i][j - 1] * 10 + S[j]) % MOD

    print(sum([row.count(0) for row in dp]))


Fixed submission:
from collections import defaultdict

if __name__ == '__main__':
    MOD = 2019
    strS = input()
    S = [int(s) for s in strS]
    N = len(S)

    mods = defaultdict(int)
    mods[0] += 1
    pow10_mod = 1
    prv_mod = 0
    for idx, s in enumerate(S[::-1]):
        tmp_mod = (prv_mod + s * pow10_mod) % MOD

        mods[tmp_mod] += 1
        pow10_mod = (pow10_mod * 10) % MOD
        prv_mod = tmp_mod

    ans = 0
    for key, value in mods.items():
        if value >= 2:
            ans += value * (value - 1) // 2

    print(ans)