Language: C++

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<string> vs;
using pll = pair<ll, ll>;
using vl = vector<ll>;
using vll = vector<vl>;
using vpll = vector<pll>;

struct Edge {
	ll to;
    ll cost;
};

struct edge {
    ll from;
    ll to;
    ll cost;
};

using Graph = vector<vector<Edge>>;
using graph = vector<vector<edge>>;

#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define mp make_pair
#define pb push_back
/***** define constant start *******/
const int MAX = 10000000;
const ll MOD = 998244353;
const double PI = 3.1415926535897932;
const ll INF = 1LL << 60;
string abc = "abcdefghijklmnopqrstuvwxyz";
string ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
/****** define constant end ********/

template <class T>
inline bool chmin(T &a, T b){
    if (a > b){
        a = b;
        return true;
    }
    return false;
}

template <class T>
inline bool chmax(T &a, T b){
    if (a < b){
        a = b;
        return true;
    }
    return false;
}

void debug_out() { std::cout << std::endl; }

template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
    cout << H << " ";
    debug_out(T...);
}
#ifdef LOCAL
#define debug(...) cout << "debug: "; debug_out(__VA_ARGS__)
#else
#define debug(...)
#endif

#define rep(i, a, n) for (int i = (int)(a); i < (int)(n); i++)
#define rrep(i, a, n) for (int i = ((int)(n-1)); i >= (int)(a); i--)
#define Rep(i, a, n) for (long long i = (long long)(a); i< (long long)(n); i++)
#define RRep(i, a, n) for (long long i = ((long long)(n-1ll)); i>=(long long)(a); i--)
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()

template <typename T>
std::ostream& operator<<(std::ostream& os, std::vector<T> vec) {
  for (std::size_t i = 0; i < vec.size(); i++)os << vec[i] << (i + 1 == vec.size() ? "" : " ");
  return os;
}

// 負の数にも対応した % 演算
long long mod(long long val, long long m) {
    long long res = val % m;
    if (res < 0)
        res += m;
    return res;
}
// a^n mod を計算する
long long modpow(long long a, long long n, long long mod) {
    long long res = 1;
    while (n > 0) {
        if (n & 1)
        res = res * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}
// mod. m での a の逆元 a^{-1} を計算する
long long modinv(long long a, long long m) {
    long long b = m, u = 1, v = 0;
    while (b) {
        long long t = a / b;
	    a -= t * b;
        swap(a, b);
	    u -= t * v;
        swap(u, v);
    }
    u %= m;
    if (u < 0)
        u += m;
    return u;
}
/****** define variable start ******/
ll n, x, m;
/******* define variable end *******/
int solve() {
	cin >> n >> x >> m;
	ll loop_start = -1;
	ll before_sum = x;
	ll tmp = x;
	map<ll, bool> ma;
	ma[x] = true;
	REP(i, n){
		if(ma[((tmp * tmp) % m)]){
			tmp = tmp * tmp % m;
			loop_start = i;
			break;
		}
		before_sum += tmp * tmp % m;
		tmp = tmp * tmp % m;
		ma[tmp] = true;
	}
	ll loop_num = -1;
	ll loop_sum = tmp;
	map<ll, bool> ma2;
	ma2[tmp] = true;
	REP(i, n){
		if(ma2[((tmp * tmp) % m)]){
			loop_num = i;
			tmp = tmp * tmp % m;
			break;
		}
		loop_sum += tmp * tmp % m;
		tmp = tmp * tmp % m;
		ma2[tmp] = true;
	}
	loop_start -= loop_num;
	before_sum -= loop_sum;
	loop_num++;

	debug(loop_start, before_sum, loop_num, loop_sum, tmp);

	if(loop_start > n || loop_start < 0){
		ll sum = x;
		ll tmp2 = x;
		REP(i, n-1){
			sum += tmp2 * tmp2 % m;
			tmp2 = tmp2 * tmp2 % m;
		}
		cout << sum << endl;
		return 0;
	}
	ll ans = before_sum + (n-loop_start) / loop_num * loop_sum;
	REP(i, max(0, n - (loop_start + (n-loop_start) / loop_num * loop_num)){
		ans += tmp;
		tmp = tmp * tmp % m;
	}
	cout << ans << endl;
    return 0;
}

int main() {
	// clock_t start = clock();
	cout << std::fixed << std::setprecision(10);
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int end_solve = solve();
	/*
	clock_t end = clock();
	const double time = static_cast<double>(end - start) / CLOCKS_PER_SEC *
	1000.0; printf("time %lf[ms]\n", time);
	*/

	return end_solve;
}


Compiler output:
a.cc:188:2: error: unterminated argument list invoking macro "REP"
  188 | }
      |  ^
a.cc: In function 'int solve()':
a.cc:167:9: error: 'REP' was not declared in this scope
  167 |         REP(i, max(0, n - (loop_start + (n-loop_start) / loop_num * loop_num)){
      |         ^~~
a.cc:167:12: error: expected '}' at end of input
  167 |         REP(i, max(0, n - (loop_start + (n-loop_start) / loop_num * loop_num)){
      |            ^
a.cc:119:13: note: to match this '{'
  119 | int solve() {
      |             ^
a.cc:167:12: warning: control reaches end of non-void function [-Wreturn-type]
  167 |         REP(i, max(0, n - (loop_start + (n-loop_start) / loop_num * loop_num)){
      |            ^