## GUPTA MECHANICAL

IN THIS WEBSITE I CAN TELL ALL ABOUT TECH. TIPS AND TRICKS APP REVIEWS AND UNBOXINGS ALSO TECH. NEWS .............

# [Solution] Game of AI (hard version) Codeforces Solution

H2. Game of AI (hard version)
time limit per test
12 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output

This is the hard version of this problem. The difference between easy and hard versions is the constraint on $k$ and the time limit. Notice that you need to calculate the answer for all positive integers $n\in \left[1,k\right]$ in this version. You can make hacks only if both versions of the problem are solved.

Cirno is playing a war simulator game with $n$ towers (numbered from $1$ to $n$) and $n$ bots (numbered from $1$ to $n$). The $i$-th tower is initially occupied by the $i$-th bot for $1\le i\le n$.

Before the game, Cirno first chooses a permutation $p=\left[{p}_{1},{p}_{2},\dots ,{p}_{n}\right]$ of length $n$ (A permutation of length $n$ is an array of length $n$ where each integer between $1$ and $n$ appears exactly once). After that, she can choose a sequence $a=\left[{a}_{1},{a}_{2},\dots ,{a}_{n}\right]$ ($1\le {a}_{i}\le n$ and ${a}_{i}\ne i$ for all $1\le i\le n$).

The game has $n$ rounds of attacks. In the $i$-th round, if the ${p}_{i}$-th bot is still in the game, it will begin its attack, and as the result the ${a}_{{p}_{i}}$-th tower becomes occupied by the ${p}_{i}$-th bot; the bot that previously

Solution Click Below:-  👉

👇👇👇👇👇

occupied the ${a}_{{p}_{i}}$-th tower will no longer occupy it. If the ${p}_{i}$-th bot is not in the game, nothing will happen in this round.

After each round, if a bot doesn't occupy any towers, it will be eliminated and leave the game. Please note that no tower can be occupied by more than one bot, but one bot can occupy more than one tower during the game.

At the end of the game, Cirno will record the result as a sequence $b=\left[{b}_{1},{b}_{2},\dots ,{b}_{n}\right]$, where ${b}_{i}$ is the number of the bot that occupies the $i$-th tower at the end of the game.

However, as a mathematics master, she wants you to solve the following counting problem instead of playing games:

Count the number of different pairs of sequences $a$$b$ from all possible choices of sequence $a$ and permutation $p$.

Calculate the answers for all $n$ such that $1\le n\le k$. Since these numbers may be large, output them modulo $M$.

Input

The only line contains two positive integers $k$ and $M$ ($1\le k\le {10}^{5}$$2\le M\le {10}^{9}$ ). It is guaranteed that ${2}^{18}$ is a divisor of $M-1$ and $M$ is a prime number.

Output

Output $k$ lines, where the $i$-th line contains a non-negative integer, which is the answer for $n=i$ modulo $M$.