Harryxun/sft_time huggingface dataset:
### BUGGY CODE ###

K, N = list(map(int, input().split()))

dprint = lambda *x:x

def cmb(n, r, mod):
    if ( r<0 or r>n ):
        return 0
    r = min(r, n-r)
    return g1[n] * g2[r] * g2[n-r] % mod

mod = 998244353 #出力の制限
__N = 4000
g1 = [1, 1] # 元テーブル
g2 = [1, 1] #逆元テーブル
inverse = [0, 1] #逆元テーブル計算用テーブル

for i in range( 2, __N + 1 ):
    g1.append( ( g1[-1] * i ) % mod )
    inverse.append( ( -inverse[mod % i] * (mod//i) ) % mod )
    g2.append( (g2[-1] * inverse[-1]) % mod )



def kumiawase(p, q):
    # dprint(p,q,cmb(p+q-1,q-1 ,mod))
    return cmb(p+q -1,q-1 ,mod)


def solve(n):
    assert n <= K+1
    # 組み合わせの組
    # abs(n-1)//2 + 偶数分 n//2?
    # 2 ->  1,1
    # 3 -> 1,2
    # 4 -> 1,3 2,2
    # 5 -> 1,4 2,3
    # 6 -> 1,5 2,4 3,3
    # 7  3通り

    # 組のうち片方だけ使用する数 p
    ans = 0
    kumi = (n - 1) // 2
    for p in range(kumi+1):
        # dprint("p",p,"n",n)
        if p>N:
            break
        # 使えるのは p個 + 組に関係ないやつ
        ans += 2**p *cmb(kumi,p,mod)* kumiawase(N - p, p+ (K-kumi*2- int(n%2==0) ) )
        # dprint("anstmp1",ans)
        if n % 2 == 0 and N - p -1 >= 0:
            ans += 2**p *cmb(kumi,p,mod) * kumiawase(N - p -1 , p+ (K-kumi*2- int(n%2==0) ) ) # K==n//2を使用
    # dprint("-------",ans)
    return ans


ans = []
for i in range(2, 2 * K +1):
    if i <= K+1:
        tmp = solve(i)%998244353
        ans.append(tmp)
        print(tmp)
    else:
        print(ans[-i + K])


### FIXED CODE ###

K, N = list(map(int, input().split()))

dprint = lambda *x:x

def cmb(n, r, mod):
    if ( r<0 or r>n ):
        return 0
    r = min(r, n-r)
    return g1[n] * g2[r] * g2[n-r] % mod

mod = 998244353 #出力の制限
__N = 8000
g1 = [1, 1] # 元テーブル
g2 = [1, 1] #逆元テーブル
inverse = [0, 1] #逆元テーブル計算用テーブル

for i in range( 2, __N + 1 ):
    g1.append( ( g1[-1] * i ) % mod )
    inverse.append( ( -inverse[mod % i] * (mod//i) ) % mod )
    g2.append( (g2[-1] * inverse[-1]) % mod )



def kumiawase(p, q):
    # dprint(p,q,cmb(p+q-1,q-1 ,mod))
    return cmb(p+q -1,q-1 ,mod)


def solve(n):
    assert n <= K+1
    # 組み合わせの組
    # abs(n-1)//2 + 偶数分 n//2?
    # 2 ->  1,1
    # 3 -> 1,2
    # 4 -> 1,3 2,2
    # 5 -> 1,4 2,3
    # 6 -> 1,5 2,4 3,3
    # 7  3通り

    # 組のうち片方だけ使用する数 p
    ans = 0
    kumi = (n - 1) // 2
    for p in range(kumi+1):
        # dprint("p",p,"n",n)
        if p>N:
            break
        # 使えるのは p個 + 組に関係ないやつ
        ans += 2**p *cmb(kumi,p,mod)* kumiawase(N - p, p+ (K-kumi*2- int(n%2==0) ) )
        # dprint("anstmp1",ans)
        if n % 2 == 0 and N - p -1 >= 0:
            ans += 2**p *cmb(kumi,p,mod) * kumiawase(N - p -1 , p+ (K-kumi*2- int(n%2==0) ) ) # K==n//2を使用
    # dprint("-------",ans)
    return ans


ans = []
for i in range(2, 2 * K +1):
    if i <= K+1:
        tmp = solve(i)%998244353
        ans.append(tmp)
        print(tmp)
    else:
        print(ans[-i + K])
### BUGGY CODE ###

import sys
import math
sys.setrecursionlimit(1000000)
mod=1000000007
def power_func(a,b,p):
    """a^b mod p を求める"""
    if b==0: 
        return 1
    if b%2==0:
        d=power_func(a,b//2,p)
        return d*d %p
    if b%2==1:
        return (a*power_func(a,b-1,p ))%p
class Node:
    def __init__(self):
        self.next=[]
    def get_data(self,new_next,weight=1):
        self.next.append(new_next)
def BFS(i,depth,K):  
    depth+=1
    tmp=i
    seki=1
    num=len(tmp)
    if depth==1:
        calc=K-1

        
    else:
        calc=K-2
        num-=1
    #"""
    loop=range(calc,calc-num,-1)
    for i in loop:
        seki*=i%1000000007
    #"""
    """
    if calc-num>=0:
        fac=math.factorial(calc-num)%mod
        seki*=((math.factorial(calc)%mod)*power_func(fac,mod-2,mod))%mod
        seki=(math.factorial(calc)//math.factorial(calc-num))%mod
    else:
        return 0
    """
    if seki==0:
        print(0)
        exit()
            
    for each in tmp:
        if not check_node[each]:
            check_node[each]=True
            seki*=BFS(node[each].next,depth,K)%mod
    return seki


N,K=list(map(int,input().split()))
node=[Node() for _ in range(N)]
check_node=[False for _ in range(N)]
check_node[0]=True
for i in range(N-1):
    p,n=list(map(int,input().split()))
    node[n-1].get_data(p-1)
    node[p-1].get_data(n-1)
start=0
d=0

com=K*BFS(node[0].next,d,K)
print(int(com)%mod)

### FIXED CODE ###

import sys
import math
sys.setrecursionlimit(1000000)
mod=1000000007
class Node:
    def __init__(self):
        self.next=[]
    def get_data(self,new_next,weight=1):
        self.next.append(new_next)
def BFS(i,depth,K):  
    depth+=1
    tmp=i
    seki=1
    num=len(tmp)
    if depth==1:
        calc=K-1
        loop=range(calc,calc-num,-1)
        for i in loop:
            seki*=i%1000000007

        
    else:
        calc=K-2
        num-=1
        if calc>=num:
            seki=cache[num]
        else:
            print(0)
            exit()

            
            
    for each in tmp:
        if not check_node[each]:
            check_node[each]=True
            seki*=BFS(node[each].next,depth,K)%mod
    return seki


N,K=list(map(int,input().split()))

if K>2:
    cache=[0 for i in range(K-2)]
    cache[0]=1
    for i in range(1,K-2):
        cache[i]=cache[i-1]*(K-1-i)%mod
    cache.append(cache[-1])
else:
    cache=[1,1]
node=[Node() for _ in range(N)]
check_node=[False for _ in range(N)]
check_node[0]=True
for i in range(N-1):
    p,n=list(map(int,input().split()))
    node[n-1].get_data(p-1)
    node[p-1].get_data(n-1)
start=0
d=0

com=K*BFS(node[0].next,d,K)
print(int(com)%mod)
### BUGGY CODE ###

n,k=map(int,input().split())
a=list(map(int,input().split()))
f=list(map(int,input().split()))
a.sort()
if sum(a)<=k:
    print(0)
    exit()
f.sort(reverse=True)
l=0
r=10**12
x=(r+l)//2
def check(x):
    p=0
    for i in range(n):
        if x//f[i]<a[i]:
            p+=a[i]-x//f[i]
            if p>k:
                return False
    if p>k:
        return False
    else:
        return True
while l<r:
    if check(x):
        r=x
        x=(r+l)//2
    else:
        l=x
        x=(r+l)//2
print(r)


### FIXED CODE ###

import sys
input = sys.stdin.readline
from operator import itemgetter
sys.setrecursionlimit(10000000)

a = []
f = []
n = 0

def rec(l, r, k):
    if l+1 < r:
        mid = (l + r) // 2
        s = 0
        for i in range(n):
            s += max(a[i] - mid // f[i], 0)
        if s <= k:
            return rec(l, mid, k)
        else:
            return rec(mid, r, k)
    else:
        return r

def main():
    global a, f, n
    n, k = map(int, input().strip().split())
    a = list(map(int, input().strip().split()))
    f = list(map(int, input().strip().split()))
    a = sorted(a)
    f = sorted(f, reverse=True)
    print(rec(-1, 10**12, k))

if __name__ == '__main__':
    main()
### BUGGY CODE ###

n = int(input())
h = list(map(int,input().split()))

m = 0

for i in range(n-1):
  count =0
  for j in range(i,n-1):
    if h[j]>=h[j+1]:
      count+=1
    else:
      break
      
  if m<count and count>0:
    m = count
    
print(m)

### FIXED CODE ###

n = int(input())
h = list(map(int,input().split()))

m = 0
a =0
loop = True

while loop:
  count =0
  for j in range(a,n-1):
    if h[j]>=h[j+1]:
      count+=1
    else:
      a = j+1
      break      
  if m<count and count>0:
    m = count
    
  if a > n-2 or j >= n-2:
        loop = False
      
print(m)
### BUGGY CODE ###

n = int(input())
heights = list(map(int, input().split()))

diffMax = 2 * max(heights)
diffMin = 2 * min(heights)

cnt = 0

for i in range(n - diffMin + 1):
    for j in range(i + diffMin, n):
        if (j - i) == (heights[i] + heights[j]):
            cnt += 1

print(cnt)

### FIXED CODE ###

n = int(input())
heights = list(map(int, input().split()))

diffMax = 2 * max(heights)
diffMin = 2 * min(heights)

cnt = 0

arrSize = min([max([i + heights[i] for i in range(len(heights))]),max([i - heights[i] for i in range(len(heights))])]) + 1

seppenPlus = [0] * arrSize
seppenMinus = [0] * arrSize


for i in range(len(heights)):
    seppenPlusNum = i + heights[i]
    seppenMinusNum = i - heights[i]

    if seppenPlusNum < arrSize:
    	seppenPlus[seppenPlusNum] += 1

    if seppenMinusNum >= 0:
        seppenMinus[seppenMinusNum] += 1

for i in range(len(seppenPlus)):
    cnt += seppenPlus[i] * seppenMinus[i]

print(cnt)
### BUGGY CODE ###

import itertools 
n, m, q = map(int, input().split())
query = [list(map(int, input().split())) for _ in range(q)]

ls = [0] * n + [1] * (m - 1)
mx = 0
for l in itertools.permutations(ls):
    a = [0]
    idx = 1
    for i in l: 
        if i == 0: a.append(idx)
        else: idx += 1
    #print(l, a)
    v = 0
    for i in query:
        if a[i[1]] - a[i[0]] == i[2]: v += i[3]
    mx = max(v, mx)
print(mx)


### FIXED CODE ###

import itertools 
n, m, q = map(int, input().split())
query = [list(map(int, input().split())) for _ in range(q)]

ls = list(range(n + m - 1))
mx = 0
for l in itertools.combinations(ls, m - 1):
    a = [0]
    idx = 0
    val = 1
    for i in range(n + m - 1): 
        if idx < m - 1 and i == l[idx]:
            val += 1
            idx += 1
        else: 
            a.append(val)
    #print(l, a)
    v = 0
    for i in query:
        if a[i[1]] - a[i[0]] == i[2]: v += i[3]
    mx = max(v, mx)
print(mx)
### BUGGY CODE ###

def main():
    mod = 998244353
    s, k = input().split()
    k, n, one, cnt, z, zero_list = int(k), len(s), s.count("1")+1, 0, 0, []
    for i in range(n):
        if s[i] == "0":
            zero_list.append(cnt)
            cnt = 0
        else:
            cnt += 1
    mm = min(one-1, k)
    ans = 0

    dp = [[0]*(mm+1) for _ in [0]*one]
    dp[0][0] = 1

    for i in zero_list:
        dp2 = [[0]*(mm+1) for _ in [0]*one]
        for j in range(one):
            for l in range(max(z+i-j, 0), one-j):
                ml = max(l-i, 0)
                for p in range(min(j, mm-ml)+1):
                    ans += 1
                    dp2[j+l][p+ml] = (dp2[j+l][p+ml]+dp[j][p]) % mod
        z += i
        dp = dp2
    print(sum([sum(i) for i in dp]) % mod)


main()


### FIXED CODE ###

def main():
    mod = 998244353
    s, k = input().split()
    k = int(k)
    n = len(s)

    one = s.count("1")

    cnt = 0
    zero_list = []
    for i in range(n):
        if s[i] == "0":
            zero_list.append(cnt)
            cnt = 0
        else:
            cnt += 1

    z = 0
    mm = min(one, k)

    dp = [[0]*(one+1) for _ in [0]*(one+1)]
    dp[0][0] = 1

    for i in range(len(zero_list)):
        dp2 = [[0]*(mm+1) for _ in [0]*(one+1)]
        base = zero_list[i]
        # j:何個今までに入れたか
        for j in range(one+1):
            # l:何個入れるか
            for l in range(one+1-j):
                if l < z+base-j:
                    continue
                ml = max(l-base, 0)
                # p:これまでのペナルティ
                for p in range(min(one, k)+1):
                    q = p+ml
                    if q <= mm:
                        dp2[j+l][q] = (dp2[j+l][q]+dp[j][p]) % mod
                    else:
                        break
        z += base
        dp = dp2
    print(sum([sum(i) for i in dp]) % mod)


main()
### BUGGY CODE ###

import sys
sys.setrecursionlimit(10**8)
from bisect import bisect_left, bisect_right
from copy import copy, deepcopy
N = int(input())
A = list(map(int, input().split()))
edge = [[] for _ in range(N)]
for i in range(N-1):
    a,b = map(int, input().split())
    edge[a-1].append(b-1)
    edge[b-1].append(a-1)

def dfs(v,LIS):
    if len(edge[v])==0:
        return
    for u in edge[v]:
        if visited[u]==False:
            visited[u]=True
            lis = copy(LIS)
            if A[u] > lis[-1]:
                lis.append(A[u])
            else:
                ind = bisect_left(LIS,A[u])
                lis[ind] = A[u]
            ans[u] = len(lis)
            dfs(u,lis)
    return

ans = [0]*N
visited = [False]*N
visited[0]=True
LIS = [A[0]]
ans[0] = 1
dfs(0,LIS)
print(*ans, sep='\n')

### FIXED CODE ###

import sys
sys.setrecursionlimit(10**8)
from bisect import bisect_left, bisect_right
N = int(input())
A = list(map(int, input().split()))
edge = [[] for _ in range(N)]
for i in range(N-1):
    a,b = map(int, input().split())
    edge[a-1].append(b-1)
    edge[b-1].append(a-1)

def dfs(v,LIS):
    if len(edge[v])==0:
        return
    for u in edge[v]:
        if visited[u]==False:
            visited[u]=True
            if A[u] > LIS[-1]:
                LIS.append(A[u])
                ans[u] = len(LIS)
                dfs(u,LIS)
                LIS.pop()
            else:
                ind = bisect_left(LIS,A[u])
                stack = LIS[ind]
                LIS[ind] = A[u]
                ans[u] = len(LIS)
                dfs(u,LIS)
                LIS[ind] = stack
    return

ans = [0]*N
visited = [False]*N
visited[0]=True
LIS = [A[0]]
ans[0] = 1
dfs(0,LIS)
print(*ans, sep='\n')
### BUGGY CODE ###

import sys
sys.setrecursionlimit(10**7)

def sol():
    N, Q = map(int, input().split())
    edge = [[] for _ in range(N)]

    for _ in range(N - 1):
        a, b = map(int, input().split())
        a -= 1
        b -= 1
        edge[a].append(b)
        edge[b].append(a)

    ans = [0 for _ in range(N)]

    for _ in range(Q):
        p, x = map(int, input().split())
        p -= 1
        ans[p] += x

    def calc(now, parent):
        if parent != -1:
            ans[now] += ans[parent]
        for to in edge[now]:
            if to != parent:
                calc(to, now)
        return

    calc(0, -1)
    print(*ans)


sol()

### FIXED CODE ###

from collections import deque
import sys
input = sys.stdin.buffer.readline

N, Q = map(int, input().split())

edges = [[] for _ in range(N)]
for _ in range(N - 1):
    fr, to = map(lambda a: int(a) - 1, input().split())
    edges[fr].append(to)
    edges[to].append(fr)

P = [0] * N
for _ in range(Q):
    p, x = map(int, input().split())
    P[p - 1] += x

ans = [-1] * N
st = deque([(0, 0)])
while st:
    now, val = st.pop()

    if ans[now] >= 0:
        continue
    val += P[now]
    ans[now] = val

    for to in edges[now]:
        st.append((to, val))

print(*ans)
### BUGGY CODE ###

import collections
h,w = map(int, input().split())
s=[input()for _ in range(h)]

r=[[1,0],[-1,0],[0,1],[0,-1]]
def bfs(i,j):
    global h,w,r
    f=[-1 for _ in range(h*w)]
    que = collections.deque([[i,j,0]])
    while que:
        i,j,k = que.popleft()
        f[i*w+j] =k
        for sr in r:
            ti = i + sr[0]
            tj = j + sr[1]
            if ti>=0 and tj>=0 and ti<h and tj<w:
                if s[ti][tj] == ".":
                    if f[ti*w+tj] == -1:
                        que.append([ti,tj,k+1])
    return max(f)


res = 0
for i in range(h):
    for j in range(w):
        if s[i][j] ==".":
            tres = bfs(i,j)
            res = max(res,tres)
print(res)

### FIXED CODE ###

import collections

h, w = map(int, input().split())
s = [input() for _ in range(h)]
r = [[1, 0], [-1, 0], [0, 1], [0, -1]]


def bfs(i, j):
    global h, w, r
    f = [-1 for _ in range(h * w)]
    que = [[] for _ in range(h * w)]
    que[0].append([i, j])
    for k in range(h * w):
        if que[k]:
            res = k
            for i, j in que[k]:
                f[i * w + j] = k
                for sr in r:
                    ti = i + sr[0]
                    tj = j + sr[1]
                    if ti >= 0 and tj >= 0 and ti < h and tj < w:
                        if s[ti][tj] == ".":
                            if f[ti * w + tj] == -1:
                                que[k + 1].append([ti, tj])
    return res


fl = 0
for i in range(h):
    if "#" in s[i]:
        fl = 1
        break
if fl == 0:
    print(h + w - 2)
else:
    res = 0
    for i in range(h):
        for j in range(w):
            if s[i][j] == ".":
                res = max(res, bfs(i, j))
    print(res)
### BUGGY CODE ###

N, M = [int(i) for i in input().split()]

cnt = min(N, M // 2)
N -= cnt
M -= cnt * 2

while True:
  if N >= 1:
    N -= 1
  elif M >= 2:
    M -= 2
  else:
    break

  if M >= 2:
    cnt += 1
    M -= 2
  else:
    break

print(cnt)


### FIXED CODE ###

N, M = [int(i) for i in input().split()]

cnt = min(N, M // 2)
N -= cnt
M -= cnt * 2

if M > 0:
  cnt += M // 4

print(cnt)
### BUGGY CODE ###

import numpy as np

n = int(input())
h = list(map(int, input().split()))
ans = 0
for i in range(n-1):
    if h[i] > 10**5:
        continue
    num = min(n, i + h[i] + 1)
    s = np.array(h[num:])
    length = len(s)
    u = np.arange(length) + 1
    s -= u
    t = np.count_nonzero(s)
    ans += length - t
print(ans)


### FIXED CODE ###

from collections import Counter

n = int(input())
h = list(map(int, input().split()))
a = []
b = []
for i in range(n):
    a.append(i + h[i])
    b.append(i - h[i])

x = Counter(a)
y = Counter(b)

z = set(a)
ans = 0
for i in z:
    ans += x[i] * y[i]

print(ans)
### BUGGY CODE ###

k, s = map(int, input().split())
ans = 0
for x in range(k+1):
    for y in range(k+1):
        for z in range(k+1):
            if x + y + z == s:
                ans += 1
else:
    print(ans)

### FIXED CODE ###

k, s = map(int, input().split())
ans = 0
for x in range(k+1):
    for y in range(k+1):
        z = s - x - y
        if 0 <= z <= k:
            ans += 1
else:
    print(ans)
### BUGGY CODE ###

n,m=map(int,input().split())
mod=10**9+7
sn=1
sm=1
for i in range(1,n+1):
    sn*=i
for j in range(1,m+1):
    sm*=j
if 2<=abs(n-m):
    print(0)
elif abs(n-m)==1:
    print(sn*sm%mod)
elif n==m:
    print(2*sn*sm%mod)


### FIXED CODE ###

import math
mod=10**9+7
n,m=map(int,input().split())
ans=0
if abs(n-m)==0:
    ans=math.factorial(n)*math.factorial(m)*2
elif abs(n-m)==1:
    ans=math.factorial(n)*math.factorial(m)
else:
    ans=0
ans=ans%mod
print(ans)
### BUGGY CODE ###

import sys
from collections import Counter
input = sys.stdin.readline
n = int(input())
a = list(map(int,input().split()))
ansList = [1]*n

for i in range(n):
    if ansList[i] == 1:
        for j in range(i+1,n):
            if a[i]%a[j] == 0:
                ansList[i] = 0
            if a[j]%a[i] == 0:
                ansList[j] = 0

c = Counter(ansList)
print(c[1])

### FIXED CODE ###

# 解説を見て解きました。

n = int(input())
a = sorted(list(map(int,input().split())))
# aの最大値+1(0を含むので)
m_a = a[-1]+1
# dp[i](aの最小値<=i<=aの最大値)が1の個数の合計が答え
dp = [0]*m_a

for i in a:
    # dp[i]の初期値は0なので1を足す
    dp[i] += 1
    # dp[i]が2以上の時は列挙する必要がないのでパス
    if dp[i] == 1:
        # aの最大値までのiの倍数を列挙
        for j in range(i*2, m_a, i):
            # 倍数をはじくためにdp[j]を2以上にする
            dp[j] += 2

print(dp.count(1))
### BUGGY CODE ###

import itertools

n  = int(input())

max_m = len(str(n))

ans = 0

for m in range(1,max_m+1):
  for i in range(1, m):
    for j in range(1, m-i):
      k = m-i-j
      ls = ['7']*i+['5']*j+['3']*k
      for l in set(itertools.permutations(ls)):
        s = ''.join(l)
        s = int(s)
        if s <= n:
          ans += 1
          #print(s)
print(ans)

### FIXED CODE ###

n = int(input())

def dfs(s):
  ret = 0
  if int(s) > n:
    return 0
  if all(s.count(c) > 0 for c in '753'):
    ret = 1
  for c in '753':
    ret += dfs(s+c)
  return ret

print(dfs('0'))
### BUGGY CODE ###

n = int(input())
A = list(map(int,input().split()))
A.append(-float("inf"))
n = len(A)

A.sort()
s = 2
while s <= A[-1]+A[-2]:
    s *= 2
s //= 2

U = [0] * n

ans = 0
while s > 0:
    ll = 0
    rr = n-1
    while rr >= 1 and rr-ll >= 2:
        # print("rr",rr,"ll",ll)
        t = s - A[rr]
        # print("s",s,"t",t)
        l = ll
        r = rr-1
        m = (r+l)//2
        while r-l > 1:
            # print("r",r,"l",l,"m",m)
            if A[m] >= t:
                r = m
            else:
                l = m
            m = (r+l)//2
        if A[l+1] == t:
            ans += 1
            U[l+1] = 1
            U[rr] = 1
            # print(ans)
            A[l+1] = -float("inf")
        ll = l
        rr -= 1
        # print(A)
    s //= 2

    B = []
    for i in range(len(A)):
        if U[i] == 0:
            B.append(A[i])
    A = B
    n = len(A)
    U = [0] * n
print(ans)


### FIXED CODE ###

n = int(input())
A = list(map(int,input().split()))
A.append(-float("inf"))
n = len(A)

A.sort()
s = 2
while s <= A[-1]+A[-2]:
    s *= 2
s //= 2

U = [0] * n

ans = 0
while s > 0:
    ll = 0
    rr = n-1
    while rr >= 1 and s <= A[rr]:
        rr -= 1
    while rr >= 1 and rr-ll >= 2:
        # print("rr",rr,"ll",ll)
        t = s - A[rr]

        # print("s",s,"t",t)
        l = ll
        r = rr-1
        m = (r+l)//2
        while r-l > 1:
            # print("r",r,"l",l,"m",m)
            if A[m] >= t:
                r = m
            else:
                l = m
            m = (r+l)//2
        if A[l+1] == t:
            ans += 1
            U[l+1] = 1
            U[rr] = 1
            # print(ans)
            A[l+1] = -float("inf")
        ll = l
        rr -= 1
        # print(A)
    s //= 2

    B = []
    for i in range(len(A)):
        if U[i] == 0:
            B.append(A[i])
    A = B
    n = len(A)
    U = [0] * n
print(ans)
### BUGGY CODE ###

N=int(input())
ls=[int(s) for s in input().split()]
ls.sort()
ls.reverse()
ls2=[ls[0],ls[1]]
def plus(lst,num):
  val=0
  n=len(lst)
  for i in range(n):
    for k in range(1,n+1):
      if i+k>=n:
        val+=k-1
        break
      if lst[i]-lst[i+k]>=num:
        val+=k-1
        break
  return val
ans=0
for j in range(2,N):
  ans+=plus(ls2,ls[j])
  ls2.append(ls[j])
print(ans)

### FIXED CODE ###

import bisect
N=int(input())
ls=[int(s) for s in input().split()]
ls.sort()
ans=0
for i in range(1,N-1):
  for j in range(i+1,N):
    k = bisect.bisect_right(ls[:i],ls[j]-ls[i])
    ans+=i-k
print(ans)
### BUGGY CODE ###

n = int(input())
s = [input() for i in range(n)]
ans = {}
word_list = list(set(s))

for i in word_list:
  ans[i] = s.count(i)

for k, v in sorted(ans.items()):
  if v == max(ans.values()):
    print(k)

### FIXED CODE ###

from collections import Counter
n = int(input())
s = [input() for _ in range(n)]
ans = []
charctor_count = Counter(s)
max_count = max(charctor_count.values())
for k, v in charctor_count.items():
  if v == max_count:
    ans.append( k)
print("\n".join(sorted(ans)))
### BUGGY CODE ###

def main():
    N, X, M = map(int, input().split())
    NN = N
    li = []
    while X not in li and N != 0:
        li = li + [X]
        X = X ** 2 % M
        N -= 1
    
    if N == 0:
        print(sum(x for x in li))

    elif N != 0 and X in li:
        l = len(li)
        s = li.index(X)
        T = l - s
        q = (NN - s) // T
        r = (NN - s) % T
        print(sum(li[i] for i in range(s)) + sum(li[i] for i in range(s, len(li))) * q + sum(li[i] for i in range(s, s + r)))


main()

### FIXED CODE ###

N, X, M = map(int, input().split())
NN = N
li = []
isused = [False] * M
while isused[X] == False and N != 0:
    li.append(X)
    isused[X] = True
    X = (X ** 2) % M
    N -= 1
    
if N == 0:
    print(sum(li))

elif N != 0 and X in li:
    l = len(li)
    s = li.index(X)
    T = l - s
    q = (NN - s) // T
    r = (NN - s) % T
    print(sum(li) + sum(li[i] for i in range(s, len(li))) * (q-1) + sum(li[i] for i in range(s, s + r)))