Problem p02550 (python)

Fixed submission:
def readInt():
    return int(input())
def readList():
    return list(map(int,input().split()))
def readMap():
	return map(int,input().split())
def readStr():
    return input()
inf=float('inf')
mod = 10**9+7
import math
def solve(N,X,M):
    seen=set()
    order_seen=[]
    while X not in seen:
        seen.add(X)
        order_seen.append(X)
        X=f(X,M)
    pos=order_seen.index(X)  # Find position of when the cycle begins
    if pos>N:  # If position is greater than N, then we know we are not going to include any values from the cycle.
        return sum(order_seen[:N])
    terms_before_cycle=order_seen[:pos]
    sum_terms_before_cycle=sum(terms_before_cycle)
    ans=sum_terms_before_cycle
    terms_in_cycle=order_seen[pos:]
    sum_cycle=sum(terms_in_cycle)
    length_cycle=len(terms_in_cycle)
    number_remaining_terms=N-pos
    mult_factor=(number_remaining_terms)//length_cycle
    ans+=(sum_cycle*mult_factor)
    numb_remain=(number_remaining_terms)%length_cycle
    ans+=sum(terms_in_cycle[:numb_remain])
    return ans

# The function for the recurrence relation.  A_n+1=A_n^2%M
def f(A,M):
    return pow(A,2,M)

N,X,M=readMap()
print(solve(N,X,M))

Buggy submission (verdict: Runtime Error):
def readInt():
    return int(input())
def readList():
    return list(map(int,input().split()))
def readMap():
	return map(int,input().split())
def readStr():
    return input()
inf=float('inf')
mod = 10**9+7
import math
from collections import OrderedDict
def solve(N,X,M):
    A=X
    ans=A
    seen=set()
    order_seen=[]
    for i in range(2,N+1):
        A=f(A,M)
        if i!=2:
            if A not in seen:
                seen.add(A)
                order_seen.append(A)
            else:  # If A has already been seen we are just going to repeat so break out of the loop.
                i-= 1 # Correcting factor for fact that when we break out of the loop we go one above with i
                break
        if A==0:   # If A=0 it will repeat forever.
            return ans
        ans+=A
    n=len(order_seen)
    N-=i
    mult_factor=N//n
    total_sum_seen=sum(order_seen)
    ans+=(total_sum_seen*mult_factor)
    number_remaining=N%n
    for i in range(number_remaining):
        ans+=order_seen[i]
    return ans

# The function for the recurrence relation.  A_n+1=A_n^2%M
def f(A,M):
    return A**2%M

N,X,M=readMap()
print(solve(N,X,M))

Buggy submission (verdict: Runtime Error):
def readInt():
    return int(input())
def readList():
    return list(map(int,input().split()))
def readMap():
	return map(int,input().split())
def readStr():
    return input()
inf=float('inf')
mod = 10**9+7
import math
from collections import OrderedDict
def solve(N,X,M):
    A=X
    ans=A
    seen=set()
    order_seen=[]
    for i in range(2,N+1):
        A=f(A,M)
        if i!=2:
            if A not in seen:
                seen.add(A)
                order_seen.append(A)
            else:  # If A has already been seen we are just going to repeat so break out of the loop.
                break
        if A==0:   # If A=0 it will repeat forever.
            return ans
        ans+=A
    n=len(order_seen)
    i-=1 # Correcting factor for fact that when we break out of the loop we go one above with i
    N-=i
    mult_factor=N//n
    total_sum_seen=sum(order_seen)
    ans+=(total_sum_seen*mult_factor)
    number_remaining=N%n
    for i in range(number_remaining):
        ans+=order_seen[i]
    return ans

# The function for the recurrence relation.  A_n+1=A_n^2%M
def f(A,M):
    return A**2%M

N,X,M=readMap()
print(solve(N,X,M))