#P11107. [ROI 2023] Ultra mex (Day 1)

[ROI 2023] Ultra mex (Day 1)

Background

Translated from ROI 2023 D1T4.

Suppose AA is a set of non-negative integers. Let the smallest non-negative integer that does not belong to AA be denoted by mex(A)\operatorname{mex}(A). For example, mex({0,1,2,4,5,9})=3\operatorname{mex}(\{0, 1, 2, 4, 5, 9\}) = 3.

We also define an operation on a set AA that contains 00, called Ultra\operatorname{Ultra}. Let m=mex(A)m = \operatorname{mex}(A). Since 0A0 \in A, clearly m>0m > 0. We construct a new set Ultra(A)\operatorname{Ultra}(A) as follows: XOR every element of AA with m1m - 1. 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 AA contains 00, then the set Ultra(A)\operatorname{Ultra}(A) also contains 00.

We choose a set A0A_0 consisting of nn distinct integers between 00 and 2k12^{k} - 1, and 00 is in A0A_0. 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)$.
  • \dots
  • $m_i = \operatorname{mex}(A_i), A_{i+1} = \operatorname{Ultra}(A_i)$.

If starting from some number ll, the values mim_i no longer change, that is, for all ili \ge l, mi=mlm_i = m_l, then the set A0A_0 is called mex-stable, and mlm_l is called the mex-limit of the set A0A_0.

Problem Description

Given integers k,n,pk, n, p, compute the number of sets A0A_0 that satisfy the following conditions:

  • It contains nn distinct integers from 00 to 2k12^{k} - 1 (and 00 must be included in A0A_0).
  • It is mex-stable.
  • Its mex-limit equals pp.

Since the answer may be large, output the result modulo MM. It is guaranteed that M1M - 1 is divisible by 2182^{18} (262144262144).

Input Format

The first line contains an integer MM, the modulus to take (where 3M1093 \le M \le 10^9 and M1M - 1 is divisible by 2182^{18}, so in fact MM cannot be less than 218+12^{18} + 1). It is guaranteed that MM is a prime.

The second line contains an integer tt, the number of test cases (1t1000001 \le t \le 100000).

For each test case, there are three integers k,n,pk, n, p (1k171 \le k \le 17, 1n,p2k1 \le n, p \le 2^k).

Output Format

For each test case, output one line containing one integer, the number of sets AA that satisfy the conditions modulo MM.

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