#include <bits/stdc++.h>
using namespace std;
char a[1000005];
char b[1000005];
int muli[15];
int main() {
  scanf("%s%s", a, b);
  int modo;
  scanf("%d", &modo);
  int i;
  long long sum = 0;
  for (i = 0; a[i] != '\0'; i++) {
    sum *= 10;
    sum += (a[i] - '0');
    sum %= modo;
  }
  muli[0] = 1;
  for (i = 0; b[i] != '\0'; i++)
    ;
  for (i--; b[i] == '0'; i--) {
    b[i] = '9';
  }
  b[i]--;
  for (i = 1; i <= 9; i++) {
    muli[i] = (long long)muli[i - 1] * sum % modo;
  }
  int p = 1;
  int t;
  for (i = 0; b[i] != '\0'; i++) {
    int j;
    t = p;
    for (j = 0; j < 9; j++) {
      p = (long long)t * p % modo;
    }
    p = (long long)p * muli[b[i] - '0'] % modo;
  }
  p = (long long)p * (sum - 1) % modo;
  if (p <= 0) p += modo;
  printf("%d\n", p);
  return 0;
}
