Under construction

Still being built. Things may change

Queue

Time limit1sMemory limit2048 MB

Summary
Worms with lengths 1 to 6 are merged and split in queues, and the task asks for the product of f(t) over all length-k substrings of s.
Level

Hard9 of 10

Topics
Linked list, String matching, Implementation
Solved
No attempts yet

Problem

There are nn worms numbered from 11 to nn. The length of each worm is a positive integer between 11 and 66. You want to arrange the worms into several queues. Initially, each worm is alone in its own queue. Each worm is both the front and the end of its queue.

There are mm operations. Each operation is one of the following three types:

  • 1 i j (1≤i,j≤n1 \leq i,j \leq n): Merge the queue that contains worm ii with the queue that contains worm jj. In the new queue, worm jj immediately follows worm ii. Every other worm keeps its neighbors. It is guaranteed that worm ii is at the tail of its queue, worm jj is at the front of its queue, and ii and jj are not in the same queue.
  • 2 i (1≤i≤n1 \leq i \leq n): Split the queue between worm ii and the worm right after it. It is guaranteed that worm ii is not at the tail of its queue.
  • 3 s k (kk is a positive integer and ss is a numeric string with length at least kk): Find the product of f(t)f(t) modulo 998,244,353, taken over all substrings tt of length kk in ss. There are ∣s∣−k+1|s|-k+1 such substrings for given ss and kk.

The value f(t)f(t) is defined as follows.

In a given queue, the kk-string after worm ii is formed by starting at worm ii, walking toward the tail, and taking the kk worms closest to ii, counting ii itself. Their lengths are concatenated in order. If fewer than kk worms remain before the tail, worm ii has no kk-string. For example, suppose a queue holds worms 10, 22, 3 with lengths 4, 5, 6. The 3-string after worm 10 is 456. Worm 22 has no 3-string, but its 2-string is 56 and its 1-string is 5.

f(t)f(t) is the number of worms whose kk-string is exactly tt.

Input

The first line contains two positive integers nn and mm, the number of worms and the number of operations. The second line contains nn positive integers, each at most 6, giving the lengths of worms 11 through nn. Each of the next mm lines describes one operation. The input file may be large.

Output

For each operation of type 3 s k, print the answer on its own line.

Constraints

n≤2×105n \leq 2 \times 10^5, m≤5×105m \leq 5 \times 10^5, k≤50k \leq 50.

Let ∑∣s∣\sum |s| be the sum of the lengths of ss over all queries. Then ∑∣s∣≤107\sum |s| \leq 10^7.

Let cc be the number of operations of the form 2 i. Then c≤103c \leq 10^3.

Examples2

  1. Example 1

    Input
    5 9
    3 1 3 5 3
    3 333135 2
    3 333135 1
    1 1 3
    1 2 5
    1 3 2
    1 5 4
    3 333135 2
    3 333135 1
    3 333135 3
    
    Expected output
    0
    81
    1
    81
    0
    
  2. Example 2

    Input
    2 10
    6 6
    3 666666 1
    1 1 2
    3 666666 2
    3 666666 4
    3 666666666666666666666666666666 1
    2 1
    1 2 1
    3 666666 2
    3 666666 4
    3 666666666666666666666666666666 1
    
    Expected output
    64
    1
    0
    75497471
    1
    0
    75497471