Under construction

Still being built. Things may change

Prefix-free Queries

Time limit2sMemory limit256 MB

Summary
Given a string and queries of substring lists, count the prefix-free subsets of the substrings modulo m, for each query.
Level

Hard9 of 10

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

Problem

C(s1,s2,…,sk)C (s_1, s_2, \ldots, s_k) is the number of ways to build a prefix-free set from the multiset {s1,s2,…,sk}\{s_1, s_2, \ldots, s_k\}. A prefix-free set is a set of distinct strings in which no string is a prefix of another. The empty set is a valid prefix-free set. For example, if for every i≠ji \neq j the string sis_i is not a prefix of sjs_j, then C(s1,…,sk)=2kC (s_1, \ldots, s_k) = 2^{k}.

We count the ways to build such sets, not the sets themselves. That is, we count the subsets of indices from {1,2,…,k}\{1, 2, \ldots, k\} whose strings form a prefix-free set. For example, CC("aa", "aa", "a", "a") =5= 5. The five ways are the empty set, and the sets containing only the first, second, third, or fourth string.

You are given a string ss of nn lowercase English letters and qq queries. Let s[l,r]s[l, r] be the substring slsl+1…srs_{l} s_{l + 1} \ldots s_{r}. For each query "kk mm l1l_1 r1r_1 l2l_2 r2r_2 …\ldots lkl_k rkr_k", print C(s[l1,r1],s[l2,r2],…,s[lk,rk])C (s[l_1, r_1], s[l_2, r_2], \ldots, s[l_k, r_k]) modulo mm.

Input

The first line contains two integers nn and qq (1≤n≤4⋅1051 \le n \le 4 \cdot 10^{5}, 1≤q≤4⋅1051 \le q \le 4 \cdot 10^{5}). The second line contains the string ss of length nn. Each of the next qq lines contains one query in the form "kk mm l1l_1 r1r_1 …\ldots lkl_k rkr_k", where 1≤k≤4⋅1051 \le k \le 4 \cdot 10^{5}, 2≤m≤1092 \le m \le 10^{9}, and 1≤lj≤rj≤n1 \le l_j \le r_j \le n. The sum of all kk over all queries does not exceed 4⋅1054 \cdot 10^{5}.

Output

For each query, print one line with the value of C(s[l1,r1],…,s[lk,rk])C (s[l_1, r_1], \ldots, s[l_k, r_k]) modulo mm.

Examples1

  1. Example 1

    Input
    10 6
    aabbaacaba
    4 30 1 2 5 6 10 10 10 10
    5 20 1 2 3 4 5 6 7 8 9 10
    1 2 1 10
    3 20 9 9 7 7 8 8
    5 20 6 6 7 7 8 8 9 9 10 10
    4 20 1 1 2 2 3 3 4 4
    
    Expected output
    5
    4
    0
    8
    16
    9