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++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include<iostream>
#include<cmath>
#include<cstdlib>
#include<string>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
#include<iomanip>

using namespace std;

typedef long long ll;

typedef std::pair<int, int> ipair;
bool lessPair(const ipair& l, const ipair& r){return l.second < r.second;}
bool morePair(const ipair& l, const ipair& r){return l.second > r.second;}

template<class T> inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template<class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

const ll MOD = 1e9 + 7;
// const long long INF = 1LL<<60;
void add(long long &a, long long b) { a += b; if (a >= MOD) a -= MOD; }
void sub(long long &a, long long b) { a -= b; if (a < 0) a += MOD; }
void mul(long long &a, long long b) { a *= b; a %= MOD; }
ll llmin(ll a, ll b) { if (a < b) return a; else return b; }
ll llmax(ll a, ll b) { if (a < b) return b; else return a; }
ll llabs(ll a) { if (a >= 0) return a; else return - a; }
ll llmodpow(ll a, ll n) {
   if (n == 0) return 1;
   ll tmp = llmodpow(a, n / 2);
   mul(tmp, tmp);
   if (n & 1) mul(tmp, a);
   return tmp;
}

int amari[13];

void calc(int a, int b, int c) {
   memset(amari, 0, sizeof(amari));
   int d, e, f;
   for (int i = 0; i <= 9; i++) {
      if (a == -1) d = i;
      else d = a;
      for (int j = 0; j <= 9; j++) {
         if (b == -1) e = j;
         else e = b;
         for (int k = 0; k <= 9; k++) {
            if (c == -1) f = k;
            else f = c;
            int tmp = 100 * d + 10 * e + f;
            amari[tmp % 13]++;
            if (c != -1) break;
         }
         if (b != -1) break;
      }
      if (a != -1) break;
   }
}
int ctoi(const char c){
  switch(c){
    case '0': return 0;
    case '1': return 1;
    case '2': return 2;
    case '3': return 3;
    case '4': return 4;
    case '5': return 5;
    case '6': return 6;
    case '7': return 7;
    case '8': return 8;
    case '9': return 9;
    default : return -1;
  }
}
int main() {
   string S;
   cin >> S;
   int N = (int)S.size();
   int M = N / 3 + N % 3;
   ll dp[13][M + 1];
   memset(dp, 0, sizeof(dp));
   for (int m = 1; m <= M; m++) {
      int a, b, c;
      int id;
      id = N - 1 - (m - 1) * 3;
      // cout << id << endl;
      if (S[id] == '?') a = -1;
      else a = ctoi(S[id]);
      id--;
      if (S[id] == '?') b = -1;
      else b = ctoi(S[id]);
      id--;
      if (S[id] == '?') c = -1;
      else c = ctoi(S[id]);
      // cout << c << " " << b << " " << a << endl; 
      calc(c, b, a);
      if (m == 1) {
         for (int i = 0; i < 13; i++) {
            dp[i][m] = amari[i];
         }
      } else {
         if (m % 2 == 0) {
            for (int i = 0; i < 13; i++) {
               for (int j = 0; j < 13; j++) {
                  int k = (i + 13 - j) % 13;
                  dp[i][m] += dp[j][m - 1] * amari[(13 - k) % 13];
                  dp[i][m] %= MOD;
               }
            }
         } else {
            for (int i = 0; i < 13; i++) {
               for (int j = 0; j < 13; j++) {
                  int k = (i + 13 - j) % 13;
                  dp[i][m] += dp[j][m - 1] * amari[k];
                  dp[i][m] %= MOD;
               }
            }
         }
      }
      // for (int i = 0; i < 13; i++) cout << amari[i] << " ";
      // cout << endl;
      // for (int i = 0; i < 13; i++) cout << dp[i][m] << " ";
      // cout << endl;
   }
   ll ans = dp[5][M];
   if (ans < 0) ans += MOD;
   cout << ans << endl; 
   return 0;
}