cho.sh
Notes
Loading...

DNA Deletions and Protein Count

Time limit

2s

Memory limit

128 MB

Problem

A DNA string consists of the nucleotides A, C, G, and T. Some DNA strings can be converted into a protein, which is a nonempty sequence of amino acids. The conversion works as follows.

  1. Split the string from left to right into non-overlapping groups of 3 characters. Each group is called a codon. If 1 or 2 nucleotides remain at the end, ignore them. For example, the DNA string ACCTGTACG produces the codons ACC, TGT, and ACG in that order, while ACCTGTAC produces only ACC and TGT, ignoring the final AC.
  2. Using the given codon conversion table, replace each codon from left to right by its corresponding amino acid. If any codon is not in the table, the string cannot be converted into a protein. For example, if ACC maps to thr, TGT maps to cys, and ACG maps to thr, then ACCTGTACG is converted into thr cys thr.

A deletion removes one or more nucleotides from a DNA string. The remaining nucleotides keep their relative order. For example, after a deletion, ACTG could become ACG or CG, among other strings.

Given a DNA string and a codon conversion table, compute how many distinct proteins can be obtained after applying zero or more deletions.

Input

The first line contains the DNA string. Its length is at most 2,500.

The second line contains M, the number of codons in the codon conversion table. M is at most 50.

Each of the next M lines contains one table entry: a codon followed by an amino acid. A codon is a length-3 string made only of A, C, G, and T. An amino acid is a nonempty string of length at most 20 consisting of uppercase or lowercase English letters. No codon appears more than once in the table.

Output

Print one line containing the number of distinct obtainable proteins modulo 1,000,000,007.