Problem:
Score : 500 points

Problem Statement

Let us denote by f(x, m) the remainder of the Euclidean division of x by m.

Let A be the sequence that is defined by the initial value A_1=X and the recurrence relation A_{n+1} = f(A_n^2, M).
Find \displaystyle{\sum_{i=1}^N A_i}.

Constraints

1 \leq N \leq 10^{10}

0 \leq X < M \leq 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N X M

Output

Print \displaystyle{\sum_{i=1}^N A_i}.

Sample Input 1

6 2 1001

Sample Output 1

1369

The sequence A begins 2,4,16,256,471,620,\ldots Therefore, the answer is 2+4+16+256+471+620=1369.

Sample Input 2

1000 2 16

Sample Output 2

6

The sequence A begins 2,4,0,0,\ldots Therefore, the answer is 6.

Sample Input 3

10000000000 10 99959

Sample Output 3

492443256176507

Solution in C++ (GCC 9.2.1) (judge verdict: Accepted)

#include <iostream>
#include <map>
#include <set>
#include <algorithm>
#include <vector>
#include <sstream>
#include <string>
#include <functional>
#include <queue>
#include <deque>
#include <stack>
#include <limits>
#include <unordered_map>
#include <unordered_set>
#include <cmath>
#include <fstream>
#include <iterator>
#include <random>
#include <chrono>


#define forr(i,start,count) for (int i = (start); i < (start)+(count); ++i)
#define set_map_includes(set, elt) (set.find((elt)) != set.end())
#define readint(i) int i; cin >> i
#define readll(i) ll i; cin >> i
#define readdouble(i) double i; cin >> i
#define readstring(s) string s; cin >> s

typedef long long ll;

using namespace std;

ll modd = 1000 * 1000 * 1000 + 7;

int main()   {

      ios_base::sync_with_stdio(false);

      cout.precision(17);
      mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
      uniform_int_distribution<int> rand_gen(0, modd);   // rand_gen(rng) gets the rand no


//    readint(test_cases);
    int test_cases = 1;
    forr(t, 1, test_cases) {
        readll(n);
        readll(x);
        readll(m);

        if (n<=100100) {
            ll ret = 0, a = x;
            forr(i,0,n) {
                ret += x;
                x *= x; x %= m;
            }
            cout << ret << endl;
            continue;
        }

        vector<int> which(m,-1);
        ll curr = x;
        int i = 1;
        while (true) {
            which[curr] = i;
            ++i;
            curr *= curr; curr %= m;
            if (which[curr]!=-1) {break;}
        }

        ll ret = 0;
        ll a = x;
        forr(j,0,which[curr]-1) {
            ret += a;
            a *= a; a %= m;
        }
        n -= (which[curr]-1);
        ll cycle_sum = 0;
        ll cycle_count = i - which[curr];
        forr(j,0,cycle_count) {
            cycle_sum += a;
            a *= a; a %= m;
        }
        ll no_cycles = n/cycle_count;
        ret += no_cycles * cycle_sum;
        n %= cycle_count;
        forr(j,0,n) {
            ret += a;
            a *= a; a %= m;
        }
        cout << ret << endl;

    }
    return 0;
}