Problem p02550 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>
#include <string.h>
#pragma GCC optimize("O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define rep(i, N) for (long i = 0; i < (long)N; i++)
static inline void PUT(char c) {
  static char buf[1 << 15], *ptr = buf;
  if (ptr == buf + strlen(buf) || c == 0) {
    fwrite(buf, 1, ptr - buf, stdout), ptr = buf;
  }
  *ptr++ = c;
}
static inline long IN(void) {
  long x = 0, f = 0, c = getchar();
  while (c < 48 || c > 57) {
    f ^= c == 45, c = getchar();
  }
  while (c > 47 && c < 58) {
    x = x * 10 + c - 48, c = getchar();
  }
  return f ? -x : x;
}
static inline void OUT(long a) {
  if (a < 0)
    PUT('-'), a = -a;
  int d[40], i = 0;
  do {
    d[i++] = a % 10;
  } while (a /= 10);
  while (i--) {
    PUT(d[i] + 48);
  }
  PUT('\\n');
}
int MOD;
static inline int MPow(long a, long b) {
  return b ? 1l * MPow(1l * a * a % MOD, b >> 1) * (b & 1 ? a : 1) % MOD : 1;
}
int main() {
  long N = IN(), calc = 0;
  int X = IN(), vis[100001];
  MOD = IN();
  int val[100001];
  if (N <= MOD) {
    rep(i, N) {
      calc += X;
      X = MPow(X, 2);
    }
    return OUT(calc), 0;
  } else {
    rep(i, MOD + 1) {
      val[i + 1] = X;
      if (vis[X]) {
        rep(j, vis[X]) { calc += val[j + 1]; }
        int k = vis[X] + 1;
        while (k <= i + 1) {
          calc += 1l * (N - vis[X] + i + 1 - k) / (i + 1 - vis[X]) * val[k];
          k++;
        }
        return OUT(calc), 0;
      }
      vis[X] = i + 1;
      X = MPow(X, 2);
    }
  }
}

Error: WA

Fixed submission:

#include <stdio.h>
#include <string.h>
#pragma GCC optimize("O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define rep(i, N) for (long i = 0; i < (long)N; i++)
static inline void PUT(char c) {
  static char buf[1 << 15], *ptr = buf;
  if (ptr == buf + strlen(buf) || c == 0) {
    fwrite(buf, 1, ptr - buf, stdout), ptr = buf;
  }
  *ptr++ = c;
}
static inline long IN(void) {
  long x = 0, f = 0, c = getchar();
  while (c < 48 || c > 57) {
    f ^= c == 45, c = getchar();
  }
  while (c > 47 && c < 58) {
    x = x * 10 + c - 48, c = getchar();
  }
  return f ? -x : x;
}
static inline void OUT(long a) {
  if (a < 0)
    PUT('-'), a = -a;
  int d[40], i = 0;
  do {
    d[i++] = a % 10;
  } while (a /= 10);
  while (i--) {
    PUT(d[i] + 48);
  }
  PUT('\\n');
}
long MOD;
static inline long MPow(long a, long b) {
  return b ? MPow(a * a % MOD, b >> 1) * (b & 1 ? a : 1) % MOD : 1;
}
int main() {
  long beginloop = 0, range = 0, sz = 0, N = IN(), X = IN(), calc = 0, sum = 0,
       stopper = 0, temp = X, rangesum = 0, loop[N > 1e7 ? 10000000 : N];
  MOD = IN();
  rep(i, N) {
    if (!stopper) {
      sum += temp;
    } else {
      if (temp != stopper) {
        loop[sz] = temp;
        rangesum += loop[sz++];
      } else {
        range = i - beginloop;
        if (i != beginloop) {
          break;
        }
      }
    }
    if (temp > MPow(temp, 2) && !stopper) {
      beginloop = i + 1;
      stopper = MPow(temp, 2);
      rangesum = stopper;
      loop[sz++] = stopper;
    }
    if (!temp) {
      return OUT(sum + rangesum), 0;
    }
    temp = MPow(temp, 2);
  }
  calc = sum;
  if (range) {
    temp = (N - 1 - beginloop) / range;
    N -= beginloop + range * temp;
    calc += temp * rangesum;
    rep(i, N) { calc += loop[i]; }
  }
  return OUT(calc), 0;
}