#P11107. [ROI 2023] Ultra mex (Day 1)
[ROI 2023] Ultra mex (Day 1)
Background
Translated from ROI 2023 D1T4.
Suppose is a set of non-negative integers. Let the smallest non-negative integer that does not belong to be denoted by . For example, .
We also define an operation on a set that contains , called . Let . Since , clearly . We construct a new set as follows: XOR every element of with . For example, $\operatorname{Ultra}(\{0, 1, 2, 4, 5, 9\}) = \{0\oplus2, 1\oplus2, 2\oplus2, 4\oplus2, 5\oplus2, 9\oplus2\} = \{2, 3, 0, 6, 7, 11\} = \{0, 2, 3, 6, 7, 11\}$.
It is not hard to see that if the set contains , then the set also contains .
We choose a set consisting of distinct integers between and , and is in . Consider the following sequence:
- $m_0 = \operatorname{mex}(A_0), A_1 = \operatorname{Ultra}(A_0)$.
- $m_1 = \operatorname{mex}(A_1), A_2 = \operatorname{Ultra}(A_1)$.
- $m_i = \operatorname{mex}(A_i), A_{i+1} = \operatorname{Ultra}(A_i)$.
If starting from some number , the values no longer change, that is, for all , , then the set is called mex-stable, and is called the mex-limit of the set .
Problem Description
Given integers , compute the number of sets that satisfy the following conditions:
- It contains distinct integers from to (and must be included in ).
- It is mex-stable.
- Its mex-limit equals .
Since the answer may be large, output the result modulo . It is guaranteed that is divisible by ().
Input Format
The first line contains an integer , the modulus to take (where and is divisible by , so in fact cannot be less than ). It is guaranteed that is a prime.
The second line contains an integer , the number of test cases ().
For each test case, there are three integers (, ).
Output Format
For each test case, output one line containing one integer, the number of sets that satisfy the conditions modulo .
998244353
6
3 2 1
3 2 2
3 2 3
3 2 4
3 5 1
4 6 1
6
1
0
0
29
2461
Hint
In addition to the samples, this problem has thirty subtasks, as shown in the figure below.

Translated by ChatGPT 5