Under construction

Still being built. Things may change

Mate

Time limit2sMemory limit128 MB

Summary
For each query, count subsequences of S of length D whose last two characters are the given pair XY, modulo 1e9+7.
Level

Medium7 of 10

Topics
Combinatorics, Dynamic programming, Prefix sum, Math
Solved
No attempts yet

Problem

Mate got a string of lowercase English letters from his parents. To get at least some use out of the present, he decided to find rhymes in it for the song he is writing next.

A rhyme is a word of length DD whose next to last letter is XX and whose last letter is YY. Mate builds a word by crossing out some letters of the given string and joining the letters he did not cross out in their original order. For each rhyme, count how many ways there are to cross out letters so that the remaining word meets the condition.

Two ways count as different when the sets of crossed-out positions differ.

Input

The first line contains a string SS of lowercase English letters. (2≤∣S∣≤20002 \le |S| \le 2000)

The second line contains the integer QQ, the number of rhymes Mate needs. (1≤Q≤500 0001 \le Q \le 500\,000)

Each of the next QQ lines contains an integer DD and a string XYXY of two lowercase English letters, separated by a space. (2≤D≤∣S∣2 \le D \le |S|)

Output

Print QQ lines. The iith line contains the number of ways for the iith rhyme. The number can get very large, so print it modulo 1 000 000 0071\,000\,000\,007.

Note

Take SS = banana with D=2D = 2 and XYXY = na. Number the letters from 1. Exactly three sets of kept positions work: (3,4)(3, 4), (3,6)(3, 6) and (5,6)(5, 6). In each of them the kept letters spell na, and every other letter is crossed out.

Examples2

  1. Example 1

    Input
    banana
    3
    2 na
    3 ba
    4 nn
    
    Expected output
    3
    0
    1
    
  2. Example 2

    Input
    malimateodmameitate
    3
    10 ot
    7 aa
    3 me
    
    Expected output
    2
    464
    56