qyliang/CodeNanoFix huggingface dataset:
Buggy code:
# Date [ 2020-09-19 21:46:14 ]
# Problem [ e.py ]
# Author Koki_tkg

import sys
# import math
# import bisect
# import numpy as np
# from decimal import Decimal
# from numba import njit, i8, u1, b1 #JIT compiler
# from itertools import combinations, product
# from collections import Counter, deque, defaultdict

# sys.setrecursionlimit(10 ** 6)
MOD = 10 ** 9 + 7
INF = 10 ** 9
PI = 3.14159265358979323846

def read_str():      return sys.stdin.readline().strip()
def read_int():      return int(sys.stdin.readline().strip())
def read_ints():     return map(int, sys.stdin.readline().strip().split())
def read_ints2(x):   return map(lambda num: int(num) - x, sys.stdin.readline().strip().split())
def read_str_list(): return list(sys.stdin.readline().strip().split())
def read_int_list(): return list(map(int, sys.stdin.readline().strip().split()))
def GCD(a: int, b: int) -> int: return b if a%b==0 else GCD(b, a%b)
def LCM(a: int, b: int) -> int: return (a * b) // GCD(a, b)

def Main():
    n, x, m = read_ints()
    query = []
    s = set([])
    while x not in s:
        s.add(x)
        query.append(x)
        x = x * x % m
    
    frm = query.index(x)
    length = len(query) - frm
    total = sum(query[frm:])
    
    ans = sum(query[:frm]) + total * (n - frm) // length + sum(query[frm:frm + (n - frm) % length])
    print(ans)

if __name__ == '__main__':
    Main()

Fixed code:
# Date [ 2020-09-19 21:46:14 ]
# Problem [ e.py ]
# Author Koki_tkg

import sys
# import math
# import bisect
# import numpy as np
# from decimal import Decimal
# from numba import njit, i8, u1, b1 #JIT compiler
# from itertools import combinations, product
# from collections import Counter, deque, defaultdict

# sys.setrecursionlimit(10 ** 6)
MOD = 10 ** 9 + 7
INF = 10 ** 9
PI = 3.14159265358979323846

def read_str():      return sys.stdin.readline().strip()
def read_int():      return int(sys.stdin.readline().strip())
def read_ints():     return map(int, sys.stdin.readline().strip().split())
def read_ints2(x):   return map(lambda num: int(num) - x, sys.stdin.readline().strip().split())
def read_str_list(): return list(sys.stdin.readline().strip().split())
def read_int_list(): return list(map(int, sys.stdin.readline().strip().split()))
def GCD(a: int, b: int) -> int: return b if a%b==0 else GCD(b, a%b)
def LCM(a: int, b: int) -> int: return (a * b) // GCD(a, b)

def Main():
    n, x, m = read_ints()
    query = []
    s = set([])
    while x not in s:
        s.add(x)
        query.append(x)
        x = x * x % m
    
    frm = query.index(x)
    length = len(query) - frm
    total = sum(query[frm:])
    
    ans = sum(query[:frm]) + total * ((n - frm) // length) + sum(query[frm:frm + (n - frm) % length])
    print(ans)

if __name__ == '__main__':
    Main()
Buggy code:
n, x, m = list(map(int, input().split()))

count = 0

# table2jou = []
# for mi in range(m + 1):
#     table2jou.append(pow(mi, 2))

tablef = []
for mi in range(m + 1):
    # tablef.append(table2jou[mi] % m)
    tablef.append(pow(mi, 2, m))


count = x
pre_an = x
his = [x]
for ni in range(2, n + 1):
    an = tablef[pre_an]
    if an in his:
        leftnum = n - ni + 1
        start = his.index(an)
        end = leftnum % (len(his) - start) + start
        loopnum = leftnum // (len(his) - start)
        count = count + sum(his[start: end + 1])
        count = count + sum(his[start:]) * loopnum
        break
    his.append(an)
    count = count + an
    pre_an = an

print(count)


Fixed code:
n, x, m = list(map(int, input().split()))

count = 0

# table2jou = []
# for mi in range(m + 1):
#     table2jou.append(pow(mi, 2))

# tablef = []
# for mi in range(m + 1):
#     # tablef.append(table2jou[mi] % m)
#     tablef.append(pow(mi, 2, m))

is_used = [0] * (m + 1)

count = x
pre_an = x
his = [x]
for ni in range(2, n + 1):
    # an = tablef[pre_an]
    an = pow(pre_an, 2, m)
    if is_used[an] == 1:
        leftnum = n - ni + 1
        start = his.index(an)
        end = leftnum % (len(his) - start) + start
        loopnum = leftnum // (len(his) - start)
        count = count + sum(his[start: end])
        count = count + sum(his[start:]) * loopnum
        break
    his.append(an)
    is_used[an] = 1
    count = count + an
    pre_an = an

print(count)
Buggy code:
n,x,m=map(int,input().split())
cnt2=set()
cnt2.add(x)
cnt3=[x]
cnt4=0
cnt5=0
cnt6=x
for i in range(1,m+10):
    num2=pow(cnt6,2,m)
    if num2 in cnt2:
        for j in range(len(cnt3)):
            if cnt3[j]==num2:
                cnt7=j
                break
        cnt10=len(cnt3)-cnt7
        num4=sum(cnt3[:cnt7])
        cnt4=len(cnt3)
        break
    cnt2.add(num2)
    cnt3.append(num2)
    cnt6=num2
    if num2==0:
        ans=sum(cnt3)
        cnt5=1
        break
if cnt5==0:
    ans=num4
    n-=cnt7
    ans+=(n//cnt10)*sum(cnt3[cnt7:])
    ans+=sum(cnt3[cnt7:(n%cnt10)+cnt7])
if cnt4>=n:
    ans=sum(cnt3[:n])
print(ans)


Fixed code:
n,x,m=map(int,input().split())
cnt2=set()
cnt2.add(x)
cnt3=[x]
cnt4=0
cnt5=0
cnt6=x
for i in range(1,m+10):
    num2=pow(cnt6,2,m)
    if num2 in cnt2:
        for j in range(len(cnt3)):
            if cnt3[j]==num2:
                cnt7=j
                break
        cnt10=len(cnt3)-cnt7
        num4=sum(cnt3[:cnt7])
        cnt4=len(cnt3)
        break
    cnt2.add(num2)
    cnt3.append(num2)
    cnt6=num2
    if num2==0:
        ans=sum(cnt3)
        cnt5=1
        break
if cnt5==0 and n>cnt4:
    ans=num4
    n-=cnt7
    ans+=(n//cnt10)*sum(cnt3[cnt7:])
    ans+=sum(cnt3[cnt7:(n%cnt10)+cnt7])
if cnt4>=n:
    ans=sum(cnt3[:n])
print(ans)
Buggy code:
N, X, M = [int(x) for x in input().split()]

A = [0] * (M + 1)
firstApearAt = {i:0 for i in range(M)}
A[1] =  X
for i in range(2, M + 1):
    A[i] = (A[i - 1] * A[i - 1]) % M
    if firstApearAt[A[i]]:
        r = i
        l = firstApearAt[A[i]]
        break
    firstApearAt[A[i]] = i
S = sum(A[l:r])

ans = 0
if N <= l - 1:
    ans = sum(A[1:N + 1])
else:
    q, p = (N - l + 1) // (r - l), (N - l + 1) % (r - l)
    ans = sum(A[1:l]) + S * q + sum(A[r:r + p])

print(ans)

Fixed code:
N, X, M = [int(x) for x in input().split()]

A = [0] * (M + 1)
firstApearAt = {i:0 for i in range(M)}
A[1] =  X
firstApearAt[X] = 1
l, r = 1, 2
for i in range(2, M + 1):
    A[i] = (A[i - 1] * A[i - 1]) % M
    if firstApearAt[A[i]] > 0:
        r = i
        l = firstApearAt[A[i]]
        break
    firstApearAt[A[i]] = i

ans = 0
if N <= l - 1:
    ans = sum(A[1:N + 1])
else:
    q, p = (N - l + 1) // (r - l), (N - l + 1) % (r - l)
    ans = sum(A[1:l]) + q * sum(A[l:r]) + sum(A[l:l + p])

print(ans)
Buggy code:


def main():
    n, x, m = map(int, input().split())
    mflg = [0] + [-1] * (m-1)
    ltot = [x]

    tot = x
    a = x
    for i in range(1, n):
        a = a ** 2 % m
        if mflg[a-1] >= 0:
            z = (n - i) // (i - mflg[a-1]) - 1
            y = (n - i) % (i - mflg[a-1])
            tot += (ltot[mflg[a-1] - 1 + y] - ltot[mflg[a-1] - 1]) + (ltot[-1] - ltot[mflg[a-1] - 1]) * z
            break

        mflg[a-1] = i
        tot += a
        ltot.append(tot)

    print(tot)


main()



Fixed code:


def main():
    n, x, m = map(int, input().split())
    mflg = [-1] * m
    mflg[x] = 0
    ltot = [x]

    tot = x
    a = x
    for i in range(1, n):
        a = a ** 2 % m

        if mflg[a] >= 0:
            b = mflg[a]
            z = (n - i) // (i - b)
            y = (n - i) % (i - b)
            if b == 0 and y == 0:
                tot += ltot[i - 1] * z
            elif b == 0:
                tot += ltot[b - 1 + y] + ltot[i - 1] * z
            else:
                tot += (ltot[b - 1 + y] - ltot[b - 1]) + (ltot[i - 1] - ltot[b - 1]) * z
            break

        mflg[a] = i
        tot += a
        ltot.append(tot)

    print(tot)


main()
Buggy code:
import sys
from sys import stdin
def I():
    return stdin.readline().rstrip()
def MI():
    return map(int,stdin.readline().rstrip().split())
def LI():
    return list(map(int,stdin.readline().rstrip().split()))
#main part
n,x,m=MI()
stock=[]
for _ in range(1,n+1):
    if x not in stock:
        stock.append(x)
        x=(x**2)%m
    else:
        start=stock.index(x)
        repeat=stock[start:]
        l=len(repeat)
        n-=start
        sho=n//l
        amari=n%l
        s=sum(repeat)
        rest=0
        if amari!=0:
            for j in range(amari):
                rest+=repeat[j]
        print(s*sho+amari+sum(stock[:start]))
        sys.exit()
print(sum(stock))

Fixed code:
import sys
from sys import stdin
def I():
    return stdin.readline().rstrip()
def MI():
    return map(int,stdin.readline().rstrip().split())
def LI():
    return list(map(int,stdin.readline().rstrip().split()))
#main part
n,x,m=MI()
stock=[]
for _ in range(1,n+1):
    if x not in stock:
        stock.append(x)
        x=(x**2)%m
        continue
    else:
        start=stock.index(x)
        repeat=stock[start:]
        l=len(repeat)
        n-=start
        sho=n//l
        amari=n%l
        s=sum(repeat)
        rest=0
        if amari!=0:
            for j in range(amari):
                rest+=repeat[j]
        print(s*sho+rest+sum(stock[:start]))
        sys.exit()
print(sum(stock))
Buggy code:
n,x,m = map(int,input().split())
mod = m
amari = [-1]*m
a = x
cnt = 1
l = 0
r = 0
while 1:
    # print(a)
    if cnt == n:
        break
    
    if amari[a] != -1:
        l = amari[a]
        r = cnt
        # print(a,amari[a])
        break
    else:
        amari[a] = cnt
    cnt += 1
    a = ((a % m)**2) % m
    # if cnt > m:
    #     print("error")
    #     exit()
# print(l,r,amari)
ans = 0
if cnt == n:
    l = 0
    r = n

a = x
temp = 0
for i in range(l-1):
    ans += a
    a = (a**2) % mod
for i in range(r-l):
    temp += a
    a = (a**2) % mod
ans += temp * (n-l)//(r-l)
for i in range((n-l-1)%(r-l)):
    ans += a
    a = (a**2) % mod
print(ans)

Fixed code:
n,x,m = map(int,input().split())
mod = m
amari = [-1]*m
a = x
cnt = 1
l = 0
r = 0
while 1:
    # print(a)
    if cnt == n:
        break
    
    if amari[a] != -1:
        l = amari[a]
        r = cnt
        # print(a,amari[a])
        break
    else:
        amari[a] = cnt
    cnt += 1
    a = ((a % m)**2) % m
    # if cnt > m:
    #     print("error")
    #     exit()
ans = 0
if cnt == n:
    l = 0
    r = n
# print(l,r)
a = x
temp = 0

for i in range(l-1):
    ans += a
    a = (a**2) % mod
# print(ans)


for i in range(r-l):
    temp += a
    a = (a**2) % mod
ans += temp * ((n-l+1)//(r-l))
# print(ans,a,temp,temp * (n-l)//(r-l),(n-l)//(r-l),temp * ((n-l)//(r-l)))


if r != n:
    for i in range((n-l+1)%(r-l)):
        ans += a
        a = (a**2) % mod
        # print(ans)
if n == 1:
    ans //=2
print(ans)
Buggy code:
N, X, M = map(int, input().split())
A = X
P = set([A])
cnt, cnt2 = -1, -1
S = A
S2 = 0
off = -1
for i in range(N-1):
    A = A**2%M
    if A in P and cnt == -1:
        cnt = i
        off = A
    elif A ==off:
        cnt2 = i
        break
    S += A
    if cnt != -1:
        S2 += A
    P.add(A)
l = max(cnt2-cnt,1)
if cnt2==-1:
    res = S
else:
    res = 0
    print(l, S, S2, cnt, cnt2)
    for i in range((N-cnt-1)%l):
        res += off
        off = off**2%M
    res += S + ((N-cnt2-1)//l)*S2
print(res)

Fixed code:
N, X, M = map(int, input().split())
A = X
P = set([A])
cnt, cnt2 = -1, -1
S = A
S2 = 0
off = -1
for i in range(N-1):
    A = A**2%M
    if A in P and cnt == -1:
        cnt = i
        off = A
    elif A ==off:
        cnt2 = i
        break
    S += A
    if cnt != -1:
        S2 += A
    P.add(A)
l = max(cnt2-cnt,1)
if cnt2==-1:
    res = S
else:
    res = 0
    for i in range((N-cnt-1)%l):
        res += off
        off = off**2%M
    res += S + ((N-cnt2-1)//l)*S2
print(res)
"""
9 4 7
"""
Buggy code:
n, x, m =  map(int, input().split())
ans = 0
loop_list = [0] * m
loop_list[x] = 1
order_list = [x]

for i in range(1, m + 1):
    x = pow(x, 2, m)
    if loop_list[x] != 1:
        loop_list[x] = 1
        order_list.append(x)
    else:
        break

index = order_list.index(x)
time = (n - index) // (len(order_list) - index)
mod = (n - index) % (len(order_list) - index)

ans += sum(order_list[:index - 1])
ans += sum(order_list[index:]) * time
ans += sum(order_list[index:index + mod - 1])

print(ans)


Fixed code:
n, x, m =  map(int, input().split())
ans = 0
loop_list = [0] * m
loop_list[x] = 1
order_list = [x]

for i in range(1, m + 1):
    x = pow(x, 2, m)
    if loop_list[x] != 1:
        loop_list[x] = 1
        order_list.append(x)
    else:
        break

index = order_list.index(x)
time = (n - index) // (len(order_list) - index)
mod = (n - index) % (len(order_list) - index)

ans += sum(order_list[:index])
ans += sum(order_list[index:]) * time
ans += sum(order_list[index:index + mod])

print(ans)
Buggy code:
N, X, M = map(int, input().split())

ans = X
x = X

C = [0, x]
Xd = [-1] * (M+1)
Xd[x] = 0
Xl = [x]

for i in range(1, N):
    x = x**2 % M

    if Xd[x] > -1:
        break
    Xd[x] = i
    Xl.append(x)
    ans += x
    C.append(ans)

# print(Xl)
# print(C)
# print(Xd[x], i)
loop_len = i - Xd[x]
# print(loop_len)
if loop_len > 0:
    #S = C[i] - C[Xd[x]]
    # print(S)
    loop_num = (N - i) // loop_len
    # print(loop_num)
    #ans += loop_num * S
    m = N - loop_num * loop_len - i
    # print(ans)
    # print(m)
    ans += C[Xd[x]+m] - C[Xd[x]]

# print(S)

print(ans)

Fixed code:
N, X, M = map(int, input().split())

ans = X
x = X

C = [0, x]
Xd = [-1] * (M+1)
Xd[x] = 0
i = 1

for i in range(1, N):
    x = x**2 % M

    if Xd[x] > -1:
        break
    Xd[x] = i
    ans += x
    C.append(ans)

loop_len = i - Xd[x]
if loop_len > 0:
    S = C[i] - C[Xd[x]]
    loop_num = (N - i) // loop_len
    ans += loop_num * S
    m = N - loop_num * loop_len - i
    ans += C[Xd[x]+m] - C[Xd[x]]

print(ans)
Buggy code:
#import sys
#print(sys.maxsize)
N,X,M=map(int,input().split())
table=list(range(M))
for i in range(M):
    table[i]=((table[i]%M)**2)%M
print(table[471])
start=X%M
rireki=list()
rireki.append(start)
ss=0
for j in range(1,N):
    if table[start] in rireki:
        ss=rireki.index(table[start])
        break
    else:
        rireki.append(table[start])
        start=table[start]
nn=len(rireki)
rep=(N-ss)//(nn-ss)
nokori=N-ss-rep*(nn-ss)
loop=rireki[ss:]
#print(nn)
#print(rep)
#print(sum(loop))
#print(ss)
#print(nokori+rep*(nn-ss)+ss)
if 0 in rireki:
    goukei=sum(rireki)
else:
    goukei=rep*sum(loop)+sum(rireki[:ss])+sum(loop[:nokori])
print(goukei)

Fixed code:
#import sys
#print(sys.maxsize)
N,X,M=map(int,input().split())
#table=list(range(M))
#for i in range(M):
 #   table[i]=((table[i]%M)**2)%M
#print(table[471])
start=X%M
rset=set()
rireki=list()
rset.add(start)
rireki.append(start)
ss=0
for j in range(1,M+1):
    start=(start**2)%M
    if start in rset:
        ss=rireki.index(start)
        break
    else:
        rireki.append(start)
        rset.add(start)
nn=len(rireki)
rep=(N-ss)//(nn-ss)
nokori=N-ss-rep*(nn-ss)
loop=rireki[ss:]
#print(nn)
#print(rep)
#print(sum(loop))
#print(ss)
#print(nokori+rep*(nn-ss)+ss)
if 0 in rireki:
    goukei=sum(rireki)
else:
    goukei=rep*sum(loop)+sum(rireki[:ss])+sum(loop[:nokori])
print(goukei)
Buggy code:
from collections import defaultdict
n, x, m = list(map(int, input().split()))
dp = [False]*(m+1)
a = [0]*(m+1)
d = defaultdict(lambda: set())

for i in range(1, m+1):
    if i == 1:
        a[i] = x
        dp[x] = True
    else:
        a[i] = a[i-1]**2 % m
        if not dp[a[i]]:
            d[a[i-1]] = a[i]
            dp[a[i]] = True
        else:
            startv = a[i]
            break
start_idx = a.index(a[i])
loop_haba = i-start_idx
loop_sum = sum(a[start_idx:i])
# print(a)
# print(start_idx, loop_sum, loop_haba)
if n <= (start_idx+loop_haba-1):
    print(sum(a[:n+1]))
else:
    ans = sum(a[:(start_idx+1)])
    ans += ((n-start_idx)//loop_haba)*loop_sum
    ans += sum(a[start_idx: start_idx+((n-start_idx) % loop_haba)+1])
    print(ans)


Fixed code:
from collections import defaultdict
n, x, m = list(map(int, input().split()))
dp = [False]*(m+1)
a = [0]*(m+1)
d = defaultdict(lambda: set())
flag = False
for i in range(1, min(m+1, n+1)):
    if i == 1:
        a[i] = x
        dp[x] = True
    else:
        a[i] = a[i-1]**2 % m
        if not dp[a[i]]:
            d[a[i-1]] = a[i]
            dp[a[i]] = True

        else:
            startv = a[i]
            flag = True

            break
start_idx = a[1:].index(a[i])+1
loop_haba = i-start_idx
loop_sum = sum(a[start_idx:i])
if not flag:
    print(sum(a[:n+1]))
else:
    ans = sum(a[:(start_idx)])

    ans += ((n-start_idx)//loop_haba)*loop_sum
    ans += sum(a[start_idx: start_idx+((n-start_idx) % loop_haba)+1])
    print(ans)
Buggy code:
n,x,m = map(int,input().split())
a = []
a.append(0)
a.append(x)
for i in range(1,n):
  #tmp = (a[i]**2)%m
  tmp = (a[i]%m)*(a[i]%m)
  tmp = tmp%m
  if tmp==0:
    break
  if tmp in a:
    break
  a.append(tmp)
#print(a)
i = 1
while a[i]!=tmp:
  i+=1
kurikaeshi = len(a) - i
goukei = sum(a[i:])
#print(goukei)
tmp=(n-(len(a)-1))
#print(tmp//kurikaeshi)
a.append(tmp//kurikaeshi * goukei)
#print(a)
tmp = tmp%kurikaeshi
if tmp!=0:
  t = sum(a[i:i+tmp])
  a.append(t)
print(sum(a))

Fixed code:
n,x,m = map(int,input().split())
a = []
a.append(0)
a.append(x)
isBreak = False
for i in range(1,n):
  #tmp = (a[i]**2)%m
  tmp = (a[i]%m)*(a[i]%m)
  tmp = tmp%m
  if tmp==0:
    break
  if tmp in a:
    isBreak = True
    break
  a.append(tmp)
#print(a)
if isBreak:
  i = 1
  while a[i]!=tmp:
    i+=1
  kurikaeshi = len(a) - i
  goukei = sum(a[i:])
  #print(goukei)
  tmp=(n-(len(a)-1))
  #print(tmp//kurikaeshi)
  a.append(tmp//kurikaeshi * goukei)
  #print(a)
  tmp = tmp%kurikaeshi
  if tmp!=0:
    t = sum(a[i:i+tmp])
    a.append(t)
print(sum(a))
Buggy code:
N, X, M = map(int, input().split())
x = X
nums = []
isSearched = [False] * (M+1)
isSearchedNums = [-1] * (M+1)
n = 0
looped = 0
while True:
    x = (x*x) % M
    if isSearched[x]:
        looped = isSearchedNums[x]
        break
    isSearched[x] = True
    isSearchedNums[x] = n
    nums.append(x)
    n+=1
if (isSearched[0]):
    ans = X
    for i in range(min((N-1), len(nums))):
        ans += nums[i]

    print(ans)
else:
    ans = X
    al = 0
    d = len(nums) - looped
    for i in range(looped, len(nums)):
        al += nums[i]

    if N - 1 > len(nums):
        N -= len(nums)
        ans += sum(nums)
        ans += al * ((N-1) // d)
        for i in range(looped, ((N-1) % d) + looped):
            ans += nums[i]
    else:
        for i in range(i, N - 1):
            ans += nums[i]

    print(ans)



Fixed code:
N, X, M = map(int, input().split())
x = X
nums = []
isSearched = [False] * (M+1)
isSearchedNums = [-1] * (M+1)
n = 0
looped = 0
while True:
    x = (x*x) % M
    if isSearched[x]:
        looped = isSearchedNums[x]
        break
    isSearched[x] = True
    isSearchedNums[x] = n
    nums.append(x)
    n+=1
if (isSearched[0]):
    ans = X
    for i in range(min((N-1), len(nums))):
        ans += nums[i]

    print(ans)
else:
    ans = X
    al = 0
    d = len(nums) - looped
    for i in range(looped, len(nums)):
        al += nums[i]
    if N - 1 > len(nums):
        N -= len(nums)
        ans += sum(nums)
        ans += al * ((N-1) // d)
        for i in range(looped, ((N-1) % d) + looped):
            ans += nums[i]
    else:
        for i in range(N - 1):
            ans += nums[i]

    print(ans)
Buggy code:
import sys

input = sys.stdin.readline

n, x, m = map(int, input().split())


arr = [-1] * m
cumsum = [x]
a = x
for i in range(1, n):
    a *= a
    a %= m
    if arr[a] == -1:
        arr[a] = i
        cumsum.append(cumsum[-1] + a)
    else:
        print(i)
        di, mo = divmod(n - i, i - arr[a])

        ans = cumsum[-1]
        ans += di * (cumsum[-1] - cumsum[arr[a] - 1])
        ans += cumsum[arr[a] - 1 + mo] - cumsum[arr[a] - 1]
        break
else:
    ans = cumsum[-1]

print(ans)


Fixed code:
import sys

input = sys.stdin.readline

n, x, m = map(int, input().split())

arr = [-1] * m
cumsum = [x]
a = x
for i in range(1, n):
    a *= a
    a %= m
    if arr[a] == -1:
        arr[a] = i
        cumsum.append(cumsum[-1] + a)
    else:
        di, mo = divmod(n - i, i - arr[a])

        ans = cumsum[-1]
        ans += di * (cumsum[-1] - cumsum[arr[a] - 1])
        ans += cumsum[arr[a] - 1 + mo] - cumsum[arr[a] - 1]
        break
else:
    ans = cumsum[-1]

print(ans)
Buggy code:
n, x, m = map(int, input().split())
nxt = [-1] * m
now = x
while nxt[now] == -1:
    nxt[now] = now * now % m
    now = nxt[now]
junction = now
ans = x
now = x
n = n - 1
while n and now != junction:
    now = nxt[now]
    n -= 1
    ans += now
if n == 0:
    print(ans)
    exit()

llen = 1
lsum = junction
now = nxt[junction]
while now != junction:
    now = nxt[now]
    llen += 1
    lsum += now
ans += lsum * (n // llen)
n = n % llen

now = junction
while n:
    now = nxt[now]
    n -= 1
    ans += now
print(ans)

Fixed code:
n, x, m = map(int, input().split())
nxt = [-1] * m
now = x
while nxt[now] == -1:
    nxt[now] = now * now % m
    now = nxt[now]
junction = now
ans = x
now = x
n = n - 1
while n and now != junction:
    now = nxt[now]
    n -= 1
    ans += now
if n == 0:
    print(ans)
    exit()

llen = 1
lsum = junction
now = nxt[junction]
while now != junction:
    llen += 1
    lsum += now
    now = nxt[now]
ans += lsum * (n // llen)
n = n % llen

now = junction
while n:
    now = nxt[now]
    n -= 1
    ans += now
print(ans)
Buggy code:
n, x, m = map(int, input().split())
amr = []
for i in range(n):
    if i == 0:
        x %= m
        amr.append(x)
        continue
    x = pow(x, 2, m)

    if x in amr:
        print('----', i)
        break
    if x == 0:
        break
    amr.append(x)

if x == 0:
    print(sum(amr[:n]))
else:
    idx = amr.index(x)

    loop = sum(amr[idx:])
    l = len(amr[idx:])

    if n >= idx:
        cnt = (n-idx)//l
        print(cnt*loop + sum(amr[:idx]) + sum(amr[idx:idx+n-idx-cnt*l:]))
    else:
        print(sum(amr[:n]))



Fixed code:
n, x, m = map(int, input().split())
amr = []
for i in range(n):
    if i == 0:
        x %= m
        amr.append(x)
        continue
    x = pow(x, 2, m)

    if x in amr:
        break
    if x == 0:
        break
    amr.append(x)

if x == 0:
    print(sum(amr[:n]))
else:
    idx = amr.index(x)

    loop = sum(amr[idx:])
    l = len(amr[idx:])

    if n >= idx:
        cnt = (n-idx)//l
        print(cnt*loop + sum(amr[:idx]) + sum(amr[idx:idx+n-idx-cnt*l:]))
    else:
        print(sum(amr[:n]))
Buggy code:
import sys

def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def main():
    n, x, m = MI()
    a = x
    flag = [False]*(m+1)
    t = 0
    s = []
    idx = 0
    for _ in range(m+1):
        if flag[a]:
            idx = s.index(a)
            break
        flag[a] = True
        s.append(a)
        t += a
        a = (a*a) % m
        if a == 0:
            print(t)
            sys.exit()
    ans = sum(s[0:idx])
    ans += sum(s[idx:])*((n-idx)//len(s))
    ans += sum(s[idx:idx+(n-idx)%len(s)+1])
    print(ans)

if __name__ == '__main__':
    main()

Fixed code:
import sys

def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def main():
    n, x, m = MI()
    a = x
    flag = [False]*(m+1)
    t = 0
    s = []
    idx = 0
    for _ in range(m+1):
        if flag[a]:
            idx = s.index(a)
            break
        flag[a] = True
        s.append(a)
        t += a
        a = (a*a) % m
        if a == 0:
            print(t)
            sys.exit()
    ans = sum(s[0:idx])
    ans += sum(s[idx:])*((n-idx)//(len(s)-idx))
    ans += sum(s[idx:idx+(n-idx)%(len(s)-idx)])
    print(ans)

if __name__ == '__main__':
    main()