QOJ.ac

QOJ

Límite de tiempo: 1 s Límite de memoria: 512 MB Puntuación total: 100

#7649. Sequence

Estadísticas

A sequence $(a_1, a_2, \dots, a_n)$ is said to be 120-pattern-avoiding if and only if there do not exist $1 \leq i < j < k \leq n$ such that $a_k < a_i < a_j$.

Given a prime number $P$ and $q$ queries, for each query given by $n$ and $m$, find the number of integer sequences $a$ of length $n$ with values in the range $[0, m]$ that are 120-pattern-avoiding. The result should be taken modulo $P$.

Input

The first line contains two integers $P$ and $q$, representing the modulus and the number of queries.

Each of the next $q$ lines contains two integers $n$ and $m$, representing a query.

Output

Output $q$ lines, where the $i$-th line is the answer to the $i$-th query modulo $P$.

Examples

Input 1

(input data)

Output 1

(output data)

Constraints

For all test cases, $10^8 \leq P \leq 10^9$, $1 \leq q \leq 8 \times 10^4$, $1 \leq n \leq 100$, and $0 \leq m \leq 10^9$.

The test cases are categorized as follows:

  • Test cases 1, 2: $n \leq 16, m \leq 16$
  • Test cases 3, 4: $n \leq 18, m \leq 18$
  • Test cases 5, 6: $n \leq 18, m \leq 10^9$
  • Test cases 7, 8: $n \leq 20, m \leq 20$
  • Test cases 9, 10: $n \leq 20, m \leq 10^9$
  • Test cases 11–16: $n \leq 100, m \leq 100$
  • Test cases 17–20: $n \leq 100, m \leq 10^9$

Test cases 11–20 have gradients for both $n$ and $q$.

Editorials

IDTypeStatusTitlePosted ByLast UpdatedActions
EditorialOpen Official EditorialQingyu- Download
#858EditorialOpen题解 2Qingyu2026-01-28 02:28:24View

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.