output_description: Print the plural form of the given Taknese word.
  
  * * *
input_description: Input is given from Standard Input in the following format:
  
      
      
      S
attempt: import sys
  
  readline = sys.stdin.readline
  sys.setrecursionlimit(10**8)
  mod = 10**9 + 7
  # mod = 998244353
  INF = 10**18
  eps = 10**-7
  
  n, x, m = map(int, input().split())
  s = set()
  l = []
  flag = False
  cnt = 1
  ans = 0
  
  l.append(x)
  for i in range(m):
      if x not in s:
          s.add(x)
          x = (x**2) % m
          l.append(x)
      else:
          break
  
  h = len(l)
  cnt = 1
  num = 0
  for i in range(h):
      if l[i] == l[h - 1]:
          while l[i + cnt] != l[h - 1]:
              cnt += 1
          flag = True
          break
      else:
          num += 1
          ans += l[i]
  
  n -= num
  if num != 0:
      del l[h - 1]
      del l[0:num]
  
  if n >= len(l):
      mod = n % len(l)
      ans += sum(l) * (n // len(l))
      for i in range(1, mod):
          ans += l[i - 1]
      print(ans)
  else:
      for i in range(n):
          ans += l[i]
      print(ans)
  
problem_description: Statement
  
  In the Kingdom of AtCoder, people use a language called Taknese, which uses
  lowercase English letters.
  
  In Taknese, the plural form of a noun is spelled based on the following rules:
  
    * If a noun's singular form does not end with `s`, append `s` to the end of the singular form.
    * If a noun's singular form ends with `s`, append `es` to the end of the singular form.
  
  You are given the singular form S of a Taknese noun. Output its plural form.
samples: [{"input": "apple", "output": "apples\n    \n\n`apple` ends with `e`, so its plural form is `apples`.\n\n* * *"}, {"input": "bus", "output": "buses\n    \n\n`bus` ends with `s`, so its plural form is `buses`.\n\n* * *"}, {"input": "box", "output": "boxs"}]