String Paradigm

3

1 votes
Problem

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 109+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 (1iN) line contains a string Ti 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 109+9.

Constraints

1N10
1|Ti|N
1M5 x 105

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

Follow ups:
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.

Editor Image

?