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 System.Collections.Generic;
using System.Linq;
class Program
{
    static string S;
    static void Main()
    {
        S = Console.ReadLine();
      Modular[,] amarisave=new Modular[S.Length,13];//その桁の余りが０の場合～１２の場合について，ケース数をセーブ
for(int i=0;i<S.Length;i++){for(int j=0;j<13;j++){amarisave[i,j]=new Modular(0);}}
      for(int i=0;i<S.Length;i++)
{
      
  //上から一桁ずつやっていく。     
if(S[i]=='?'){
if(i>0){
  for(int l=0;l<10;l++){
              for(int j=0;j<13;j++){
              for(int k=0;k<13;k++){
if((j*10+l)%13==k){amarisave[i,k]+=amarisave[i-1,j];}
              }
              }
  }
}
  else
  {
  for(int j=0;j<10;j++){amarisave[i,j]=1;}
  }
}
        else{
          if(i>0){
            for(int j=0;j<13;j++){
              for(int k=0;k<13;k++){
if((j*10+S[i]-'0')%13==k){amarisave[i,k]=amarisave[i-1,j];}
              }
              }
          }else{amarisave[0,S[i]-'0']=1;}
        }
}

        Console.WriteLine(((int)amarisave[S.Length-1,5]).ToString());
        
    }

 


}

class Modular
{
    private const int M = 1000000007;
    private long value;
    public Modular(long value) { this.value = value; }
    public static implicit operator Modular(long a)
    {
        var m = a % M;
        return new Modular((m < 0) ? m + M : m);
    }
    public static Modular operator +(Modular a, Modular b)
    {
        return a.value + b.value;
    }
    public static Modular operator -(Modular a, Modular b)
    {
        return a.value - b.value;
    }
    public static Modular operator *(Modular a, Modular b)
    {
        return a.value * b.value;
    }
    private static Modular Pow(Modular a, int n)
    {
        switch (n)
        {
        case 0:
            return 1;
        case 1:
            return a;
        default:
            var p = Pow(a, n / 2);
            return p * p * Pow(a, n % 2);
        }
    }
    public static Modular operator /(Modular a, Modular b)
    {
        return a * Pow(b, M - 2);
    }
    private static readonly List<int> facs = new List<int> { 1 };
    private static Modular Fac(int n)
    {
        for (int i = facs.Count; i <= n; ++i)
        {
            facs.Add((int)(Math.BigMul(facs.Last(), i) % M));
        }
        return facs[n];
    }
    public static Modular Ncr(int n, int r)
    {
        return (n < r)  ? 0
             : (n == r) ? 1
                        : Fac(n) / (Fac(r) * Fac(n - r));
    }
    public static explicit operator int(Modular a)
    {
        return (int)a.value;
    }
}