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: Wrong Answer)

#include<bits/stdc++.h> 
#include <ext/pb_ds/assoc_container.hpp> 
#include <ext/pb_ds/tree_policy.hpp> 
using namespace std;
using namespace __gnu_pbds;  
#define ordered_set tree<ll, null_type , less<ll> , rb_tree_tag , tree_order_statistics_Node_update> 
#define ll long long
#define ull unsigned long long
#define pb push_back
#define inf 1e18
#define mk make_pair
#define ld long double
#define mod 1000000007
#define fi first
#define se second
#define fastIO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define test ll t; cin>>t; while(t--)
#define setbits __builtin_popcount
#define endl '\n'
#define LOCAL
#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i));}
sim, class b dor(pair < b, c > d) {
    ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
    *this << "[";
    for (auto it = d.b; it != d.e; ++it)
        *this << ", " + 2 * (it == d.b) << *it;
    ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
int main()
{
    fastIO;
    ull n,x,m;
    cin>>n>>x>>m;
    vector<ll>dp(1000001);
    if(x==0)
    {
        cout<<0<<endl;
        return 0;
    }
    dp[0]=x;
    dp[1]=(x*x)%m;
    map<ll,ll>mp;
    ull val,val1,val2,ans=0;
    for(int i=2;i<1000001;i++)
    {
        dp[i]=(dp[i-1]*dp[i-1])%m;
        if(mp.find(dp[i])==mp.end())
        {
            mp[dp[i]]=i;
        }
        else
        {
            break;
        }
    }   
    if(n==1)
    {
        cout<<dp[0]<<endl;
    }   
    else
    if(n==2)
    {
        cout<<dp[1]+dp[0]<<endl;
    }
    else
    {
        ull val3=0;
        val1=mp.size();
        vector<ull>pre(val1+1);
        val=1;
        for(int i=2;i<val1+2;i++)
        {
            pre[val]=pre[val-1]+dp[i];
            val++;
        }
        //debug()<<pre;
        ans=dp[0]+dp[1];
        n-=2;
        val=n/val1;
        val2=n%val1;
        ans+=pre[val1]*val;
        ans+=pre[val2];
        cout<<ans<<endl;
    }
    //debug()<<dp;
}