Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
def solve():
    MOD = 10**9 + 7

    N = int(input())
    As = list(map(int, input().split()))

    num1s = [0] * 60
    for A in As:
        for d in range(60):
            if (A>>d) & 1:
                num1s[d] += 1

    ans = 0
    for d in range(60):
        ans += num1s[d] * (N-num1s[d]) * pow(2, d, MOD)
        ans %= MOD

    print(ans)


solve()


Fixed submission:
MOD = 10**9 + 7

N = int(input())
As = list(map(int, input().split()))

ans = 0
for d in range(60):
    sumXOR = 0
    num0, num1 = 0, 0
    for A in As:
        if (A>>(59-d))&1:
            num1 += 1
        else:
            num0 += 1
#    print('d:', d, '/ num0:', num0, '/ num1:', num1)
    sumXOR += (num0 * num1) % MOD
    sumXOR %= MOD
    ans *= 2
    ans += sumXOR
    ans %= MOD

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
def solve():
    MOD = 10**9 + 7

    N = int(input())
    As = list(map(int, input().split()))

    ans = 0
    for d in range(60):
        nums = [0, 0]
        for A in As:
            b = (A>>d) & 1
            nums[b] += 1
        v = nums[0]*nums[1]
        v *= pow(2, d, MOD)
        ans += v
        ans %= MOD

    print(ans)


solve()


Fixed submission:
MOD = 10**9 + 7

N = int(input())
As = list(map(int, input().split()))

ans = 0
for d in range(60):
    sumXOR = 0
    num0, num1 = 0, 0
    for A in As:
        if (A>>(59-d))&1:
            num1 += 1
        else:
            num0 += 1
#    print('d:', d, '/ num0:', num0, '/ num1:', num1)
    sumXOR += (num0 * num1) % MOD
    sumXOR %= MOD
    ans *= 2
    ans += sumXOR
    ans %= MOD

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
MOD = 10**9 + 7

N = int(input())
As = list(map(int, input().split()))

ans = 0
for d in range(60):
    sumXOR = 0
    num0, num1 = 0, 0
    for A in As:
        if (A>>(59-d))&1:
            num1 += 1
        else:
            num0 += 1
#    print('d:', d, '/ num0:', num0, '/ num1:', num1)
    sumXOR += (num0 * num1) % MOD
    sumXOR %= MOD
    ans *= 2
    ans += sumXOR
    ans %= MOD

print(ans)


Fixed submission:
MOD = 10**9 + 7

N = int(input())
As = list(map(int, input().split()))

ans = 0
for d in range(60):
    sumXOR = 0
    num0, num1 = 0, 0
    for A in As:
        if (A>>(59-d))&1:
            num1 += 1
        else:
            num0 += 1
#    print('d:', d, '/ num0:', num0, '/ num1:', num1)
    sumXOR += (num0 * num1) % MOD
    sumXOR %= MOD
    ans *= 2
    ans += sumXOR
    ans %= MOD

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

MOD = 10**9 + 7

N = int(input())
As = list(map(int, input().split()))

ptns = Counter([(0, 0, 0)])
#print('ptns:', ptns)
for A in As:
    ptn2s = Counter()
    for ptn, num in ptns.items():
        for i in range(3):
            if ptn[i] == A:
                ptn2 = list(ptn)
                ptn2[i] += 1
                ptn2s[tuple(ptn2)] += num
                ptn2s[tuple(ptn2)] %= MOD
    ptns = ptn2s

#print(ptns)
print(sum(ptns.values()) % MOD)


Fixed submission:
from collections import Counter

MOD = 10**9 + 7

N = int(input())
As = list(map(int, input().split()))

ptns = Counter([(0, 0, 0)])
#print('ptns:', ptns)
for A in As:
    ptn2s = Counter()
    for ptn, num in ptns.items():
        for i in range(3):
            if ptn[i] == A:
                ptn2 = list(ptn)
                ptn2[i] += 1
                ptn2s[tuple(ptn2)] += num
                ptn2s[tuple(ptn2)] %= MOD
    ptns = ptn2s

#print(ptns)
print(sum(ptns.values()) % MOD)
Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

def func(D, C):
    A, B = D, D
    rests = []
    num = 0
    while True:
        if C%2:
            rests.append(A)
            C -= 1
        if C == 0:
            break
        num += C//2
        X = A+B
        if X >= 10:
            A, B = X//10, X%10
        else:
            A, B = X, X
            C //= 2

    rests = rests[::-1]
    ret, E = func_naive(rests)
    return num+ret, E

def func_naive(Xs):
    num = 0
    while True:
        if len(Xs) == 1:
            break
        B = Xs.pop()
        A = Xs.pop()
        X = A+B
        if X >= 10:
            Xs.append(X//10)
            Xs.append(X%10)
        else:
            Xs.append(X)
        num += 1
    return num, Xs[0]

M = int(input())
dcs = [tuple(map(int, input().split())) for _ in range(M)]

ans = 0
Es = []
for d, c in dcs:
    num, E = func(d, c)
    ans += num
    Es.append(E)

num, E = func_naive(Es)
ans += num

print(ans)


Fixed submission:
import sys
input = sys.stdin.readline

def func(D, C):
    A, B = D, D
    rests = []
    num = 0
    while True:
        if C%2:
            rests.append(A)
            C -= 1
        if C == 0:
            break
        num += C//2
        X = A+B
        if X >= 10:
            A, B = X//10, X%10
        else:
            A, B = X, X
            C //= 2

    rests = rests[::-1]
    ret, E = func_naive(rests)
    return num+ret, E

def func_naive(Xs):
    num = 0
    while True:
        if len(Xs) == 1:
            break
        B = Xs.pop()
        A = Xs.pop()
        X = A+B
        if X >= 10:
            Xs.append(X//10)
            Xs.append(X%10)
        else:
            Xs.append(X)
        num += 1
    return num, Xs[0]

M = int(input())
dcs = [tuple(map(int, input().split())) for _ in range(M)]

ans = 0
Es = []
for d, c in dcs:
    num, E = func(d, c)
    ans += num
    Es.append(E)

num, E = func_naive(Es)
ans += num

print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
N, T = map(int, input().split())
items = [tuple(map(int, input().split())) for _ in range(N)]

items.sort()

ans = 0
dp = {0: 0}
for wi, vi in items:
    for w, v in dp.items():
        if w < T:
            v2 = v + vi
            if v2 > ans:
                ans = v2

    for w, v in list(dp.items()):
        w2 = w + wi
        if w2 <= T:
            v2 = v + vi
            if w2 in dp:
                if v2 > dp[w2]:
                    dp[w2] = v2
            else:
                dp[w2] = v2

v2 = max(dp.values())
ans = max(ans, v2)

print(ans)


Fixed submission:
N, T = map(int, input().split())
items = [tuple(map(int, input().split())) for _ in range(N)]

items.sort()

ans = 0
dp = {0: 0}
for wi, vi in items:
    for w, v in dp.items():
        if w < T:
            v2 = v + vi
            if v2 > ans:
                ans = v2

    for w, v in list(dp.items()):
        w2 = w + wi
        if w2 <= T:
            v2 = v + vi
            if w2 in dp:
                if v2 > dp[w2]:
                    dp[w2] = v2
            else:
                dp[w2] = v2

v2 = max(dp.values())
ans = max(ans, v2)

print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
def solve():
    INF = 10**12

    def max2(x, y): return x if x >= y else y
    def min2(x, y): return x if x <= y else y

    N, K = map(int, input().split())
    Hs = [0] + list(map(int, input().split()))

    dp = [[INF]*(N+1) for _ in range(N+1)]
    dp[0][0] = 0
    for i in range(1, N+1):
        H = Hs[i]
        diffs = []
        for iPrev in range(i):
            diff = max2(0, H-Hs[iPrev])
            diffs.append(diff)
        dp[i][0] = 0
        dp[i][1] = H
        for j in range(2, min(i, N-K)+1):
            for iPrev in range(i):
                dp[i][j] = min2(dp[i][j], dp[iPrev][j-1]+diffs[iPrev])

    ans = min(dpi[N-K] for dpi in dp)
    print(ans)


solve()


Fixed submission:
def solve():
    INF = float('inf')

    def max2(x, y): return x if x >= y else y

    N, K = map(int, input().split())
    Hs = [0] + list(map(int, input().split()))

    if N == K:
        return 0

    dp = [INF] * (N+1)
    dp[0] = 0
    for j in range(1, N-K+1):
        dp2 = [INF] * (N+1)
        for i, H in enumerate(Hs[j:], j):
            for k in range(j-1, i):
                dp2[i] = min(dp2[i], dp[k] + max2(0, H-Hs[k]))
        dp = dp2

    return min(dp)

ans = solve()
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
N, K = map(int, input().split())
As = list(map(int, input().split()))
Fs = list(map(int, input().split()))

As.sort()
Fs.sort(reverse=True)
maxAF = max([A*F for A, F in zip(As, Fs)])

def isOK(X):
    rest = K
    for A, F in zip(As, Fs):
        if A*F > X:
            A2 = X//F
            d = A-A2
            if d > rest:
                return False
            rest -= d
    return True

ng, ok = -1, maxAF
while abs(ok-ng) > 1:
    mid = (ng+ok) // 2
    if isOK(mid):
        ok = mid
    else:
        ng = mid

print(ok)


Fixed submission:
N, K = map(int, input().split())
As = list(map(int, input().split()))
Fs = list(map(int, input().split()))

As.sort()
Fs.sort(reverse=True)
maxAF = max([A*F for A, F in zip(As, Fs)])

def isOK(X):
    rest = K
    for A, F in zip(As, Fs):
        if A*F > X:
            A2 = X//F
            d = A-A2
            if d > rest:
                return False
            rest -= d
    return True

ng, ok = -1, maxAF
while abs(ok-ng) > 1:
    mid = (ng+ok) // 2
    if isOK(mid):
        ok = mid
    else:
        ng = mid

print(ok)
Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

def solve():
    INF = float('inf')

    def WarshallFloyd(adjList, INF):
        numV = len(adjList)
        D = [[INF]*numV for _ in range(numV)]
        for u, adj in enumerate(adjList):
            for v, wt in adj:
                D[u][v] = wt
            D[u][u] = 0

        for k in range(numV):
            Dk = D[k]
            for i in range(numV):
                Di = D[i]
                Dik = Di[k]
                for j in range(numV):
                    D2 = Dik + Dk[j]
                    if D2 < Di[j]:
                        D[i][j] = D2

        return D

    N, M, L = map(int, input().split())
    adjL = [[] for _ in range(N)]
    for _ in range(M):
        a, b, c = map(int, input().split())
        a, b = a-1, b-1
        adjL[a].append((b, c))
        adjL[b].append((a, c))
    Q = int(input())
    querys = [tuple(map(int, input().split())) for _ in range(Q)]

    cost1ss = WarshallFloyd(adjL, INF)

    adjL2 = [[] for _ in range(N)]
    for a in range(N):
        for b in range(a+1, N):
            if cost1ss[a][b] <= L:
                adjL2[a].append((b, 1))
                adjL2[b].append((a, 1))

    cost2ss = WarshallFloyd(adjL2, INF)

    anss = []
    for s, t in querys:
        s, t = s-1, t-1
        cost = cost2ss[s][t]
        if cost == INF:
            anss.append(-1)
        else:
            anss.append(cost-1)

    print('\n'.join(map(str, anss)))


solve()


Fixed submission:
def solve():
    INF = float('inf')

    def WarshallFloyd(adjList):
        numV = len(adjList)
        D = [[INF]*numV for _ in range(numV)]
        for u, adj in enumerate(adjList):
            for v, wt in adj:
                D[u][v] = wt
            D[u][u] = 0
        for k in range(numV):
            Dk = D[k]
            for i in range(numV):
                Di = D[i]
                Dik = Di[k]
                for j in range(numV):
                    D2 = Dik + Dk[j]
                    if D2 < Di[j]:
                        D[i][j] = D2
        return D

    N, M, L = map(int, input().split())
    adjL = [[] for _ in range(N)]
    for _ in range(M):
        A, B, C = map(int, input().split())
        A, B = A-1, B-1
        adjL[A].append((B, C))
        adjL[B].append((A, C))

    D = WarshallFloyd(adjL)

    adjL2 = [[] for _ in range(N)]
    for i in range(N):
        for j in range(i+1, N):
            if D[i][j] <= L:
                adjL2[i].append((j, 1))
                adjL2[j].append((i, 1))

    D = WarshallFloyd(adjL2)

    Q = int(input())
    anss = []
    for _ in range(Q):
        s, t = map(int, input().split())
        s, t = s-1, t-1
        if D[s][t] == INF:
            anss.append(-1)
        else:
            anss.append(D[s][t]-1)

    print('\n'.join(map(str, anss)))


solve()
Buggy python submission (verdict: Time Limit Exceeded):
def solve():
    N = int(input())
    Ps = list(map(int, input().split()))

    def makeBIT(numEle):
        numPow2 = 2 ** (numEle-1).bit_length()
        data = [0] * (numPow2+1)
        return data, numPow2
    def addValue(iA, A):
        iB = iA + 1
        while iB <= numPow2:
            data[iB] += A
            iB += iB & -iB
    def getSum(iA):
        iB = iA + 1
        ans = 0
        while iB > 0:
            ans += data[iB]
            iB -= iB & -iB
        return ans

    data, numPow2 = makeBIT(N)

    iPs = list(range(N))
    iPs.sort(key=lambda iP: Ps[iP])

    ans = 0
    for iP in reversed(iPs):
        v = getSum(iP)
        Bs = []
        for x in range(v-1, v+3):
            if x <= 0:
                L = -1
            elif getSum(numPow2-1) < x:
                L = N
            else:
                maxD = numPow2.bit_length()-1
                L = 0
                S = 0
                for d in reversed(range(maxD)):
                    if S+data[L+(1<<d)] < x:
                        S += data[L+(1<<d)]
                        L += 1<<d
            Bs.append(L)

        num = (Bs[1]-Bs[0])*(Bs[2]-iP) + (iP-Bs[1])*(Bs[3]-Bs[2])
        ans += Ps[iP] * num
        addValue(iP, 1)

    print(ans)


solve()


Fixed submission:
N = int(input())
Ps = list(map(int, input().split()))

iPs = [0] * (N+1)
for iP, A in enumerate(Ps):
    iPs[A] = iP

ans = 0
iLs = list(range(N+1))
iRs = list(range(N+1))
for P in range(1, N+1):
    i = iPs[P]
    iL, iR = iLs[i], iRs[i]
    if iL <= 1:
        iL2 = 0
    else:
        iL2 = iLs[iL-1]
    if iR >= N-2:
        iR2 = N-1
    else:
        iR2 = iRs[iR+1]
    num = 0
    if iL != 0:
        num += (iL-iL2)*(iR-i+1)
    if iR != N-1:
        num += (i-iL+1)*(iR2-iR)
    ans += P * num
    iLs[iR+1] = iL
    iRs[iL-1] = iR

print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline

def solve():
    N, Q = map(int, input().split())
    adjL = [[] for _ in range(N)]
    for _ in range(N-1):
        A, B = map(int, input().split())
        A, B = A-1, B-1
        adjL[A].append(B)
        adjL[B].append(A)

    anss = [0] * N
    for _ in range(Q):
        p, x = map(int, input().split())
        p = p-1
        anss[p] += x

    useds = [False] * N
    def dfs(vNow, x):
        useds[vNow] = True
        anss[vNow] += x
        for v2 in adjL[vNow]:
            if not useds[v2]:
                dfs(v2, anss[vNow])

    dfs(0, 0)

    print(' '.join(map(str, anss)))

solve()


Fixed submission:
def solve():
    import sys
    sys.setrecursionlimit(10**9)
    input = sys.stdin.readline

    N, Q = map(int, input().split())
    adjL = [[] for _ in range(N)]
    for _ in range(N-1):
        A, B = map(int, input().split())
        A, B = A-1, B-1
        adjL[A].append(B)
        adjL[B].append(A)

    anss = [0] * N
    for _ in range(Q):
        p, x = map(int, input().split())
        p = p-1
        anss[p] += x

    useds = [False] * N
    def dfs(vNow, x):
        useds[vNow] = True
        anss[vNow] += x
        ans = anss[vNow]
        for v2 in adjL[vNow]:
            if not useds[v2]:
                dfs(v2, ans)

    dfs(0, 0)

    print(' '.join(map(str, anss)))

solve()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline

N, Q = map(int, input().split())
adjL = [[] for _ in range(N)]
for _ in range(N-1):
    A, B = map(int, input().split())
    A, B = A-1, B-1
    adjL[A].append(B)
    adjL[B].append(A)

anss = [0] * N
for _ in range(Q):
    p, x = map(int, input().split())
    p = p-1
    anss[p] += x

useds = [False] * N
def dfs(vNow, x):
    useds[vNow] = True
    anss[vNow] += x
    for v2 in adjL[vNow]:
        if not useds[v2]:
            dfs(v2, anss[vNow])

dfs(0, 0)

print(' '.join(map(str, anss)))


Fixed submission:
def solve():
    import sys
    sys.setrecursionlimit(10**9)
    input = sys.stdin.readline

    N, Q = map(int, input().split())
    adjL = [[] for _ in range(N)]
    for _ in range(N-1):
        A, B = map(int, input().split())
        A, B = A-1, B-1
        adjL[A].append(B)
        adjL[B].append(A)

    anss = [0] * N
    for _ in range(Q):
        p, x = map(int, input().split())
        p = p-1
        anss[p] += x

    useds = [False] * N
    def dfs(vNow, x):
        useds[vNow] = True
        anss[vNow] += x
        ans = anss[vNow]
        for v2 in adjL[vNow]:
            if not useds[v2]:
                dfs(v2, ans)

    dfs(0, 0)

    print(' '.join(map(str, anss)))

solve()
Buggy python submission (verdict: Time Limit Exceeded):
from heapq import heapify, heappush, heappop

def max2(x, y): return x if x >= y else y

def solve(As, Bs):
    for A, B in zip(As, Bs):
        if A > B:
            return -1

    PQ = [(-B, i) for i, (A, B) in enumerate(zip(As, Bs)) if A != B]
    heapify(PQ)
    ans = 0
    while PQ:
        B, i = heappop(PQ)
        A, B = As[i], -B
        sumNeib = Bs[i-1] + Bs[(i+1)%N]

        if B <= sumNeib or B-sumNeib < A:
            return -1
        num = (B - max2(A, sumNeib)) // sumNeib
        B -= sumNeib * num
        if B-sumNeib >= A:
            B -= sumNeib
            num += 1

        if A != B:
            heappush(PQ, (-B, i))
        Bs[i] = B
        ans += num

    return ans


N = int(input())
As = list(map(int, input().split()))
Bs = list(map(int, input().split()))

ans = solve(As, Bs)
print(ans)


Fixed submission:
from heapq import heapify, heappush, heappop

def max2(x, y): return x if x >= y else y

def solve(As, Bs):
    for A, B in zip(As, Bs):
        if A > B:
            return -1

    PQ = [(-B, i) for i, (A, B) in enumerate(zip(As, Bs)) if A != B]
    heapify(PQ)
    ans = 0
    while PQ:
        B, i = heappop(PQ)
        A, B = As[i], -B
        sumNeib = Bs[i-1] + Bs[(i+1)%N]

        if B <= sumNeib or B-sumNeib < A:
            return -1
        num = (B - max2(A, sumNeib)) // sumNeib
        B -= sumNeib * num
        if B-sumNeib >= A:
            B -= sumNeib
            num += 1

        if A != B:
            heappush(PQ, (-B, i))
        Bs[i] = B
        ans += num

    return ans


N = int(input())
As = list(map(int, input().split()))
Bs = list(map(int, input().split()))

ans = solve(As, Bs)
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque

def BellmanFord(adjList, vSt, INF):
    numV = len(adjList)
    negINF = -INF
    costs = [INF] * numV
    costs[vSt] = 0
    vNows = set([vSt])

    for _ in range(numV):
        vChangeds = set()
        for vFr in vNows:
            costFr = costs[vFr]
            for vTo, wt in adjList[vFr]:
                c2 = costFr + wt
                if c2 < costs[vTo]:
                    costs[vTo] = c2
                    vChangeds.add(vTo)
        if not vChangeds:
            return costs
        vNows = vChangeds

    for v in vNows:
        costs[v] = negINF

        Q = deque(vNows)
    while Q:
        vFr = Q.popleft()
        for vTo, wt in adjList[vFr]:
            if costs[vTo] == negINF: continue
            costs[vTo] = negINF
            Q.append(vTo)

    return costs


N, M, P = map(int, input().split())
adjL = [[] for _ in range(N)]
for _ in range(M):
    A, B, C = map(int, input().split())
    A, B = A-1, B-1
    adjL[A].append((B, -(C-P)))

costs = BellmanFord(adjL, 0, float('inf'))

if costs[N-1] == -float('inf'):
    print(-1)
else:
    print(max(0, -costs[N-1]))


Fixed submission:
from collections import deque

def BellmanFord(adjList, vSt, INF):
    numV = len(adjList)
    negINF = -INF
    costs = [INF] * numV
    costs[vSt] = 0
    vNows = set([vSt])

    for _ in range(numV):
        vChangeds = set()
        for vFr in vNows:
            costFr = costs[vFr]
            for vTo, wt in adjList[vFr]:
                c2 = costFr + wt
                if c2 < costs[vTo]:
                    costs[vTo] = c2
                    vChangeds.add(vTo)
        if not vChangeds:
            return costs
        vNows = vChangeds

    for v in vNows:
        costs[v] = negINF

        Q = deque(vNows)
    while Q:
        vFr = Q.popleft()
        for vTo, wt in adjList[vFr]:
            if costs[vTo] == negINF: continue
            costs[vTo] = negINF
            Q.append(vTo)

    return costs


N, M, P = map(int, input().split())
adjL = [[] for _ in range(N)]
for _ in range(M):
    A, B, C = map(int, input().split())
    A, B = A-1, B-1
    adjL[A].append((B, -(C-P)))

costs = BellmanFord(adjL, 0, float('inf'))

if costs[N-1] == -float('inf'):
    print(-1)
else:
    print(max(0, -costs[N-1]))
Buggy python submission (verdict: Time Limit Exceeded):
def solve():
    MOD = 10**9 + 7

    Ss = input()

    N = len(Ss)

    dp = [0] * 13
    dp[0] = 1
    for d, Sd in enumerate(Ss):
        dp2 = [0] * 13
        for mod13 in range(13):
            if Sd != '?':
                x = int(Sd)
                mod13n = (10*mod13 + x) % 13
                dp2[mod13n] += dp[mod13]
                dp2[mod13n] %= MOD
            else:
                for x in range(10):
                    mod13n = (10*mod13 + x) % 13
                    dp2[mod13n] += dp[mod13]
                    dp2[mod13n] %= MOD
        dp = dp2

    print(dp[5])


solve()


Fixed submission:
MOD = 10**9 + 7

Ss = input()

N = len(Ss)

dp = [[0]*(13) for _ in range(N+1)]
dp[0][0] = 1
for d, Sd in enumerate(Ss):
#    print(d, Sd)
    for mod13 in range(13):
        if Sd != '?':
            x = int(Sd)
            mod13n = (10*mod13 + x) % 13
            dp[d+1][mod13n] += dp[d][mod13]
            dp[d+1][mod13n] %= MOD
        else:
            for x in range(10):
                mod13n = (10*mod13 + x) % 13
                dp[d+1][mod13n] += dp[d][mod13]
                dp[d+1][mod13n] %= MOD

print(dp[-1][5])

Buggy python submission (verdict: Time Limit Exceeded):
MOD = 10**9 + 7

Ss = input()

N = len(Ss)

dp = [[0]*(13) for _ in range(N+1)]
dp[0][0] = 1
for d, Sd in enumerate(Ss):
#    print(d, Sd)
    for mod13 in range(13):
        if Sd != '?':
            x = int(Sd)
            mod13n = (10*mod13 + x) % 13
            dp[d+1][mod13n] += dp[d][mod13]
            dp[d+1][mod13n] %= MOD
        else:
            for x in range(10):
                mod13n = (10*mod13 + x) % 13
                dp[d+1][mod13n] += dp[d][mod13]
                dp[d+1][mod13n] %= MOD

print(dp[-1][5])


Fixed submission:
MOD = 10**9 + 7

Ss = input()

N = len(Ss)

dp = [[0]*(13) for _ in range(N+1)]
dp[0][0] = 1
for d, Sd in enumerate(Ss):
#    print(d, Sd)
    for mod13 in range(13):
        if Sd != '?':
            x = int(Sd)
            mod13n = (10*mod13 + x) % 13
            dp[d+1][mod13n] += dp[d][mod13]
            dp[d+1][mod13n] %= MOD
        else:
            for x in range(10):
                mod13n = (10*mod13 + x) % 13
                dp[d+1][mod13n] += dp[d][mod13]
                dp[d+1][mod13n] %= MOD

print(dp[-1][5])