Buggy python submission (verdict: Time Limit Exceeded):
N, X, M = map(int, input().split())
A = [X]
a = X
s = X
r = 0
r0 = 0
l = 0
amari = 0

for i in range(M+1):
  a = a ** 2 % M
  if a in A:
    r0 = A.index(a)
    l = len(A[r0:])
    r = (N - i - 1) // l
    amari = (N - i - 1) % l
    s = s + sum(A[r0:])*r + sum(A[r0:r0+amari])
    break
  A.append(a)
  s += A[-1]

print(s)

Fixed submission:
N, X, M = map(int, input().split())
A = [X]
A_dict = {X}
a = X
s = X
r = 0
r0 = 0
l = 0
amari = 0

for i in range(M+1):
  a = a ** 2 % M
  if a in A_dict:
    r0 = A.index(a)
    l = len(A[r0:])
    r = (N - i - 1) // l
    amari = (N - i - 1) % l
    s = s + sum(A[r0:])*r + sum(A[r0:r0+amari])
    break
  A.append(a)
  A_dict.add(a)
  s += A[-1]

print(s)

Buggy python submission (verdict: Time Limit Exceeded):
N, X, M = map(int, input().split())
A = [X]
a = X
s = X
r = 0
r0 = 0
l = 0
amari = 0

for i in range(M+1):
  a = a ** 2 % M
  if A.count(a) >= 1:
    r0 = A.index(a)
    l = len(A[r0:])
    r = (N - i - 1) // l
    amari = (N - i - 1) % l
    s = s + sum(A[r0:])*r + sum(A[r0:r0+amari])
    break
  A.append(a)
  s += A[-1]

print(s)

Fixed submission:
N, X, M = map(int, input().split())
A = [X]
A_dict = {X}
a = X
s = X
r = 0
r0 = 0
l = 0
amari = 0

for i in range(M+1):
  a = a ** 2 % M
  if a in A_dict:
    r0 = A.index(a)
    l = len(A[r0:])
    r = (N - i - 1) // l
    amari = (N - i - 1) % l
    s = s + sum(A[r0:])*r + sum(A[r0:r0+amari])
    break
  A.append(a)
  A_dict.add(a)
  s += A[-1]

print(s)

Buggy python submission (verdict: Time Limit Exceeded):
N, X, M = map(int, input().split())
A = [X]
a = X
s = X
r = 0
r0 = 0
l = 0
amari = 0

for i in range(M+1):
  a = a ** 2 % M
  if A.count(a) >= 1:
    r0 = A.index(a)
    l = len(A[r0:])
    r = (N - i - 1) // l
    amari = (N - i - 1) % l
    s = s + sum(A[r0:])*r + sum(A[r0:r0+amari])
    break
  A.append(a)
  s += A[-1]

print(s)

Fixed submission:
N, X, M = map(int, input().split())
A = [X]
A_dict = {X}
a = X
s = X
r = 0
r0 = 0
l = 0
amari = 0

for i in range(M+1):
  a = a ** 2 % M
  if a in A_dict:
    r0 = A.index(a)
    l = len(A[r0:])
    r = (N - i - 1) // l
    amari = (N - i - 1) % l
    s = s + sum(A[r0:])*r + sum(A[r0:r0+amari])
    break
  A.append(a)
  A_dict.add(a)
  s += A[-1]

print(s)

Buggy python submission (verdict: Time Limit Exceeded):
N, X, M = map(int, input().split())
A = [X]
a = X
s = X
r = 0
r0 = 0
l = 0
amari = 0

for i in range(M):
  a = a ** 2 % M
  if A.count(a) >= 1:
    if a == 0:
      s = sum(A)
      break
    else:
      r0 = A.index(a)
      l = len(A[r0:])
      r = (N - i - 1) // l
      amari = (N - i - 1) % l
      s = s + sum(A[r0:])*r + sum(A[r0:r0+amari])
      break
  A.append(a)
  s += A[-1]

print(s)

Fixed submission:
N, X, M = map(int, input().split())
A = [X]
A_dict = {X}
a = X
s = X
r = 0
r0 = 0
l = 0
amari = 0

for i in range(M+1):
  a = a ** 2 % M
  if a in A_dict:
    r0 = A.index(a)
    l = len(A[r0:])
    r = (N - i - 1) // l
    amari = (N - i - 1) % l
    s = s + sum(A[r0:])*r + sum(A[r0:r0+amari])
    break
  A.append(a)
  A_dict.add(a)
  s += A[-1]

print(s)

Buggy python submission (verdict: Time Limit Exceeded):
N, X, M = map(int, input().split())
A = [X]
a = X
s = X
r = 0
r0 = 0
l = 0
amari = 0

for i in range(N-1):
  a = a ** 2 % M
  if A.count(a) >= 1:
    if a == 0:
      s = sum(A)
      break
    else:
      r0 = A.index(a)
      l = len(A[r0:])
      r = (N - i - 1) // l
      amari = (N - i - 1) % l
      s = s + sum(A[r0:])*r + sum(A[r0:r0+amari])
      break
  A.append(a)
  s += A[-1]

print(s)

Fixed submission:
N, X, M = map(int, input().split())
A = [X]
A_dict = {X}
a = X
s = X
r = 0
r0 = 0
l = 0
amari = 0

for i in range(M+1):
  a = a ** 2 % M
  if a in A_dict:
    r0 = A.index(a)
    l = len(A[r0:])
    r = (N - i - 1) // l
    amari = (N - i - 1) % l
    s = s + sum(A[r0:])*r + sum(A[r0:r0+amari])
    break
  A.append(a)
  A_dict.add(a)
  s += A[-1]

print(s)

Buggy python submission (verdict: Time Limit Exceeded):
N, X, M = map(int, input().split())
A = []
a = X
s = X
r = 0
r0 = 0
l = 0
amari = 0

A.append(X)
if N > 1:
  for i in range(N-1):
    a = a ** 2 % M
    if A.count(a) >= 1:
      if a == 0:
        s = sum(A)
        break
      else:
        r0 = A.index(a)
        l = len(A[r0:])
        r = (N - i - 1) // l
        amari = (N - i - 1) % l
        s = s + sum(A[r0:])*r + sum(A[r0:r0+amari])
        break
    else:
      A.append(a)
      s += A[-1]
  
print(s)

Fixed submission:
N, X, M = map(int, input().split())
A = [X]
A_dict = {X}
a = X
s = X
r = 0
r0 = 0
l = 0
amari = 0

for i in range(M+1):
  a = a ** 2 % M
  if a in A_dict:
    r0 = A.index(a)
    l = len(A[r0:])
    r = (N - i - 1) // l
    amari = (N - i - 1) % l
    s = s + sum(A[r0:])*r + sum(A[r0:r0+amari])
    break
  A.append(a)
  A_dict.add(a)
  s += A[-1]

print(s)