### BUGGY CODE ###

# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from fractions import Fraction
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations, accumulate
from operator import add, mul, sub, itemgetter, attrgetter


import sys
# sys.setrecursionlimit(10**6)
# readline = sys.stdin.buffer.readline
readline = sys.stdin.readline

INF = 1 << 60


def read_int():
    return int(readline())


def read_int_n():
    return list(map(int, readline().split()))


def read_float():
    return float(readline())


def read_float_n():
    return list(map(float, readline().split()))


def read_str():
    return readline().strip()


def read_str_n():
    return readline().strip().split()


def ep(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.perf_counter()
        ret = f(*args, **kwargs)
        e = time.perf_counter()

        ep(e - s, 'sec')
        return ret

    return wrap


class Doubling():
    def __init__(self, a0, M):
        """
        a0 is an array-like object which contains ai, 0 <= i < N.
        ai is the next value of i.
        """
        N = len(a0)
        self.N = N
        self.nt = [[None] * N for i in range(M.bit_length()+1)]
        for i, a in enumerate(a0):
            self.nt[0][i] = a

        for i in range(1, len(self.nt)):
            for j in range(N):
                if self.nt[i-1][j] is None:
                    self.nt[i][j] = None
                else:
                    self.nt[i][j] = self.nt[i-1][self.nt[i-1][j]]

    def apply(self, i, n):
        """
        Apply n times from i
        """
        j = i
        for k in range(n.bit_length()):
            m = 1 << k
            if m & n:
                j = self.nt[k][j]
            if j is None:
                break
        return j


@mt
def slv(N, X, M):
    ans = 0
    x = X
    x %= M
    if math.gcd(X, M) != 1:
        for _ in range(N):
            ans += x
            x *= x
            x %= M
            if x == 0:
                break
        return ans
    else:
        x = X
        s = [X]
        u = set()
        while True:
            x *= x
            x %= M
            s.append(x)
            if x in u:
                break
            u.add(x)

        i = s.index(s[-1])
        if i + 1 >= N:
            return sum(s[:N]) % M

        N -= i
        ans += sum(s[:i])

        s = s[i:]
        s.pop()
        ss = sum(s)
        ans += (N // len(s)) * ss
        ans += sum(s[:N%len(s)])
        return ans


def main():
    N, X, M = read_int_n()
    print(slv(N, X, M))


if __name__ == '__main__':
    main()


### FIXED CODE ###

# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from fractions import Fraction
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations, accumulate
from operator import add, mul, sub, itemgetter, attrgetter


import sys
# sys.setrecursionlimit(10**6)
# readline = sys.stdin.buffer.readline
readline = sys.stdin.readline

INF = 1 << 60


def read_int():
    return int(readline())


def read_int_n():
    return list(map(int, readline().split()))


def read_float():
    return float(readline())


def read_float_n():
    return list(map(float, readline().split()))


def read_str():
    return readline().strip()


def read_str_n():
    return readline().strip().split()


def ep(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.perf_counter()
        ret = f(*args, **kwargs)
        e = time.perf_counter()

        ep(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, X, M):
    ans = 0
    x = X
    x %= M
    # if math.gcd(X, M) != 1:
    if False:
        for _ in range(N):
            ans += x
            x *= x
            x %= M
            if x == 0:
                break
        return ans
    else:
        x = X
        s = [X]
        u = set()
        while True:
            x *= x
            x %= M
            s.append(x)
            if x in u:
                break
            u.add(x)

        i = s.index(s[-1])
        if i + 1 >= N:
            return sum(s[:N]) % M

        N -= i
        ans += sum(s[:i])

        s = s[i:]
        s.pop()
        ss = sum(s)
        ans += (N // len(s)) * ss
        ans += sum(s[:N%len(s)])
        return ans


def main():
    N, X, M = read_int_n()
    print(slv(N, X, M))

    # N = 10**10
    # X = random.randint(1, 10**5)
    # M = random.randint(1, 10**5)
    # N, X, M = 10000000000, 82162, 79872
    # print(N, X, M)
    # print(slv(N, X, M))

if __name__ == '__main__':
    main()