Tag(s):

Algorithms, Dynamic Programming, Dynamic programming, Mathematics, Medium, Two dimensional

Problem
Editorial
Analytics

You are given a string $S$ of length $N$ having distinct lowercase alphabetic characters. Each character in the string $S$ has some follow-up characters. The follow-up character denotes what characters can follow a particular character that belongs to the string $S$. Note that the follow-up characters belong to the string $S$.

Now you have to pick one of the characters of the string $S$ and mark it as a start character. After you pick one character, you need to look in its follow-up list and choose one character from it and continue this to form a new string.

Now, you have to count the total number of non-empty subsequences of all the possible $M$ length strings that can be made out of the characters of the string $S$. Since the number could be very large output it modulo $10^9 + 9$.

Note: Carefully check out the sample explanation for a better understanding of the problem.

Input Format

The first line of the input contains a single integer $N$, the length of the string.
The second line of the input contains a string $S$ of length $N$.
Then $N$ lines follow, $i$-th $(1 \le i \le N)$ line contains a string $T_i$ where each character in the string is a follow up character of the $i$-th character in the main string $S$.
The last line contains a single integer $M$, length of the string that has to be constructed.

Output Format

The only line of the output contains a single integer, the total number of non-empty subsequences of all the possible $M$ length strings modulo $10^9 + 9$.

Constraints

$1 \le N \le 10$
$1 \le |T_i| \le N$
$1 \le M \le 5$ x $10^5$

SAMPLE INPUT
4
abcd
b
ac
abd
3
SAMPLE OUTPUT
98
Explanation

$a : [b]$
$b : [a, c]$
$c : [a, b, d]$
$d : [a, d]$

Few strings of length $3$ and thier non-empty subsequences:
$aba : [a, b, a, ab, aa, ba, aba]$
$abc : [a, b, c, ab, ac, bc, abc]$
$dda : [d, d, a, dd, da, da, dda]$
and so on...

Note that, there are $98$ such non-empty subsequences.

Time Limit: 1.0 sec(s) for each input file.
Memory Limit: 256 MB
Source Limit: 1024 KB
Marking Scheme: Marks are awarded when all the testcases pass.
Allowed Languages: Bash, C, C++, C++14, Clojure, C#, D, Erlang, F#, Go, Groovy, Haskell, Java, Java 8, JavaScript(Rhino), JavaScript(Node.js), Julia, Kotlin, Lisp, Lisp (SBCL), Lua, Objective-C, OCaml, Octave, Pascal, Perl, PHP, Python, Python 3, R(RScript), Racket, Ruby, Rust, Scala, Swift, Swift-4.1, TypeScript, Visual Basic

CODE EDITOR

Initializing Code Editor...

Contributor 