Problem p02960 (python)

Fixed submission:
S = input()
con = 10**9 + 7

T = [s if s != "?" else "0" for s in S]
T = int(''.join(T))
SS = T % 13

def get_mod(S):
    c = {}
    iii = 1
    for i in range(len(S)):
        if S[len(S) - i - 1] != "?":
            iii *= 10
            iii %= 13
            continue
        c[i] = {(j * iii) % 13: 1 for j in range(0, 10)}
        iii *= 10
        iii %= 13
    return c
c = get_mod(S)

def get_tdp(pdp):
    tdp = {i: 0 for i in range(13)}
    for r in v.keys():
        for _r, _v in pdp.items():
            tdp[(_r + r) % 13] += _v
    for i in range(13):
        tdp[i] %= con
    return tdp

pdp = {i: 0 for i in range(13)}
pdp[SS] += 1
for k, v in c.items():
    pdp = get_tdp(pdp)

ans = pdp[5]
print(ans)

Buggy submission (verdict: Runtime Error):
from functools import lru_cache

def main(S):
    con = 10**9 + 7

    @lru_cache(maxsize=1024)
    def get_c(iii):
        return [mm13[iii][j] % 13 for j in l10]

    l10 = list(range(10))
    l13 = list(range(13))
    m13 = {i: {j: (i + j) % 13 for j in l13} for i in l13}
    mm13 = {i: {j: (i * j) % 13 for j in range(11)} for i in range(100)}
    c = []
    iii = 1
    for i in range(len(S)):
        s = S[len(S) - i - 1]
        if s != "?":
            c.append(mm13[iii][int(s)])
        else:
            c.append(get_c(iii))
        iii = mm13[iii][10]

    _tdp = {i: 0 for i in l13}
    it = iter(c)
    pdp = {i: 1 for i in next(it)}
    d = 1
    for v in it:
        tdp = _tdp.copy()
        for r in v:
            for _r, _v in pdp.items():
                tdp[m13[_r][r]] += _v
        if d == 0:
            for i in l13:
                tdp[i] = tdp[i] % con
        d = (d + 1) % 50
        pdp = tdp

    ans = pdp.get(5, 0)
    print(ans % con)

if __name__ == "__main__":
    S = input()
    main(S)

Buggy submission (verdict: Runtime Error):
from fastcache import clru_cache

def main(S):
    con = 10**9 + 7

    @clru_cache(maxsize=1024, typed=True)
    def get_c(iii):
        return [(j * iii) % 13 for j in l10]

    l10 = list(range(10))
    l13 = list(range(13))
    m13 = {i: {j: (i + j) % 13 for j in l13} for i in l13}
    c = []
    iii = 1
    for i in range(len(S)):
        s = S[len(S) - i - 1]
        if s != "?":
            c.append([(int(s) * iii) % 13])
        else:
            c.append(get_c(iii))
        iii = (iii * 10) % 13

    _tdp = {i: 0 for i in l13}
    it = iter(c)
    pdp = {i: 1 for i in next(it)}
    d = 1
    for v in it:
        tdp = _tdp.copy()
        for r in v:
            for _r, _v in pdp.items():
                tdp[m13[_r][r]] += _v
        if d == 0:
            for i in l13:
                tdp[i] = tdp[i] % con
        d = (d + 1) % 50
        pdp = tdp

    ans = pdp.get(5, 0)
    print(ans % con)

if __name__ == "__main__":
    S = input()
    main(S)

Buggy submission (verdict: Runtime Error):
def main():
    S = input()
    con = 10**9 + 7

    def get_mod(S):
        c = {}
        iii = 1
        for i in range(len(S)):
            if S[len(S) - i - 1] != "?":
                c[i] = {(int(S[len(S) - i - 1]) * iii) % 13: 1}
                iii *= 10
                iii %= 13
                continue
            c[i] = {(j * iii) % 13: 1 for j in range(0, 10)}
            iii *= 10
            iii %= 13
        return c
    c = get_mod(S)

    def get_tdp(pdp):
        tdp = {i: 0 for i in range(13)}
        for r in v.keys():
            for _r, _v in pdp.items():
                tdp[(_r + r) % 13] += _v
        for i in range(13):
            tdp[i] %= con
        return tdp

    it = iter(c.items())
    pdp = next(it)[-1]
    for k, v in it:
        pdp = get_tdp(pdp)

    ans = pdp[5]
    print(ans)

if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
from collections import defaultdict
S = input()

T = [s if s != "?" else "0" for s in S]
T = int(''.join(T))
SS = T % 13

c = {}
for i in range(len(S)):
    if S[len(S) - i - 1] != "?":
        continue
    tc = defaultdict(lambda: 0)
    for j in range(0, 10):
        t = j * 10 ** i
        tc[t % 13] += 1
    c[i] = tc

dp = [{SS: 1}]
for k, v in c.items():
    tdp = defaultdict(lambda: 0)
    for r, c in v.items():
        for _r, _v in dp[-1].items():
            tdp[(_r + r) % 13] += _v * c
    dp.append(tdp)

ans = dp[-1][5]
print(ans % (10**9 + 7))