William and Edoardo are using a new instant messaging app to communicate. However, the application only supports messages composed exclusively of lowercase letters of the English alphabet. They are avid emoji users, so this is clearly unacceptable!
For this reason, they devised a way to convert any message to a string that can be sent using the app: any symbol in the original message is mapped to a sequence of one or more lowercase letters and this encoding is known to both of them. The encoding is defined for symbols, mapping the -th of which to a sequence , composed only of lowercase letters.
Edoardo has received a message , composed of lowercase letters, and now needs to translate it back to its original form. However, he realized that, in some cases, a message can be interpreted in multiple ways. This is a huge problem! For instance, consider the following encoding, where :
; ; ; ; ; ; ;
If the message is Yes!
, William will actually send to Edoardo aabbcc
. However, also the message No?
is translated into aabbcc
. What a mess! Help Edoardo count the number of different ways in which the message can be interpreted.
Input data
The first line contains two integers and , the number of lowercase letters in the received message and the number of different symbols in their encoding. The second line contains a string composed of lowercase letters of the English alphabet, the message received by Edoardo. Further lines follow, the -th of which containing a string composed of lowercase letters, the mapping of the -th symbol in the encoding.
Output data
You need to write a single line with an integer: the number of different ways in which the received message can be interpreted. Since this number can be very big, calculate it modulo .
Constraints and clarifications
- .
- .
- , for each .
- for each .
# | Score | Constraints |
---|---|---|
1 | 0 | Examples |
2 | 15 | , . |
3 | 12 | or for each . |
4 | 18 | , . |
5 | 24 | and are composed only of the letter a , for each . |
6 | 31 | No additional limitations. |
Example 1
stdin
4 4
aabb
a
b
bb
ab
stdout
3
Explanation
In the first sample case, the message aabb
can be interpreted in three ways: ; ; ;
Example 2
stdin
4 4
aabb
a
ab
aa
aba
stdout
0
Explanation
In the second sample case, the message cannot be interpreted in any way using the given mapping!