atcoder#ABC305G. [ABC305G] Banned Substrings

[ABC305G] Banned Substrings

Score : 550550 points

Problem Statement

You are given a set S={s1,s2,,sM}S=\lbrace s _ 1,s _ 2,\ldots,s _ M\rbrace of non-empty strings of length at most 66 consisting of a and b. Find the number of strings TT of length NN consisting of a and b that satisfy the following condition:

  • TT does not contain sis _ i as a consecutive substring for any siSs _ i\in S.

Since the answer can be enormous, find it modulo 998244353998244353.

Constraints

  • 1N10181\leq N\leq10^{18}
  • 1M1261\leq M\leq126
  • NN and MM are integers.
  • sis _ i is a non-empty string of length at most 66 consisting of a and b.
  • sisj (1i<jM)s _ i\neq s _ j\ (1\leq i\lt j\leq M)

Input

The input is given from Standard Input in the following format:

NN MM

s1s _ 1

s2s _ 2

\vdots

sMs _ M

Output

Print the answer modulo 998244353998244353 in a single line.

4 3
aab
bbab
abab
10

There are 1010 strings of length 44 consisting of a and b that do not contain aab, bbab, or abab as consecutive substrings: aaaa, abaa, abba, abbb, baaa, baba, babb, bbaa, bbba, and bbbb. Thus, you should print 1010.

20 1
aa
17711
1000000007 28
bbabba
bbbbaa
aabbab
bbbaba
baaabb
babaab
bbaaba
aabaaa
aaaaaa
aabbaa
bbaaaa
bbaabb
bbabab
aababa
baaaba
ababab
abbaba
aabaab
ababaa
abbbba
baabaa
aabbbb
abbbab
baaaab
baabbb
ababbb
baabba
abaaaa
566756841

Print the answer modulo 998244353998244353.