Under construction

Still being built. Things may change

Repeating Subsequence Tests

Time limit2sMemory limit512 MB

Summary
Given a string and up to 10^6 generated substring queries, count distinct subsequences of each substring modulo 10^9+7 and output the last answer.
Level

Hard8 of 10

Topics
String, Prefix sum, Dynamic programming, Divide and conquer
Solved
No attempts yet

Problem

A subsequence of a string is the string that remains after removing some of its characters. You may remove no characters, or all of them. For example, the distinct subsequences of "ababA" are "" (the empty string), "a", "b", "A", "ab", "aa", "aA", "ba", "bb", "bA", "aba", "abb", "abA", "aab", "aaA", "bab", "baA", "bbA", "abab", "abaA", "abbA", "aabA", "babA", and "ababA", 24 in total. This problem treats uppercase and lowercase letters as different.

You are given a string SS of length NN. Write a program that finds, for each of QQ contiguous substrings of SS, the number of distinct subsequences of that substring.

To reduce input time, the problem uses the following pseudorandom generator. Given a0a_0, b0b_0, pp, qq, rr, for i≥1i \geq 1 the values aia_i, bib_i, xix_i, yiy_i, and ziz_i are defined as follows. First, let z0=0z_0 = 0 and X=109+7X = 10^9 + 7.

ai=(p⋅ai−1+q⋅bi−1+zi−1+r) mod Xa_i = (p \cdot a_{i-1} + q \cdot b_{i-1} + z_{i-1} + r) \bmod X bi=(p⋅bi−1+q⋅ai−1+zi−1+r) mod Xb_i = (p \cdot b_{i-1} + q \cdot a_{i-1} + z_{i-1} + r) \bmod X xi=min⁡(ai mod n,bi mod n)+1x_i = \min(a_i \bmod n, b_i \bmod n) + 1 yi=max⁡(ai mod n,bi mod n)+1y_i = \max(a_i \bmod n, b_i \bmod n) + 1 zi=(cxicxi+1…cyi−1cyi has this many distinct subsequences) mod Xz_i = (c_{x_i} c_{x_i+1} \ldots c_{y_i-1} c_{y_i}\text{ has this many distinct subsequences}) \bmod X

u mod vu \bmod v is the remainder when uu is divided by vv. The way the generator produces its numbers has no meaning, and for any input there is an algorithm that outputs the answer within the time limit.

Find zQz_Q.

Input

The first line contains the string SS. Its length NN satisfies 11 to 10610^6, and SS consists only of uppercase and lowercase English letters.

The second line contains six integers QQ, a0a_0, b0b_0, pp, qq, rr, separated by single spaces. It is given that 1≤Q≤1061 \leq Q \leq 10^6 and 0≤a0,b0,p,q,r<X=109+70 \leq a_0, b_0, p, q, r < X = 10^9 + 7.

Output

On the first line, print zQz_Q.

Examples1

  1. Example 1

    Input
    ababA
    2 4 6 8 10 12
    
    Expected output
    7