This page is still under construction.

Parts of this page are still being built. What you see may change.

Math Homework

Time limit1sMemory limit128 MB

Summary
Count N-digit strings, leading zeros allowed, whose divisibility by each of 1 to 6 matches a given pattern, modulo 1e9+7.
Level

Medium7 of 10

Topics
Matrix, Number theory, Dynamic programming
Solved
No attempts yet

Problem

Dongil got a hard math assignment and it is due tomorrow, so barely any time is left.

The assignment is division practice. The goal is to learn the process of dividing an NN digit number by a small number. An NN digit number here may start with 0, and a number written with leading zeros still counts as an NN digit number.

Every question on the assignment has the same shape.

How many two digit integers are divisible by 6 and not divisible by 5?

The answers to that question are 06, 12, 18, 24, 36, 42, 48, 54, 66, 72, 78, 84 and 96, so there are 13 of them.

Note that 0 is divisible by every positive integer.

Too many numbers satisfy the conditions for Dongil to list them one by one, so he decided to find only how many there are.

Input

The first line contains the number of test cases TT. (1≤T≤10001 \le T \le 1000)

Each of the next TT lines contains an integer NN and a string of length 6, separated by a space.

NN means the numbers to count have NN digits. (1≤N≤10181 \le N \le 10^{18})

The string consists only of the characters 0, 1 and 2. If its ii-th character is 0, the number must not be divisible by ii. If it is 1, the number must be divisible by ii. If it is 2, divisibility by ii does not matter. (1≤i≤61 \le i \le 6)

Output

For each test case, print on one line the number of NN digit numbers that satisfy the conditions, modulo 1,000,000,007.

Examples1

  1. Example 1

    Input
    4
    2 222201
    1 111001
    1 111111
    2 222222
    
    Expected output
    13
    1
    1
    100