Cow Poetry#10335

Cow Poetry

1 second
128 megabytes
Hard

Unbeknownst to Farmer John, Bessie is quite a patron of the arts! Recently, she has started studying many of the great poets, and now she wants to try writing some poetry of her own.

Bessie knows words, and she wants to arrange them into poems. She has determined the length of each of her words in syllables and has assigned each word to a "rhyme class". A word rhymes only with words of the same rhyme class.

Each of Bessie's poems consists of lines, and each line must contain exactly syllables. A line is a sequence of words, and each word may be used any number of times. Moreover, Bessie's poems must follow a specific rhyme scheme.

Bessie would like to know how many different poems satisfying these constraints she can write.

Input

The first line contains three integers , and k~(1 \le n \le 5000, 1 \le m \le 10^5, 1 \le k \le 5000).

Each of the next lines contains two integers s_i and c_i~(1 \le s_i \le k, 1 \le c_i \le n), meaning that Bessie knows a word of s_i syllables belonging to rhyme class c_i.

The last lines describe the desired rhyme scheme: each of them contains one uppercase Latin letter e_i. All lines with equal values of e_i must end with words of the same rhyme class. Lines with different values of e_i do not have to end with words of different rhyme classes.

Output

Print the number of poems satisfying these constraints that Bessie can write. Since this number can be very large, output it modulo 10^9 + 7.

Examples

Example 1

Loading
Loading

Note

In the example, Bessie knows three words. The first two words rhyme with each other and have lengths of three and four syllables; the last word is three syllables long and does not rhyme with the others. Bessie wants to write a three-line poem in which each line contains ten syllables and the first and the last lines rhyme. There are such poems. One of them consists of the lines , and , where , and denote the first, the second and the third words.