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# (.NET Core 3.1.201) (judge verdict: Runtime Error)

using System;
using System.Linq;
using System.Collections.Generic;
using System.Numerics;

namespace ABC179
{
    class Program
    {
        static void Main(string[] args)
        {
            var NXM = Console.ReadLine().Split().Select(long.Parse).ToArray();
            var N = NXM[0]; var X = NXM[1]; var M = NXM[2];

            // make array
            var array = new long[M];
            var a_ = X;
            array[0] = a_;
            for (int i = 1; i < M; i++)
            {
                a_ = (a_ * a_) % M;
                array[i] = a_;
            }

            //Console.WriteLine(string.Join(" ", array));

            var t = Program.cycle_finding(array);
            var s = t.Item1; var e = t.Item2; var len = t.Item3;
            //Console.WriteLine("{0}, {1}", s, e);

            N -= s;
            var ans = 0L;
            for (int i = 0; i < s; i++)
            {
                ans += array[i];
            }

            var a = N / len;
            var b = N % len;

            var sum = 0L;
            for (long i = s; i <= e; i++)
            {
                sum += array[i];
            }

            ans += a * sum;

            for (int i = 0; i < b; i++)
            {
                ans += array[s + i];
            }

            Console.WriteLine(ans);


        }

        // 循環開始・終了位置
        static Tuple<long, long, long> cycle_finding(long[] a)
        {
            var x_length = new long[a.Length].Select(x => x = -1).ToArray();
            var s = 0L;
            var e = 0L;

            var len = 0;
            var index = 0;
            for (int i = 0; x_length[a[i]] == -1; i++)
            {
                x_length[a[i]] = len;
                len++;
                index = i;
            }
            var cycle_length = len - x_length[a[index + 1]];

            s = x_length[a[index + 1]];
            e = x_length[a[index]];
            //Console.WriteLine("{0}, {1}", s, e);
            //Console.WriteLine(string.Join(" ", x_length));

            var tuple = Tuple.Create(s, e, cycle_length);
            return tuple;
        }

    }
}