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 Haskell (GHC 7.10.3) (judge verdict: Time Limit Exceeded)

{-# LANGUAGE BangPatterns #-}
import Control.Monad
import Data.Maybe
import Debug.Trace
import Data.List
import qualified Data.Map.Strict as Map
import qualified Data.IntMap.Strict as IntMap
import qualified Data.Set as Set
import qualified Data.IntSet as IntSet
import Data.Functor
-- import Data.Array
import Data.Array.Unboxed
import Control.Monad.ST
import Data.Array.ST
import Data.Char

main :: IO ()
main = getLine >>= print . solve

solve = (!5) . foldl' f initial . zip [0..] . reverse 
  where
    initial = listArray (0, 12) $ 1:replicate 12 0
    f :: Array Int Int -> (Int, Char) -> Array Int Int
    f prev (i, c)
      | c == '?'  = runSTArray $ do
        arr <- newArray (0, 12) 0
        forM_ [0..9] $ \i -> do
          forM_ [0..12] $ \j -> do
            let d = (i * tens + j) `mod` 13
            v <- readArray arr d
            writeArray arr d $ v `modadd` (prev!j)
        return arr
      | otherwise = listArray (0, 12) $ map g [0..12]
        where
          si = digitToInt c
          tens = power 10 i
          rem = (si * tens) `mod` 13
          g j = prev ! ((j - rem) `mod` 13)

divConst = 10^9 + 7
modadd x y = (x + y) `mod` divConst
modsub x y = (x - y) `mod` divConst
modmul x y = ((x `mod` divConst) * (y `mod` divConst)) `mod` divConst
moddiv x y = modmul x (power y (divConst - 2))

power x y
  | y == 0 = 1
  | y == 1 = x `mod` 13
  | y `mod` 2 == 0 = (power x (y `div` 2))^2 `mod` 13
  | otherwise = (power x (y `div` 2))^2 * x `mod` 13