eolymp
bolt
Try our new interface for solving problems
Problems

Intervals

Intervals

An integer number is called good if it consists of good digits.

You are given n intervals [li, ri] and the set of digits which are considered good. Find how many ways there are to choose one integer from each of those intervals so that their sum will be a good number. You can choose the same number in multiple intervals. Two ways are considered different if there exists an integer i (1in) such that different integers are chosen from interval i. The answer may be large, so you have to give it modulo 109 + 7.

Input

The first line contains ten integers: i-th of them is equal to 1 if digit i is good and 0 otherwise. These integers are numbered from 0 to 9. The next line contains a single integer n (1n7): the number of intervals. Next n lines contain two integers each without leading zeros: li and ri (0liri < 1010).

Output

Print the number of ways modulo 109 + 7.

Time limit 2 seconds
Memory limit 128 MiB
Input example #1
1 1 1 1 1 1 1 1 1 1
2
4 6
15 19
Output example #1
15
Input example #2
1 0 1 0 1 0 1 0 0 0
2
4 6
15 19
Output example #2
7
Input example #3
1 0 0 1 1 1 1 1 1 1
2
4 6
15 19
Output example #3
0
Source 2013 Petrozavodsk, Moscow SU ST + NNSU Contest, Day 2, August 24, Problem H