Buggy python submission (verdict: Runtime Error):
# 繰り返しの始まり + 繰り返し + 途中で終わった繰り返し分
N,X,M = map(int,input().split())
p = [0] * (M+2)
sum = [0] * (M+2)
p[X] = 1
sum[1] = X

repeat_start = 0
repeat_end = 0

for i in range(2,N+1):
  X = (X**2) % M
  if p[X] != 0:
    repeat_start = p[X]
    repeat_end = i
    break
  else:
    sum[i] = sum[i-1] + X
    p[X] += i

if repeat_start == 0:
  print(sum[N])
else:
  repeat_cnt,mod=divmod(N-repeat_start-1,repeat_end-repeat_end)
  print(d*(sum[repeat_end-1]-sum[repeat_start-1]) + sum[repeat_start+mod-1])


Fixed submission:
# 繰り返しの始まり + 繰り返し + 途中で終わった繰り返し分
N,X,M = map(int,input().split())
p = [0] * (M+2)
sum = [0] * (M+2)
p[X] = 1
sum[1] = X

repeat_start = 0
repeat_end = 0

for i in range(2,N+1):
  X = (X**2) % M
  if p[X] != 0:
    repeat_start = p[X]
    repeat_end = i
    break
  else:
    sum[i] = sum[i-1] + X
    p[X] += i

if repeat_start == 0:
  print(sum[N])
else:
  repeat_cnt,mod=divmod(N-repeat_start+1,repeat_end-repeat_start)
  print(repeat_cnt*(sum[repeat_end-1]-sum[repeat_start-1]) + sum[repeat_start+mod-1])

Buggy python submission (verdict: Runtime Error):
N,X,M = map(int,input().split())

cnt = [False] + [1] + [0] * (M-1)
numbers = [X]
ans = 0

if M == 0:
  print(0)
  exit()
for n in range(N):
  X = pow((X % M),2)%M
  cnt[X] += 1
  if (cnt[X] >= 2) or X == 0:
    break
  else:
    numbers.append(X) 

if X == 0:
  print(sum(numbers))
  exit()

repeat_before = numbers[0:numbers.index(X)]
repeat = numbers[numbers.index(X):X]

repeat_cnt,mod  = divmod(N-numbers.index(X),len(repeat))


print(sum(repeat_before) + sum(repeat) * repeat_cnt + sum(repeat[:mod]))



Fixed submission:
# 繰り返しの始まり + 繰り返し + 途中で終わった繰り返し分
N,X,M = map(int,input().split())
p = [0] * (M+2)
sum = [0] * (M+2)
p[X] = 1
sum[1] = X

repeat_start = 0
repeat_end = 0

for i in range(2,N+1):
  X = (X**2) % M
  if p[X] != 0:
    repeat_start = p[X]
    repeat_end = i
    break
  else:
    sum[i] = sum[i-1] + X
    p[X] += i

if repeat_start == 0:
  print(sum[N])
else:
  repeat_cnt,mod=divmod(N-repeat_start+1,repeat_end-repeat_start)
  print(repeat_cnt*(sum[repeat_end-1]-sum[repeat_start-1]) + sum[repeat_start+mod-1])

Buggy python submission (verdict: Runtime Error):
# 繰り返しの始まり + 繰り返し + 途中で終わった繰り返し分
N,X,M = map(int,input().split())

cnt = [False] + [1] + [0] * (M-1)
numbers = [X]
ans = 0
for n in range(N):
  X = pow((X % M),2)%M
  cnt[X] += 1
  if (cnt[X] >= 2) or X == 0:
    break
  else:
    numbers.append(X) 

if X == 0:
  print(sum(numbers))
  exit()

repeat_before = numbers[0:numbers.index(X)]
repeat = numbers[numbers.index(X):X]

repeat_cnt,mod  = divmod(N-numbers.index(X),len(repeat))


print(sum(repeat_before) + sum(repeat) * repeat_cnt + sum(repeat[:mod]))

Fixed submission:
# 繰り返しの始まり + 繰り返し + 途中で終わった繰り返し分
N,X,M = map(int,input().split())
p = [0] * (M+2)
sum = [0] * (M+2)
p[X] = 1
sum[1] = X

repeat_start = 0
repeat_end = 0

for i in range(2,N+1):
  X = (X**2) % M
  if p[X] != 0:
    repeat_start = p[X]
    repeat_end = i
    break
  else:
    sum[i] = sum[i-1] + X
    p[X] += i

if repeat_start == 0:
  print(sum[N])
else:
  repeat_cnt,mod=divmod(N-repeat_start+1,repeat_end-repeat_start)
  print(repeat_cnt*(sum[repeat_end-1]-sum[repeat_start-1]) + sum[repeat_start+mod-1])