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 Scala (2.11.7) (judge verdict: Wrong Answer)

import java.io._
import java.util.StringTokenizer
import scala.util.control.Breaks.{breakable,break}
import scala.collection.mutable.{Map, Queue, PriorityQueue}

object Main extends App {
  def repeatSquares(x: Long, n: Int, mod: Int): Long = {
    if(n == 0)
      1
    else if(n % 2 == 0)
      repeatSquares(x*x % mod, n/2, mod)
    else
      (x * repeatSquares(x*x % mod, n/2, mod)) % mod
  }


  val in = new InputReader(System.in)
  val pw = new PrintWriter(System.out)

  
  val _S = in.next()

  //
  val MOD = 1e9.toInt + 7

  val N = _S.length
  val S = "0" + _S.reverse
  //val a = new Array[Int](N)


  val dp = Array.ofDim[Long](N+1, 13)
  dp(0)(0) = 1

  for (i <- 1 to N) {
    val _now = S(i)
    val base = repeatSquares(10, i-1, MOD)

    if (_now == '?') {
      for (now <- 0 to 9) {
        for (k <- 0 until 13) {
          if (dp(i-1)(k) > 0) {
            val hoge = ((now * base + k) % 13).toInt
            dp(i)(hoge) += dp(i-1)(k)
            dp(i)(hoge) %= MOD
          } 
        }
      }
    }
    else {
      val now = _now.toInt - '0'.toInt

      for (k <- 0 until 13) {
        if (dp(i-1)(k) > 0) {
          val hoge = ((now * base + k) % 13).toInt
          dp(i)(hoge) += dp(i-1)(k)
          dp(i)(hoge) %= MOD
        }
      }
    }
  }

  //dp.foreach(a => println(a.deep))
  println(dp(N)(5))
}


class InputReader(stream: InputStream) {
  private val reader = new BufferedReader(new InputStreamReader(stream))
  private var tokenizer: StringTokenizer = new StringTokenizer(reader.readLine())
  def next(): String = {
    while (!tokenizer.hasMoreTokens()) {
      tokenizer = new StringTokenizer(reader.readLine())
    }
    tokenizer.nextToken()
  }
}