All problems

Problem 951

Decode Ways II

Hard · 30 pts · String, Dynamic Programming

Problem

Letters are encoded as numbers: A = 1, B = 2, ..., Z = 26. An encoded message contains digits and the character '*', which stands for any digit from 1 to 9. Count the ways to decode the whole message into letters, modulo 1000000007. A part cannot start with 0, so 06 is invalid. Let ways[i] be the count for the first i characters, adding the ways to decode the last character alone and the last two characters together, counting the choices each '*' allows. Keep the counts in a 64-bit integer type (long long in C++, long in Java) and take the remainder after every addition and multiplication: adding or multiplying two remainders can be larger than a 32-bit int can hold (Python needs nothing special).

Input

A single string S of digits and '*'.

Output

Print the number of decodings modulo 1000000007.

Constraints

1 <= |S| <= 100000

Example input

*

Example output

9

Explanation

A single * can be any of 1 to 9, giving 9 decodings. For 1*, the message is either 1 followed by one of 9 letters, or one of 11 to 19 as a single letter, so there are 18.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Combination Sum IV Next: Arithmetic Slices