PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#SRD0000150. Truy vấn hoán vị đoạn (Range Shuffle Query)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

2 lộ trình

Truy vấn hoán vị đoạn (Range Shuffle Query)

Range Shuffle Query

Source: AtCoder

Version: Phuoc Hung OJ Extended

Problem Statement

You are given an array A=(A1,…,An)A=(A_1,\ldots,A_n). Each query provides L,R,XL,R,X and considers a sequence BB initially equal to (AL,…,AR)(A_L,\ldots,A_R).

Perform the following procedure exactly once:

  1. Remove every element of BB whose value is at least XX.
  2. Rearrange the remaining elements in any order.

Count the number of distinct resulting value sequences. Different permutations of equal-valued elements count only once. If no element remains, the empty sequence counts as one possibility. Output the count modulo 998244353998244353.

The original array is unchanged, and all queries are independent.

Input

  • First line: n,qn,q.
  • Second line: the nn array elements A1,…,AnA_1,\ldots,A_n.
  • Each of the next qq lines: L,R,XL,R,X.

Output

Print qq lines. Line tt gives the number of distinct sequences for query tt, modulo 998244353998244353.

Subtasks

  • Subtask 1 (20 points): 1≤n≤1001\le n\le 100; 1≤q≤1001\le q\le 100; 1≤Ai≤n1\le A_i\le n; 1≤L≤R≤n1\le L\le R\le n; 1≤X≤n1\le X\le n; modulo 998244353998244353.
  • Subtask 2 (30 points): 1≤n≤20001\le n\le 2000; 1≤q≤20001\le q\le 2000; 1≤Ai≤n1\le A_i\le n; 1≤L≤R≤n1\le L\le R\le n; 1≤X≤n1\le X\le n; modulo 998244353998244353.
  • Subtask 3 (50 points): 1≤n≤2500001\le n\le 250000; 1≤q≤2500001\le q\le 250000; 1≤Ai≤n1\le A_i\le n; 1≤L≤R≤n1\le L\le R\le n; 1≤X≤n1\le X\le n; modulo 998244353998244353.

Examples

Example 1

Input

5 3
1 2 3 3 1
1 5 3
3 4 1
1 3 4

Output

3
1
6

Explanation

Query (1,5,3)(1,5,3) keeps 1,2,11,2,1 and produces (1,1,2)(1,1,2), (1,2,1)(1,2,1), (2,1,1)(2,1,1): three distinct sequences. Query (3,4,1)(3,4,1) removes both elements, leaving one empty sequence. Query (1,3,4)(1,3,4) keeps 1,2,31,2,3, all distinct, so 3!=63!=6 sequences.