Buggy python submission (verdict: Runtime Error):
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 submission:
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 python submission (verdict: Runtime Error):
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 submission:
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 python submission (verdict: Runtime Error):
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 submission:
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 python submission (verdict: Runtime Error):
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)
S = C[i] - C[Xd[x]]
# print(S)
loop_len = i - Xd[x]
# print(loop_len)
if loop_len > 0:
    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 submission:
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 python submission (verdict: Runtime Error):
from collections import defaultdict

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

if M == 0:
    print(X)
    exit()

ans = X
x = X

m = []
for i in range(0, M+1):
    m.append((i ** 2) % M)

C = [x]
Xd = defaultdict(lambda: -1)
Xd[x] = 0
Xl = [x]

for i in range(1, N):
    
    x = m[x]

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

# print(Xl)
# print(C)
# print(Xd[x], i)
S = C[i-1] - C[Xd[x]-1]
loop_len = i - Xd[x]
if loop_len > 0:
    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-1] - C[Xd[x]-1]

# print(S)

print(ans)

Fixed submission:
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 python submission (verdict: Runtime Error):
from collections import defaultdict

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

ans = X
x = X

m = []
for i in range(0, M+1):
    m.append((i ** 2) % M)

C = [x]
Xd = defaultdict(lambda: -1)
Xd[x] = 0
Xl = [x]

for i in range(1, N):
    
    x = m[x]

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

# print(Xl)
# print(C)
# print(Xd[x], i)
S = C[i-1] - C[Xd[x]-1]
loop_len = i - Xd[x]
if loop_len > 0:
    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-1] - C[Xd[x]-1]

# print(S)

print(ans)

Fixed submission:
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)