Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
import sys

n,x,m = (int(i) for i in input().split())
l = np.array([x])
ans = x
memo_array = [-1]*(m+1)

for i in range(1,n):
    ans = ans**2%m
    idx = memo_array[ans]
    if idx != -1:
        loop_num, mod = divmod((n-idx),(i-idx))
        print(np.sum(l[:idx]) + np.sum(l[idx:])*(loop_num)+np.sum(l[idx:idx+mod]))
        sys.exit()
    l = np.append(l,ans)
    memo_array[ans]=i
print(np.sum(l))

Fixed submission:
import sys

n,x,m = (int(i) for i in input().split())
l = [x]
ans = x
memo_array = [-1]*(m+1)

for i in range(1,n):
    ans = ans**2%m
    idx = memo_array[ans]
    if idx != -1:
        loop_num, mod = divmod((n-idx),(i-idx))
        print(sum(l[:idx]) + sum(l[idx:])*(loop_num)+sum(l[idx:idx+mod]))
        sys.exit()
    l.append(ans)
    memo_array[ans]=i
print(sum(l))

Buggy python submission (verdict: Runtime Error):
import numpy as np
import sys

n,x,m = (int(i) for i in input().split())
l = np.array([x])
ans = x
memo_array = [-1]*(m+1)
for i in range(1,n):
    ans = ans**2%m
    idx = memo_array[ans]
    if idx != -1:
        loop_num, mod = divmod((n-idx),(i-idx))
        print(np.sum(l[:loop_start]) + np.sum(l[loop_start:])*(loop_num)+np.sum(l[loop_start:loop_start+mod]))
        sys.exit()
    l = np.append(l,ans)
    memo_array[ans]=i
print(np.sum(l))

Fixed submission:
import sys

n,x,m = (int(i) for i in input().split())
l = [x]
ans = x
memo_array = [-1]*(m+1)

for i in range(1,n):
    ans = ans**2%m
    idx = memo_array[ans]
    if idx != -1:
        loop_num, mod = divmod((n-idx),(i-idx))
        print(sum(l[:idx]) + sum(l[idx:])*(loop_num)+sum(l[idx:idx+mod]))
        sys.exit()
    l.append(ans)
    memo_array[ans]=i
print(sum(l))

Buggy python submission (verdict: Runtime Error):
import numpy as np
import sys

n,x,m = (int(i) for i in input().split())
l = np.array([x])
ans = x
memo_array = [False]*(m+1)
for i in range(1,n):
    ans = ans**2%m
    if memo_array[ans]:
        loop_start = int(np.where(l==ans)[0])
        loop_num, mod = divmod((n-loop_start),(i-loop_start))
        print(np.sum(l[:loop_start]) + np.sum(l[loop_start:])*(loop_num)+np.sum(l[loop_start:loop_start+mod]))
        sys.exit()
    l = np.append(l,ans)
    memo_array[ans]=True
print(np.sum(l))

Fixed submission:
import sys

n,x,m = (int(i) for i in input().split())
l = [x]
ans = x
memo_array = [-1]*(m+1)

for i in range(1,n):
    ans = ans**2%m
    idx = memo_array[ans]
    if idx != -1:
        loop_num, mod = divmod((n-idx),(i-idx))
        print(sum(l[:idx]) + sum(l[idx:])*(loop_num)+sum(l[idx:idx+mod]))
        sys.exit()
    l.append(ans)
    memo_array[ans]=i
print(sum(l))

Buggy python submission (verdict: Runtime Error):
import numpy as np
import sys

n,x,m = (int(i) for i in input().split())
l = np.array([x])
ans = x
memo_array = [False]*m
for i in range(1,n):
    ans = ans**2%m
    if memo_array[ans]:
        loop_start = int(np.where(l==ans)[0])
        loop_num, mod = divmod((n-loop_start),(i-loop_start))
        print(np.sum(l[:loop_start]) + np.sum(l[loop_start:])*(loop_num)+np.sum(l[loop_start:loop_start+mod]))
        sys.exit()
    l = np.append(l,ans)
    memo_array[ans]=True
print(np.sum(l))

Fixed submission:
import sys

n,x,m = (int(i) for i in input().split())
l = [x]
ans = x
memo_array = [-1]*(m+1)

for i in range(1,n):
    ans = ans**2%m
    idx = memo_array[ans]
    if idx != -1:
        loop_num, mod = divmod((n-idx),(i-idx))
        print(sum(l[:idx]) + sum(l[idx:])*(loop_num)+sum(l[idx:idx+mod]))
        sys.exit()
    l.append(ans)
    memo_array[ans]=i
print(sum(l))

Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
import sys

n,x,m = (int(i) for i in input().split())
l = np.array([x])
ans = x
for i in range(n-1):
    ans = ans**2%m
    if ans in l:
        loop_start = int(np.where(l==ans)[0])
        loop_num, mod = divmod((n-loop_start),(i-loop_start+1))
        print(np.sum(l[:loop_start]) + np.sum(l[loop_start:])*(loop_num)+np.sum(l[loop_start:loop_start+mod]))
        sys.exit()
    l = np.append(l,ans)
print(np.sum(l))

Fixed submission:
import sys

n,x,m = (int(i) for i in input().split())
l = [x]
ans = x
memo_array = [-1]*(m+1)

for i in range(1,n):
    ans = ans**2%m
    idx = memo_array[ans]
    if idx != -1:
        loop_num, mod = divmod((n-idx),(i-idx))
        print(sum(l[:idx]) + sum(l[idx:])*(loop_num)+sum(l[idx:idx+mod]))
        sys.exit()
    l.append(ans)
    memo_array[ans]=i
print(sum(l))

Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
import sys

n,x,m = (int(i) for i in input().split())
l = np.array([x])
for i in range(n-1):
    ans = (l[i])**2%m
    if ans in l:
        loop_start = int(np.where(l==ans)[0])
        loop_num, mod = divmod((n-loop_start),(i-loop_start+1))
        print(np.sum(l[:loop_start]) + np.sum(l[loop_start:])*(loop_num)+np.sum(l[loop_start:loop_start+mod]))
        sys.exit()
    l = np.append(l,ans)
print(np.sum(l))

Fixed submission:
import sys

n,x,m = (int(i) for i in input().split())
l = [x]
ans = x
memo_array = [-1]*(m+1)

for i in range(1,n):
    ans = ans**2%m
    idx = memo_array[ans]
    if idx != -1:
        loop_num, mod = divmod((n-idx),(i-idx))
        print(sum(l[:idx]) + sum(l[idx:])*(loop_num)+sum(l[idx:idx+mod]))
        sys.exit()
    l.append(ans)
    memo_array[ans]=i
print(sum(l))

Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
import sys

n,x,m = (int(i) for i in input().split())
l = np.array([x])
for i in range(n-1):
    ans = (l[i])**2%m
    if ans in l:
        b = int(np.where(l==ans)[0])
        print(np.sum(l[:b]) + np.sum(l[b:])*((n-b)//(i-b+1))+np.sum(l[b:b+(n-b)%(i-b+1)]))
        sys.exit()
    else:
        l = np.append(l,ans)
print(np.sum(l))

Fixed submission:
import sys

n,x,m = (int(i) for i in input().split())
l = [x]
ans = x
memo_array = [-1]*(m+1)

for i in range(1,n):
    ans = ans**2%m
    idx = memo_array[ans]
    if idx != -1:
        loop_num, mod = divmod((n-idx),(i-idx))
        print(sum(l[:idx]) + sum(l[idx:])*(loop_num)+sum(l[idx:idx+mod]))
        sys.exit()
    l.append(ans)
    memo_array[ans]=i
print(sum(l))