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 in C# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
using static System.Console;
using static System.Math;
using System.IO;
using System.Collections.Generic;
namespace I_am_the_green_coder{
    class Inonoa{
        static void Main(){
            char[] s = IChars();

            long[,] qs = new long[2,3];
            long[] sums = new long[2];

            long[] dp = new long[13];
            dp[0] = 1;

            int d = (s.Length-1)%6;

            for(int i=0;i<s.Length;i++){
                long[] dptmp = new long[13];
                if(s[i]=='?'){
                    if(d==0){
                        for(int j=0;j<10;j++){
                            for(int k=0;k<13;k++){
                                dptmp[(k+j)%13] += dp[k];
                            }
                        }
                    }else if(d==1){
                        for(int j=0;j<10;j++){
                            for(int k=0;k<13;k++){
                                dptmp[(k+j*10)%13] += dp[k];
                            }
                        }
                    }else if(d==2){
                        for(int j=0;j<10;j++){
                            for(int k=0;k<13;k++){
                                dptmp[(k+j*100)%13] += dp[k];
                            }
                        }
                    }else if(d==3){
                        for(int j=0;j<10;j++){
                            for(int k=0;k<13;k++){
                                dptmp[(k-j+13)%13] += dp[k];
                            }
                        }
                    }else if(d==4){
                        for(int j=0;j<10;j++){
                            for(int k=0;k<13;k++){
                                dptmp[(k-j*10+130)%13] += dp[k];
                            }
                        }
                    }else if(d==5){
                        for(int j=0;j<10;j++){
                            for(int k=0;k<13;k++){
                                dptmp[(k-j*100+1300)%13] += dp[k];
                            }
                        }
                    }
                }else{
                    int dgt = int.Parse(s[i].ToString());
                    if(d==0){
                        for(int j=0;j<13;j++){
                            dptmp[(j+dgt)%13] = dp[j];
                        }
                    }else if(d==1){
                        for(int j=0;j<13;j++){
                            dptmp[(j+dgt*10)%13] = dp[j];
                        }
                    }else if(d==2){
                        for(int j=0;j<13;j++){
                            dptmp[(j+dgt*100)%13] = dp[j];
                        }
                    }else if(d==3){
                        for(int j=0;j<13;j++){
                            dptmp[(j-dgt+13)%13] = dp[j];
                        }
                    }else if(d==4){
                        for(int j=0;j<13;j++){
                            dptmp[(j-dgt*10+130)%13] = dp[j];
                        }
                    }else if(d==5){
                        for(int j=0;j<13;j++){
                            dptmp[(j-dgt*100+1300)%13] = dp[j];
                        }
                    }
                }
                d--;
                if(d==-1){
                    d = 5;
                }
                for(int j=0;j<13;j++){
                    dp[j] = dptmp[j]%MOD;
                }
            }

            Console.WriteLine(dp[5]);
        }

        static readonly long MOD = 1000000007;

        ///<summary>a_1, a_2, a_3, ...みたいなの</summary>
        static long[] ILongs(){
            string[] s = ReadLine().Split();
            long[] re = new long[s.Length];
            for(long i=0;i<s.Length;i++){
                re[i] = long.Parse(s[i]);
            }
            return re;
        }

        ///<summary>xooxoxoxxoxo...みたいなの</summary>
        static char[] IChars(){
            return ReadLine().ToCharArray();
        }

        ///<summary>n m とかをちょっとはやくかくだけ</summary>
        static string[] IStrings(){
            return ReadLine().Split();
        }

        ///<summary>n行の入力を取ったり</summary>
        static long[][] ILongLongs(long n){
            long[][] re = new long[n][];
            for(long i=0;i<n;i++){
                string[] s = ReadLine().Split();
                re[i] = new long[s.Length];
                for(long j=0;j<s.Length;j++){
                    re[i][j] = long.Parse(s[j]);
                }
            }
            return re;
        }

        ///<summary>n行の文字列を取ったり</summary>
        static char[][] ICharsChars(long n){
            char[][] re = new char[n][];
            for(long i=0;i<n;i++){
                re[i] = ReadLine().ToCharArray();
            }
            return re;
        }
    }
}