ABCD Strings

5

4 votes
Graph Theory, Linear Algebra, Hard, Matrix Exponentiation, Algorithms, Recursion, Mathematics, Open, Approved
Problem

Please find the number of N-length strings that are made of only letters 'a, 'b', 'c' and 'd' and do not contain any of the restricted strings.

Since, the answer can be very large, please print it modulo 1000000007.

Input Format:

The first line of input file contains T, the number of test cases to follow.
The first line of each test case contains 2 space-separated integers N and K.
The next K lines each contain a string S, which represents a restricted string.

Output Format:

Output T lines, each containing answer to corresponding test case.

Constraints:

T ≤ 200
N ≤ 1015
K ≤ 50
|S| ≤ 3

Sample Input
5
4 8
cac
dbb
abb
ddd
dad
aaa
dbb
cad
3 1
aca
2 7
abb
aac
aab
dcc
bbc
caa
cba
1 4
cbb
ddd
ccb
cdc
5 5
aad
bda
aac
ddb
bdc
Sample Output
202
63
16
4
789
Time Limit: 5
Memory Limit: 256
Source Limit:
Editor Image

?