output_description: Print \displaystyle{\sum_{i=1}^N A_i}.
  
  * * *
input_description: Input is given from Standard Input in the following format:
  
      
      
      N X M
attempt: n, x, m = map(int, input().split())
  cnt2 = set()
  cnt2.add(x)
  cnt3 = [x]
  cnt4 = 0
  cnt5 = 0
  cnt6 = x
  for i in range(1, m + 10):
      num2 = pow(cnt6, 2, m)
      if num2 in cnt2:
          for j in range(len(cnt3)):
              if cnt3[j] == num2:
                  cnt7 = j
                  break
          cnt10 = len(cnt3) - cnt7
          num4 = sum(cnt3[:cnt7])
          cnt4 = len(cnt3)
          break
      cnt2.add(num2)
      cnt3.append(num2)
      cnt6 = num2
      if num2 == 0:
          ans = sum(cnt3)
          cnt5 = 1
          break
  if cnt5 == 0:
      ans = num4
      n -= cnt7
      ans += (n // cnt10) * sum(cnt3[cnt7:])
      ans += sum(cnt3[cnt7 : (n % cnt10) + cnt7])
  if cnt4 >= n:
      ans = sum(cnt3[:n])
  print(ans)
  
problem_description: 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}.
samples: [{"input": "6 2 1001", "output": "1369\n    \n\nThe sequence A begins 2,4,16,256,471,620,\\ldots Therefore, the answer is\n2+4+16+256+471+620=1369.\n\n* * *"}, {"input": "1000 2 16", "output": "6\n    \n\nThe sequence A begins 2,4,0,0,\\ldots Therefore, the answer is 6.\n\n* * *"}, {"input": "10000000000 10 99959", "output": "492443256176507"}]