Problem:
 Score : 400 points    Problem Statement Given is a string S . Each character in S is either a digit ( 0 , ..., 9 ) or ? .  Among the integers obtained by replacing each occurrence of ? with a digit, how many have a remainder of 5 when divided by 13 ? An integer may begin with 0 .  Since the answer can be enormous, print the count modulo 10^9+7 .      Constraints   S is a string consisting of digits ( 0 , ..., 9 ) and ? .  1 \leq |S| \leq 10^5         Input  Input is given from Standard Input in the following format:  S       Output  Print the number of integers satisfying the condition, modulo 10^9+7 .        Sample Input 1 ??2??5      Sample Output 1 768  For example, 482305, 002865, and 972665 satisfy the condition.       Sample Input 2 ?44      Sample Output 2 1  Only 044 satisfies the condition.       Sample Input 3 7?4      Sample Output 3 0  We may not be able to produce an integer satisfying the condition.       Sample Input 4 ?6?42???8??2??06243????9??3???7258??5??7???????774????4?1??17???9?5?70???76???      Sample Output 4 153716888

Solution (Python):
modarr61=[76924, 76923, 76923, 76923, 76923, 76923, 76923, 76923, 76923, 76923, 76923, 76923, 76923]
modarr51=[7693, 7692, 7692, 7692, 7693, 7692, 7692, 7693, 7692, 7692, 7693, 7692, 7692]
modarr52=[7693, 7693, 7692, 7692, 7692, 7693, 7692, 7692, 7692, 7693, 7692, 7692, 7692]
modarr53=[7693, 7692, 7692, 7692, 7692, 7692, 7692, 7692, 7692, 7692, 7693, 7693, 7693]
modarr54=[7693, 7692, 7692, 7693, 7692, 7692, 7693, 7692, 7692, 7693, 7692, 7692, 7692]
modarr55=[7693, 7692, 7692, 7692, 7693, 7692, 7692, 7692, 7693, 7692, 7692, 7692, 7693]
modarr56=[7693, 7693, 7693, 7693, 7692, 7692, 7692, 7692, 7692, 7692, 7692, 7692, 7692]
modarr41=[770, 769, 769, 769, 769, 770, 769, 769, 769, 770, 769, 769, 769]
modarr42=[769, 769, 769, 769, 770, 769, 769, 770, 769, 769, 770, 769, 769]
modarr43=[770, 769, 769, 769, 769, 769, 769, 769, 769, 769, 769, 770, 770]
modarr44=[772, 768, 768, 771, 769, 768, 770, 770, 768, 769, 771, 768, 768]
modarr45=[769, 770, 769, 769, 769, 770, 769, 769, 769, 770, 769, 769, 769]
modarr46=[770, 769, 769, 770, 769, 769, 770, 769, 769, 769, 769, 769, 769]
modarr47=[769, 769, 769, 769, 770, 769, 769, 769, 770, 769, 769, 769, 770]
modarr48=[772, 769, 768, 768, 771, 770, 768, 768, 770, 771, 768, 768, 769]
modarr49=[769, 769, 769, 769, 769, 769, 769, 769, 769, 769, 770, 770, 770]
modarr410=[770, 769, 769, 769, 770, 769, 769, 769, 770, 769, 769, 769, 769]
modarr411=[770, 769, 769, 769, 769, 769, 769, 770, 769, 769, 770, 769, 769]
modarr412=[769, 770, 770, 770, 769, 769, 769, 769, 769, 769, 769, 769, 769]
modarr413=[772, 771, 770, 769, 768, 768, 768, 768, 768, 768, 769, 770, 771]
modarr414=[769, 769, 769, 770, 769, 769, 770, 769, 769, 770, 769, 769, 769]
modarr415=[770, 770, 770, 769, 769, 769, 769, 769, 769, 769, 769, 769, 769]
modarr31=[77, 76, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77]
modarr32=[77, 77, 77, 77, 76, 78, 77, 76, 77, 78, 76, 77, 77]
modarr33=[79, 76, 76, 78, 77, 76, 77, 78, 76, 76, 79, 76, 76]
modarr34=[77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 76, 77, 77]
modarr35=[79, 76, 76, 76, 79, 77, 76, 76, 78, 78, 76, 76, 77]
modarr36=[76, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77]
modarr37=[77, 76, 77, 77, 77, 76, 77, 77, 77, 76, 77, 78, 78]
modarr38=[77, 77, 77, 76, 78, 77, 76, 77, 78, 76, 77, 77, 77]
modarr39=[79, 77, 76, 76, 78, 78, 76, 76, 77, 79, 76, 76, 76]
modarr310=[77, 77, 77, 77, 77, 77, 77, 77, 77, 76, 77, 77, 77]
modarr311=[77, 77, 77, 77, 76, 77, 77, 77, 77, 77, 77, 77, 77]
modarr312=[77, 76, 76, 76, 77, 77, 77, 78, 77, 77, 78, 77, 77]
modarr313=[79, 79, 78, 77, 76, 76, 76, 76, 76, 76, 76, 77, 78]
modarr314=[79, 76, 76, 79, 76, 76, 78, 77, 76, 77, 78, 76, 76]
modarr315=[76, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77]
modarr316=[77, 77, 77, 78, 77, 77, 78, 77, 77, 77, 76, 76, 76]
modarr317=[77, 77, 77, 76, 77, 77, 77, 77, 77, 77, 77, 77, 77]
modarr318=[77, 78, 78, 77, 76, 77, 77, 77, 76, 77, 77, 77, 76]
modarr319=[79, 78, 77, 76, 76, 76, 76, 76, 76, 76, 77, 78, 79]
modarr320=[77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 76]
modarr21=[8, 7, 8, 8, 7, 8, 8, 7, 8, 8, 7, 8, 8]
modarr22=[7, 7, 8, 8, 8, 7, 8, 8, 8, 7, 8, 8, 8]
modarr23=[10, 7, 7, 7, 9, 8, 7, 7, 8, 9, 7, 7, 7]
modarr24=[7, 8, 8, 7, 8, 8, 7, 8, 8, 7, 8, 8, 8]
modarr25=[8, 7, 8, 8, 8, 7, 8, 8, 8, 7, 7, 8, 8]
modarr26=[8, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8]
modarr27=[7, 8, 8, 8, 7, 8, 8, 7, 8, 8, 7, 8, 8]
modarr28=[10, 7, 7, 9, 7, 7, 8, 8, 7, 7, 9, 7, 7]
modarr29=[7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 7, 7, 7]
modarr210=[8, 8, 8, 7, 7, 8, 8, 8, 7, 8, 8, 8, 7]
modarr211=[7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8]
modarr212=[10, 9, 8, 7, 7, 7, 7, 7, 7, 7, 7, 8, 9]
modarr213=[8, 8, 8, 7, 8, 8, 7, 8, 8, 7, 8, 8, 7]
modarr214=[7, 8, 8, 8, 7, 8, 8, 8, 7, 8, 8, 8, 7]
modarr215=[8, 8, 8, 8, 8, 8, 8, 8, 8, 7, 7, 7, 7]
modarr11=[1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1]
modarr12=[1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1]
modarr13=[1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1]
modarr14=[1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0]
modarr15=[1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1]
modarr16=[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0]

def makemod(n):
  if n==100000:
    return modarr11
  elif n==10000:
    return modarr12
  elif n==1000:
    return modarr13
  elif n==100:
    return modarr14
  elif n==10:
    return modarr15
  elif n==1:
    return modarr16
  elif n==110000:
    return modarr21
  elif n==101000:
    return modarr22
  elif n==100100:
    return modarr23
  elif n==100010:
    return modarr24
  elif n==100001:
    return modarr25
  elif n==11000:
    return modarr26
  elif n==10100:
    return modarr27
  elif n==10010:
    return modarr28
  elif n==10001:
    return modarr29
  elif n==1100:
    return modarr210
  elif n==1010:
    return modarr211
  elif n==1001:
    return modarr212
  elif n==110:
    return modarr213
  elif n==101:
    return modarr214
  elif n==11:
    return modarr215
  elif n==111000:
    return modarr31
  elif n==110100:
    return modarr32
  elif n==110010:
    return modarr33
  elif n==110001:
    return modarr34
  elif n==101100:
    return modarr35
  elif n==101010:
    return modarr36
  elif n==101001:
    return modarr37
  elif n==100110:
    return modarr38
  elif n==100101:
    return modarr39
  elif n==100011:
    return modarr310
  elif n==11100:
    return modarr311
  elif n==11010:
    return modarr312
  elif n==11001:
    return modarr313
  elif n==10110:
    return modarr314
  elif n==10101:
    return modarr315
  elif n==10011:
    return modarr316
  elif n==1110:
    return modarr317
  elif n==1101:
    return modarr318
  elif n==1011:
    return modarr319
  elif n==111:
    return modarr320
  elif n==111100:
    return modarr41
  elif n==111010:
    return modarr42
  elif n==111001:
    return modarr43
  elif n==110110:
    return modarr44
  elif n==110101:
    return modarr45
  elif n==110011:
    return modarr46
  elif n==101110:
    return modarr47
  elif n==101101:
    return modarr48
  elif n==101011:
    return modarr49
  elif n==100111:
    return modarr410
  elif n==11110:
    return modarr411
  elif n==11101:
    return modarr412
  elif n==11011:
    return modarr413
  elif n==10111:
    return modarr414
  elif n==1111:
    return modarr415
  elif n==111110:
    return modarr51
  elif n==111101:
    return modarr52
  elif n==111011:
    return modarr53
  elif n==110111:
    return modarr54
  elif n==101111:
    return modarr55
  elif n==11111:
    return modarr56

s=input()
l=len(s)
if l%6!=0:
  s='0'*(6-l%6)+s
  l+=(6-l%6)
mod=10**9+7
cnt=0
dp=[[0]*13 for _ in range(l//6+1)]
for i in range(l-1,-1,-6):
  num=0
  qpos=0
  for j in range(6):
    if s[i-j]=='?':
      qpos+=1*(10**j)
    else:
      num+=int(s[i-j])*(10**j)
  if qpos==0:
    tmod=[0]*13
    tmod[num%13]=1
  elif qpos==111111:
    tmod=modarr61
  else:
    tpos=num%13
    tmod=makemod(qpos)
    tmod=tmod[-tpos:]+tmod[:-tpos]
  if cnt==0:
    dp[1]=tmod
  else:
    for k in range(13):
      for l in range(13):
        dp[cnt+1][(k+l)%13]+=(tmod[k]*dp[cnt][l])%mod
  cnt+=1
print(dp[cnt][5]%mod)

Solution (Python):
mod = 10**9+7
S = input()
n = len(S)
dp = [[0]*13 for _ in range(n+1)]
dp[0][0] = 1
for ind,s in enumerate(S):
    if(s=='?'):
        for i in range(10):
            for j in range(13):
                dp[ind+1][(j*10+i)%13] += dp[ind][j]
                dp[ind+1][(j*10+i)%13] %= mod
    else:
        for j in range(13):
            dp[ind+1][(j*10+int(s))%13] += dp[ind][j]
            dp[ind+1][(j*10+int(s))%13] %= mod
print(dp[n][5])

Solution (Python):


s = input()


def main():


    n = len(s)
    mod = 10**9 + 7

    dp = [[0]* 13 for _ in range(n+1)]
    dp[0][0] = 1

    

    
    
    
    
    for i in range(n):
        if s[i] != '?':

            c = int(s[i]) - int('0')
            for j in range(13):
                i_ = i+1
                j_ = (j*10+c); j_%=13
                dp[i_][j_] += dp[i][j]
                dp[i_][j_] %= mod
        else: 
            for j in range(13):
                for k in range(10):
                    i_ = i+1
                    j_ = j*10+k ; j_ %= 13
                    dp[i_][j_] += dp[i][j]
                    dp[i_][j_] %= mod

        
    print(dp[-1][5])      
     
main()

Solution (Python):
s =list(input())
N = len(s)
mod = pow(10,9)+7
dp = [[0]*13 for i in range(N)]
if s[0] == '?':
        dp[0][0:9] = [1 for i in range(10)]
else:
    dp[0][int(s[0])] = 1
for i in range(1,N):
    if s[i] == '?':
        for j in range(10):
            for m in range (13):
                 dp[i][(m*10+j)%13] += dp[i-1][m]%mod
    else:
        for m in range(13):
            dp[i][(m*10+int(s[i]))%13] += dp[i-1][m]%mod
print(dp[-1][5]% mod)

Solution (Python):
S = input()

l = len(S)
MOD = 10**9+7
dp = [[0 for i in range(13)] for j in range(l+1)]
dp[0][0] = 1
for i in range(l):
    if S[i] == '?':
        for j in range(10):
            for k in range(13):
                dp[i+1][(k*10+j)%13] += dp[i][k] % MOD
    else:
        for k in range(13):
            dp[i+1][(k*10+int(S[i]))%13] += dp[i][k] % MOD

print(dp[-1][5] % MOD)

Solution (Python):
def main():
    M=10**9+7
    r=tuple(tuple((j-k)*9%13for j in range(10))for k in range(13))
    d=[1]+[0]*12
    for c in input():
        if c > '9':
            d = [sum(d[j] for j in k)%M for k in r] 
        else:
            d = [d[(int(c)-k)*9%13] for k in range(13)]
    print(d[5])
main()

Solution (Python):
MOD = int(1e9+7)


def main():
    S = input()
    x = [1] * 6
    for i in range(1, 6):
        x[i] = (x[i-1] * 10) % 13
    dp = [[0] * 13 for _ in range(len(S)+1)]
    dp[0][0] = 1
    for i, c in enumerate(S[::-1]):
        m = x[i % 6]
        if c == "?":
            for j in range(13):
                for k in range(10):
                    dp[i+1][(j+m*k) % 13] += dp[i][j]
            for j in range(13):
                dp[i+1][j] %= MOD
        else:
            for j in range(13):
                dp[i+1][(j+m*int(c)) % 13] = dp[i][j]
    print(dp[len(S)][5])


if __name__ == "__main__":
    main()

Solution (Python):
s = input()
l = len(s)
mod = (10**9)+7

dp = [[0 for j in range(13)] for i in range(l)]

if s[0] == '?':
    dp[0][0:9] = [1 for x in range(10)]
else:
    dp[0][int(s[0])] = 1

for i in range(l-1):
    a = i+1
    x = s[a]
    if x == '?':
        for j in range(10):
            for k in range(13):
                dp[a][(k*10+j)%13] += dp[a-1][k]%mod
    else:
        for k in range(13):
            dp[a][(k*10+int(x))%13] += dp[a-1][k]%mod

print(dp[-1][5]%mod)

Solution (Python):
from functools import reduce

S = input()


M = 13
MOD = 10**9 + 7
ans = reduce(
    lambda acc, c: (
        [
            sum(
                acc[0][(i - acc[1] * j) % M]
                for j in range(10)
            ) % MOD
            for i in range(M)
        ] if c == '?' else
        [
            acc[0][(i - acc[1] * int(c)) % M]
            for i in range(M)
        ],
        (10 * acc[1]) % M
    ),
    S[::-1],
    (
        [
            1 if i == 0 else
            0
            for i in range(M)
        ],
        1
    )
)[0][5]

print(ans)

Solution (Python):
import math
S=input()
dp=[0]*13
dp[0]=1

mod = 1000000007

for i in range(len(S)):
    nextDp=[0]*13
    if S[i] == "?":
        for j in range(13):
            for k in range(10):
                nextDp[((10*j) + k) % 13] += dp[j]
                nextDp[((10*j) + k) % 13] %= mod
    else:
        k = int(S[i])
        for j in range(13):
            nextDp[((10*j) + k) % 13] += dp[j]
            nextDp[((10*j) + k) % 13] %= mod
    for j in range(13):
        dp[j] = nextDp[j]

print(dp[5])

Solution (Python):
MOD=10**9+7
s=input()[::-1]
l=len(s)
dp=[[0 for j in range(13)] for i in range(l)]
if s[0] == "?":
    for j in range(10):dp[0][j]=1
else:
    dp[0][int(s[0])]=1
m=1
for i in range(1,l):
    m=10*m%13
    if s[i] == "?":
        for j in range(10):
            for k in range(13):dp[i][(k+j*m)%13]+=dp[i-1][k]%MOD
    else:
        for k in range(13):
            dp[i][(k+int(s[i])*m)%13]+=dp[i-1][k]%MOD
print(dp[-1][5]%MOD)

Solution (Python):
MOD=10**9+7
s=input()[::-1]
l=len(s)
dp=[[0 for j in range(13)] for i in range(l)]
if s[0] == "?":
    for j in range(10):dp[0][j]=1
else:
    dp[0][int(s[0])]=1
m=1
for i in range(1,l):
    m=10*m%13
    if s[i] == "?":
        for j in range(10):
            for k in range(13):dp[i][(k+j*m)%13]+=dp[i-1][k]%MOD
    else:
        for k in range(13):
            dp[i][(k+int(s[i])*m)%13]+=dp[i-1][k]%MOD
print(dp[-1][5]%(10**9+7))

Solution (Python):
s = input().strip()

DIV = 10**9 + 7
new = list(range(13))
old = None

c = s[-1]
if c == '?':
    old = [int(t < 10) for t in range(13)]
else:
    c = int(c)
    old = [int(t == c) for t in range(13)]

digit = 10
for i, c in enumerate(reversed(s[:-1])):
    for j in range(13):
        new[j] = 0
    if c == '?':
        for m in range(10):
            for mod, cnt in enumerate(old):
                shifted = (mod + m * digit) % 13
                new[shifted] += cnt
    else:
        c = int(c)
        for mod, cnt in enumerate(old):
            shifted = (mod + c * digit) % 13
            new[shifted] += cnt
    for j in range(13):
        new[j] %= DIV
    new, old = old, new
    digit = (digit * 10) % 13

print(old[5])

Solution (Python):
M = 10**9+7
r = tuple(tuple((j-i) * 4 % 13 for i in range(10)) for j in range(13))
d = [1] + [0] * 12
for c in input():
    if c > "9":
        d = [(sum(d[i] for i in j)) % M for j in r]
    else:
        d = [d[((j - int(c)) * 4 % 13)] for j in range(13)]
print(d[5])

Solution (Python):
import sys
def input():
    return sys.stdin.readline()[:-1]
S = list(input())
u = 10**9+7
d = len(S)
dp = [0]*13
dp[0] = 1
m = 1

for i in range(d-1,-1,-1):
    tmp = [0]*13
    a = S[i]
    if a=='?':
        for j in range(10):
            c = j*m
            for k in range(13):
                n = (c+k)%13
                tmp[n] += dp[k]
                tmp[n] %= u
    else:
        c = int(a)*m
        for k in range(13):
            n = (c+k)%13
            tmp[n] += dp[k]
            tmp[n] %= u
    m *= 10
    m %= 13
    dp = tmp
print(dp[5])

Solution (Python):

import sys
from collections import deque, defaultdict
import copy
import bisect
sys.setrecursionlimit(10 ** 9)
import math

S = input()
amari = [1, 10, 9, 12, 3, 4]
kurai = 0
hatena = []
base = 0
for i in range(len(S)):
	
	if S[len(S) - 1 - i] == '?':
		hatena.append(kurai)
	else:
		base += (int(S[len(S) - 1 - i])*amari[i % 6])
		base = base % 13
	kurai += 1


count13 = [0]*13
count13[base]+=1
for kurai in hatena:
	countnow = [0]*13
	for num in range(10):
		x = num * amari[kurai % 6]
		for i in range(13):
			y = (i + x) % 13
			countnow[y] += count13[i]
	for i in range(13):
		countnow[i] = countnow[i] % 1000000007
	count13 = copy.deepcopy(countnow)

print(count13[5])

Solution (Python):
def main():

    """ convenient functions
    
    
    
    
    
    """

    """convenient decorator
    
    
        
        
        
        
        
        
        
        
        
        
    """
    
    
    import sys
    sys.setrecursionlimit(10**7)
    from itertools import accumulate, combinations, permutations, product 
    
    from math import factorial, ceil, floor, sqrt
    def factorize(n):
        """return the factors of the Arg and count of each factor
        
        Args:
            n (long): number to be resolved into factors
        
        Returns:
            list of tuples: factorize(220) returns [(2, 2), (5, 1), (11, 1)]
        """
        fct = []  
        b, e = 2, 0  
        while b * b <= n:
            while n % b == 0:
                n = n // b
                e = e + 1
            if e > 0:
                fct.append((b, e))
            b, e = b + 1, 0
        if n > 1:
            fct.append((n, 1))
        return fct
    def combinations_count(n, r):
        """Return the number of selecting r pieces of items from n kinds of items.
        
        Args:
            n (long): number
            r (long): number
        
        Raises:
            Exception: not defined when n or r is negative
        
        Returns:
            long: number
        """
        
        if n < 0 or r < 0:
            raise Exception('combinations_count(n, r) not defined when n or r is negative')
        if n - r < r: r = n - r
        if r < 0: return 0
        if r == 0: return 1
        if r == 1: return n
        numerator = [n - r + k + 1 for k in range(r)]
        denominator = [k + 1 for k in range(r)]
        for p in range(2,r+1):
            pivot = denominator[p - 1]
            if pivot > 1:
                offset = (n - r) % p
                for k in range(p-1,r,p):
                    numerator[k - offset] /= pivot
                    denominator[k] /= pivot
        result = 1
        for k in range(r):
            if numerator[k] > 1:
                result *= int(numerator[k])
        return result
    def combinations_with_replacement_count(n, r):
        """Return the number of selecting r pieces of items from n kinds of items allowing individual elements to be repeated more than once.
        
        Args:
            n (long): number
            r (long): number
        
        Raises:
            Exception: not defined when n or r is negative
        
        Returns:
            long: number
        """
        if n < 0 or r < 0:
            raise Exception('combinations_with_replacement_count(n, r) not defined when n or r is negative')
        elif n == 0:
            return 1
        else:
            return combinations_count(n + r - 1, r)
    from bisect import bisect_left, bisect_right
    from collections import deque, Counter, defaultdict 
    from heapq import heapify, heappop, heappush, heappushpop, heapreplace,nlargest,nsmallest 
    from copy import deepcopy, copy 
    import operator
    from operator import itemgetter
    
    
    from functools import reduce, lru_cache
    def chmin(x, y):
        """change minimum
        if x > y, x = y and return (x, True).
        convenient when solving problems of dp[i]
        
        Args:
            x (long): current minimum value
            y (long): potential minimum value
        
        Returns:
            (x, bool): (x, True) when updated, else (x, False)
        """
        if x > y:
            x = y
            return (x, True)
        else:
            return (x, False)
    def chmax(x, y):
        """change maximum
        if x < y, x = y and return (x, True).
        convenient when solving problems of dp[i]
        
        Args:
            x (long): current maximum value
            y (long): potential maximum value
        
        Returns:
            (x, bool): (x, True) when updated, else (x, False)
        """
        if x < y:
            x = y
            return (x, True)
        else:
            return (x, False)

    from fractions import gcd 
    def gcds(numbers):
        return reduce(gcd, numbers)
    def lcm(x, y):
        return (x * y) // gcd(x, y)
    def lcms(numbers):
        return reduce(lcm, numbers, 1)

    
    
    INF = 10 ** 18
    MOD = 10 ** 9 + 7
    modpow = lambda a, n, p = MOD: pow(a, n, p) 
    def modinv(a, p = MOD):
        
        
        return modpow(a, p-2, p)
    def modinv_list(n, p = MOD):
        if n <= 1:
            return [0,1][:n+1]
        else:
            inv_t = [0,1]
            for i in range(2, n+1):
                inv_t += [inv_t[p % i] * (p - int(p / i)) % p]
            return inv_t
    def modfactorial_list(n, p = MOD):
        if n == 0:
            return [1]
        else:
            l = [0] * (n+1)
            tmp = 1
            for i in range(1, n+1):
                tmp = tmp * i % p
                l[i] = tmp
            return l
    def modcomb(n, k, fac_list = [], p = MOD):
        
        
        from math import factorial
        if n < 0 or k < 0 or n < k: return 0
        if n == 0 or k == 0: return 1
        if len(fac_list) <= n:
            a = factorial(n) % p
            b = factorial(k) % p
            c = factorial(n-k) % p
        else:
            a = fac_list[n]
            b = fac_list[k]
            c = fac_list[n-k]
        return (a * modpow(b, p-2, p) * modpow(c, p-2, p)) % p
    def modadd(a, b, p = MOD):
        return (a + b) % MOD
    def modsub(a, b, p = MOD):
        return (a - b) % p
    def modmul(a, b, p = MOD):
        return ((a % p) * (b % p)) % p
    def moddiv(a, b, p = MOD):
        return modmul(a, modpow(b, p-2, p))

    class UnionFindTree:
        """union find tree class
        TODO: fix this description...
        how to use (example):
        >>  uf = UnionFindTree(N) 
        >>  if uf.find_root(a) == uf.find_root(b):
        >>      do something
        >>  else:
        >>      do something
        >>      uf.unite(a, b)
        """
        def __init__(self, N):
            self.root = [-1] * (N+1)
            self.rank   = [0]  * (N+1)
            self.connected_num = [1] * (N+1)
    
        def find_root(self,x):
            root = self.root
            while root[x] != -1:
                x = root[x]
            return x
    
        def unite(self,x,y):
            root = self.root
            rank = self.rank
            connected_num = self.connected_num
            find_root = self.find_root
    
            rx = find_root(x)
            ry = find_root(y)
            if rx != ry:
                if rank[rx] < rank[ry]:
                    root[rx] = ry
                    rx,ry = ry,rx
                else:
                    if rank[rx] == rank[ry]:
                        rank[rx] += 1
                    root[ry] = rx       
                connected_num[rx] += connected_num[ry]

    """ initialize variables and set inputs
    
        
        
    
        
        
        
        
        
    
        
        
        
        
    
        
        
    """

    
    r = lambda: sys.stdin.readline().strip()
    r_int = lambda: int(r())
    R = lambda: list(map(int, r().split()))
    Rmap = lambda: map(int, r().split())
    Rfloat = lambda: list(map(float, r().split()))
    Rtuple = lambda: tuple(map(int, r().split()))

    """ how to treat input
    
    
    
    
    
    
    
    """

    if __name__ != '__main__':
        
        sys.stdin = open('sample.txt')
        
    
    

    S = r()
    lenS = len(S)
    target_digit_S = lenS - 1
    dp = [[0]*13 for _ in range(lenS)] 
    reslist = [10, 9, 12, 3, 4, 1]
    
    
    res = reslist[(target_digit_S-1) % 6] 
    if S[0] == '?':
        for i in range(10):
            dp[0][(i*res)%13] += 1
    else:
        intS = int(S[0])
        dp[0][(intS*res)%13] += 1
    
    for i in range(1, lenS):
        target_digit_S -= 1
        res = reslist[(target_digit_S-1) % 6] 
        if S[i] == '?':
            for k in range(10):
                for j in range(13):
                    dp[i][(k*res+j)%13] = (dp[i][(k*res+j)%13] + dp[i-1][j]) % MOD
        else:
            intS = int(S[i])
            for j in range(13):
                dp[i][(intS*res+j)%13] += (dp[i][(intS*res+j)%13] + dp[i-1][j]) % MOD
    print(dp[lenS-1][5])
    
    
    

    """memo: how to use defaultdict of list
    
    Dic = defaultdict(list)
    
    Dic[x].append(y)
    
    for k, v in Dic.items():
    """
    
    """memo: how to solve binary problems
    
    >>> a = 5
    >>> bin_str_a = format(a, '
    >>> print(bin_str_a)
    0b0101 
    """

if __name__ == '__main__':
    main()

Solution (Python):




N = 13
MOD = 10 ** 9 + 7

s = input()[::-1]

dp = [0 for i in range(N)]
nextdp = [0 for i in range(N)]
zeros = [0 for i in range(N)]
dp[0] = 1

r = 1
for i, si in enumerate(s):
    nextdp = zeros[:]

    for j in range(10):
        jj = j
        if si == "?":
            pass
            
        elif j == 0:
            jj = int(si)
        else:
            break

        for k in range(N):
            l = (k + r * jj) % N
            nextdp[l] = (nextdp[l] + dp[k]) % MOD

    dp = nextdp
    r = r * 10 % N

print(dp[5])

Solution (Python):
S = input()
dp = [[0] * 13 for _ in range(len(S) + 1)]
mod = pow(10, 9) + 7

dp[0][0] = 1
for i in range(1, len(dp)):
    c = int(S[i-1]) if S[i-1] != '?' else -1
    for j in range(13):
        for k in range(10):
            if c != -1 and c != k:
                continue
            dp[i][(j*10+k) % 13] += dp[i-1][j]
    for j in range(13):
        dp[i][j] %= mod

print(dp[-1][5])

Solution (Python):
import copy

S = input()

dp = [0] * 13

dp[0] = 1

mod = 10 ** 9 + 7
mul = 1
for i in range(len(S)-1, -1, -1):
    
    ndp = [0] * 13
    
    if S[i] == '?':
        for k in range(10):
            for j in range(13):
                
                
                
                ndp[(k * mul + j) % 13] += dp[j]
                ndp[(k * mul + j) % 13] %= mod
    else:
        k = int(S[i])
        for j in range(13):
            
            ndp[(k * mul + j) % 13] += dp[j]
            ndp[(k * mul + j) % 13] %= mod

    mul *= 10
    mul %= 13
    dp = copy.deepcopy(ndp)


print(dp[5])

Solution (Python):
def main():
    mod = 10**9+7
    s = list(input())
    n = len(s)
    s = s[::-1]    

    dp = [[0]*13 for i in range(n)]

    if s[0] == '?':
        for i in range(10):
            dp[0][i] = 1
    else:
        dp[0][int(s[0])] = 1

    r = 1
    for i in range(1,n):
        r = r*10%13
        if s[i] == '?':
            for j in range(10):
                q = r*j%13
                for k in range(13):
                    dp[i][k] = (dp[i][k]+dp[i-1][(k-q)%13])%mod
        else:
            q = r*int(s[i])%13
            for k in range(13):
                dp[i][k] = (dp[i][k]+dp[i-1][(k-q)%13])%mod
    print(dp[-1][5])

if __name__ == '__main__':
    main()

Solution (Python):
d=[0]*13
d[0]=1
m=1
mmod=1
t=input()[::-1]
mod=10**9+7
zero=[0]*13

for c in t:
    e=zero[:]
    for i in (range(10) if c=="?" else[int(c)]):
        for j in range(13):
            
            k=(i*m+j)%13
            e[k]+=d[j]
            e[k]%=1000000007
    d=e
    m=10*m%13
print(d[5])

Solution (Python):
d=[0]*13
d[0]=1
m=1
mmod=1
t=input()[::-1]
mod=10**9+7
zero=[0]*13

for c in t:
    e=zero[:]
    if c=="?":
        for i in range(10):
            for j in range(13):
                k=(i*m+j)%13
                e[k]+=d[j]
                e[k]%=mod
                
                
                
                
                
                
                
                
    else:
        for j in range(13):
            k=(int(c)*m+j)%13
            e[k]+=d[j]
            e[k]%=mod
            
            
            
            
            
            
            
            
            
    d=e
    m=10*m%13
print(d[5])

Solution (Python):
d=[0]*13
d[0]=1
m=1
mmod=1
t=input()[::-1]
mod=10**9+7
zero=[0]*13

for c in t:
    e=zero[:]
    if c=="?":
        for i in range(10):
            for j in range(13):
                k=(i*m+j)%13
                e[k]+=d[j]
                e[k]%=mod
    else:
        for j in range(13):
            k=(int(c)*m+j)%13
            e[k]+=d[j]
            e[k]%=mod
    d=e
    m=10*m%13
print(d[5])

Solution (Python):
d=[0]*13
d[0]=1
mul=1
mulmod=1
t=input()[::-1]
mod=10**9+7
zero=[0]*13

for c in t:
    e=zero[:]
    if c=="?":
        for i in range(10):
            for j in range(13):
                k=(i*mul+j)%13
                e[k]+=d[j]
                e[k]%=mod
    else:
        for j in range(13):
            k=(int(c)*mul+j)%13
            e[k]+=d[j]
            e[k]%=mod
    d=e
    mul=10*mul%13
print(d[5])

Solution (Python):
S = input()[::-1]
P = 10**9+7
c = 1
X = [1]+[0]*12
for s in S:
    Y = [0] * 13
    if s == "?":
        for i in range(10):
            for j in range(13):
                Y[(c*i+j)%13] += X[j]
                Y[(c*i+j)%13] %= P
    else:
        for j in range(13):
            Y[(c*int(s)+j)%13] = X[j]
            
    X = Y
    c = c*10%13
 
print(X[5])

Solution (Python):
import sys
from collections import deque
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
MAX = 10**5+5
INF = 10**9
P = 1000000007

def resolve():
    s = input().strip()
    dp = [[0]*13 for _ in range(MAX)]
    dp[0][0]=1
    for i,x in enumerate(s):
        if x=='?':
            c=-1
        else:
            c=int(x)
        for j in range(10):
            if(c!=-1 and c!=j):
                continue
            for k in range(13):
                t=(k*10+j)%13
                dp[i+1][t] = dp[i+1][t]+dp[i][k]
        for j in range(13):
            dp[i+1][j]=dp[i+1][j]%P
    l=len(s)
    print(dp[l][5]%P)


if __name__ == "__main__":
    resolve()

Solution (Python):
s = input()

dp = [[0 for i in range(13)] for j in range(len(s))]
mod = 10 ** 9 + 7

for i, s_i in enumerate(s):
    if i == 0:
        if s_i == "?":
            for k in range(10):
                dp[0][k] = 1
        else:
            dp[0][int(s_i)] = 1

    else:
        if s_i == "?":
            for k1 in range(10):
                for k2 in range(13):
                    dp[i][(k2 * 10 + k1) % 13] += dp[i-1][k2] % mod

        else:
            for k in range(13):
                dp[i][(k*10+int(s_i))%13] += dp[i-1][k] % mod


print(dp[-1][5] % mod)

Solution (Python):


 
import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline 
 
s = input()
l = len(s)
MOD = 10**9+7
 
dp = [0]*13
dp[0] = 1
for i in s:
    new = [0]*13
    if i == "?":
        for i in range(10):
            for j in range(13):
                new[(10*j+i)%13] += dp[j]
    else:
        i = int(i)
        for j in range(13):
            new[(i+10*j)%13] += dp[j]
    
    for i in range(13):
        new[i] %= MOD
    dp,new = new,dp

 
print(dp[5]%MOD)

Solution (Python):


def main():
    S = input()
    
    lenS = len(S)
    N = 13
    DP = [0] * N
    DP[0] = 1
    
    mod = 10 ** 9 + 7
    mul = 1
    for i in reversed(range(lenS)):
        nextDP = [0] *13
        c = S[i]
        if c == '?':
            for k in range(10):
                for j in range(N):
                    nextDP[(k * mul + j) %N] += DP[j]
                    nextDP[(k * mul + j) %N] %= mod

        else:
            k = int(S[i])
            for j in range(N):
                nextDP[(k * mul + j) %N] += DP[j]
                nextDP[(k * mul + j) %N] %= mod

        mul *= 10
        mul %= N
        DP = nextDP

    return DP[5]

if __name__ == '__main__':
    print(main())

Solution (Python):
M=10**9+7
r=tuple(tuple((j-i)*4%13 for i in range(10))for j in range(13))
d=[1]+[0]*12
for c in input():
  if c>"9":
    d=[(sum(d[i] for i in j))%M for j in r]
  else:
    d=[d[((j-int(c))*4%13)] for j in range(13)]
    
print(d[5])