The Films
Contest: Codeforces Round 502 (in memory of Leopoldo Taravilse, Div. 1 + Div. 2)
Difficulty rating: 3300
Tags: brute force

In "The Man in the High Castle" world, there are $$$m$$$ different film endings.

Abendsen owns a storage and a shelf. At first, he has $$$n$$$ ordered films on the shelf. In the $$$i$$$-th month he will do:

1. Empty the storage.
2. Put $$$k_i \cdot m$$$ films into the storage, $$$k_i$$$ films for each ending.
3. He will think about a question: if he puts all the films from the shelf into the storage, then randomly picks $$$n$$$ films (from all the films in the storage) and rearranges them on the shelf, what is the probability that sequence of endings in $$$[l_i, r_i]$$$ on the shelf will not be changed? Notice, he just thinks about this question, so the shelf will not actually be changed.

Answer all Abendsen's questions.

Let the probability be fraction $$$P_i$$$. Let's say that the total number of ways to take $$$n$$$ films from the storage for $$$i$$$-th month is $$$A_i$$$, so $$$P_i \cdot A_i$$$ is always an integer. Print for each month $$$P_i \cdot A_i \pmod {998244353}$$$.

$$$998244353$$$ is a prime number and it is equal to $$$119 \cdot 2^{23} + 1$$$.

It is guaranteed that there will be only no more than $$$100$$$ different $$$k$$$ values.

Input format:
The first line contains three integers $$$n$$$, $$$m$$$, and $$$q$$$ ($$$1 \le n, m, q \le 10^5$$$, $$$n+q\leq 10^5$$$) — the number of films on the shelf initially, the number of endings, and the number of months.

The second line contains $$$n$$$ integers $$$e_1, e_2, \ldots, e_n$$$ ($$$1\leq e_i\leq m$$$) — the ending of the $$$i$$$-th film on the shelf.

Each of the next $$$q$$$ lines contains three integers $$$l_i$$$, $$$r_i$$$, and $$$k_i$$$ ($$$1 \le l_i \le r_i \le n, 0 \le k_i \le 10^5$$$) — the $$$i$$$-th query.

It is guaranteed that there will be only no more than $$$100$$$ different $$$k$$$ values.

Output format:
Print the answer for each question in a separate line.

Constraints:
Time limit: 5.0 s
Memory limit: 512.0 MB

Note:
In the first sample in the second query, after adding $$$2 \cdot m$$$ films into the storage, the storage will look like this: $$$\{1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 4, 4\}$$$.

There are $$$26730$$$ total ways of choosing the films so that $$$e_l, e_{l+1}, \ldots, e_r$$$ will not be changed, for example, $$$[1, 2, 3, 2, 2]$$$ and $$$[1, 2, 3, 4, 3]$$$ are such ways.

There are $$$2162160$$$ total ways of choosing the films, so you're asked to print $$$(\frac{26730}{2162160} \cdot 2162160) \mod 998244353 = 26730$$$.

Example input:
6 4 4
1 2 3 4 4 4
1 4 0
1 3 2
1 4 2
1 5 2

Example output:
6
26730
12150
4860

Example input:
5 5 3
1 2 3 4 5
1 2 100000
1 4 4
3 5 5

Example output:
494942218
13125
151632

Editorial:
First, we claim the probability is this: $$$$$$ \frac{(mk+n-(r-l+1))^{\underline{n-(r-l+1)}}}{(mk+n)^{\underline n}} \cdot \prod_{i=1}^m (t_i +k)^{\underline c_i} $$$$$$ Where $$$a^{\underline b} = C_a^bb! = \frac{a!}{(a-b)!}$$$, $$$t_i$$$ is the number of times ending $$$i$$$ occurs in total and $$$c_i$$$ is the number of times ending $$$i$$$ occurs in segment $$$[l,r]$$$.
Proof:
For each position in $$$[l,r]$$$ with ending $$$i$$$, you can choose any film with the same ending, there are $$$(t_i+k)$$$ of them for the first candidate, and $$$(t_i+k-1)$$$ of them for the second candidate, ...
So you can satisfy the conditions with $$$\prod_{i=1}^m (t_i +k)^{\underline c_i}$$$ number of ways.
After that, other $$$n-(r-l+1)$$$ positions can be filled arbitrarily, there are $$$(mk+n-(r-l+1))^{\underline{n-(r-l+1)}}$$$ ways of doing that.
And the total number of ways is $$$(mk+n)^{\underline n}$$$ obviously.
If $$$k = 0$$$, we can maintain the expression using Mo-algorithm with complexity about $$$O(n\sqrt n)$$$. More precisely, it is $$$O(n \sqrt q)$$$ or $$$O(n \sqrt q\log m)$$$.
Key observation: There can only be $$$O(\sqrt n)$$$ different occuring times at most.
Proof:
In the worst case, the occurring times are $$$1 + 2 + 3 + ... + x = \frac{x(x+1)}{2} \le n$$$, so $$$x = O(\sqrt n)$$$.
Similarly, that there are at most $$$O(n^\frac{2}{3})$$$ different combinations of $$$t_i$$$ and $$$c_i$$$.
Proof:
In the worst case, pairs of $$$(t_i,c_i)$$$ are like:
$$$t_i$$$ : 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 ...
$$$c_i$$$ : 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 ...
Let its length of same $$$c_i$$$ be $$$A$$$ and repeat time be $$$B$$$. Then from the total occurring times we get $$$\frac{A(A+1)}{2}B \le n$$$ , and from the occurring times in segment we get $$$\frac{B(B+1)}{2} A \le r-l+1$$$ . So around $$$A = B = O(n^{\frac{1}{3}})$$$ we achieve the minimum.
Use a hash table (actually an array of size $$$O(m\sqrt n)$$$) to remember them, and brute-force them for each query. We're done.
About the $$$\frac{(mk+n-(r-l+1))^{\underline{n-(r-l+1)}}}{(mk+n)^{\underline n}}$$$ part, since there are no more than $$$K = 100$$$ different $$$k$$$ value, preprocess that for each $$$k$$$.
Complexity: $$$O(Kn + n \sqrt q + qn^{\frac{2}{3}} \log m)$$$.
Actually, due to limit of $$$K$$$, if one simply run mo-algorithm $$$K$$$ times, it will reach a $$$O(n\sqrt{qK})$$$. Under this limit it can be faster than standard solution.
**Bonus: can we have a solution that does not depend on $$$K$$$ (if only $$$\prod_{i=1}^m (t_i +k)^{\underline c_i}$$$ part is calculated) and faster than $$$O(n \sqrt q + qn^{\frac{2}{3}} \log m)$$$ ? **