Language: C++

#include <bits/stdc++.h>
using namespace std;

#define d(x) cerr << #x ":" << x << endl;
#define dd(x, y) cerr << "(" #x "," #y "):(" << x << "," << y << ")" << endl
#define rep(i, n) for (int i = (int)(0); i < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define dump(v)                  \
    cerr << #v ":[ ";            \
    for (auto macro_vi : v) {    \
        cerr << macro_vi << " "; \
    }                            \
    cerr << "]" << endl;
#define ddump(v)                           \
    cerr << #v ":" << endl;                \
    for (auto macro_row : v) {             \
        cerr << "[";                       \
        for (auto macro__vi : macro_row) { \
            cerr << macro__vi << " ";      \
        }                                  \
        cerr << "]" << endl;               \
    }
using lint       = long long;
const int INF    = 1e9;
const lint LINF  = 1e18;
const double EPS = 1e-10;

int mod = 1000000007;
struct mint {
    long long x;
    mint(long long x = 0) : x((x % mod + mod) % mod) {}
    mint& operator+=(const mint a) {
        if ((x += a.x) >= mod) x -= mod;
        return *this;
    }
    mint& operator-=(const mint a) {
        if ((x += mod - a.x) >= mod) x -= mod;
        return *this;
    }
    mint& operator*=(const mint a) {
        (x *= a.x) %= mod;
        return *this;
    }
    mint operator+(const mint a) const {
        mint res(*this);
        return res += a;
    }
    mint operator-(const mint a) const {
        mint res(*this);
        return res -= a;
    }
    mint operator*(const mint a) const {
        mint res(*this);
        return res *= a;
    }
    mint pow(long long t) const {
        if (!t) return 1;
        mint a = pow(t >> 1);
        a *= a;
        if (t & 1) a *= *this;
        return a;
    }

    // for prime mod
    mint inv() const { return pow(mod - 2); }
    mint& operator/=(const mint a) { return (*this) *= a.inv(); }
    mint operator/(const mint a) const {
        mint res(*this);
        return res /= a;
    }
};

int main() {
    lint N, X, M;
    cin >> N >> X >> mod;

    const int M = 10000000;
    vector<lint> A(M, 0);
    A[1] = X;
    map<lint, lint> mp;

    lint start, end;
    for (int i = 2; i < M; i++) {
        A[i] = ((A[i - 1] % mod) * (A[i - 1] % mod)) % mod;
        if (mp.count(A[i]) == 0) {
            mp[A[i]] = i;
        } else {
            end   = i;
            start = mp[A[i]];
            break;
        }
    }
    lint length = end - start;

    N++;

    lint ans  = 0;
    lint sum2 = 0;
    lint sum3 = 0;

    for (int i = 0; i < start; i++) {
        ans += A[i];
    }
    N -= start;

    // d(ans);
    for (int i = start; i < end; i++) {
        sum2 += A[i];
    }
    ans += int(N / length) * sum2;

    // d(ans);
    lint r = (N % length);
    for (int i = start; i < start + r; i++) {
        ans += A[i];
    }
    // d(ans);

    cout << ans << endl;

    return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:77:15: error: conflicting declaration 'const int M'
   77 |     const int M = 10000000;
      |               ^
a.cc:74:16: note: previous declaration as 'lint M'
   74 |     lint N, X, M;
      |                ^