base*base will exceed the size of an int if base >= 65536. Try this fix, seems to work:
int modpow(int base, int exp, int mod) // from stackoverflow
{
base %= mod;
long long result = 1;
while (exp > 0)
{
if (exp & 1)
result = (result * base) % mod;
base = (int)(((long long)base * (long long)base) % mod);
exp >>= 1;
}
return (int) result;
}