1
2
3
4
5
6
7
8
9
10
int power_mod(int a, int b, int mod){
int res = 1;
a = a % mod;
while (b) {
if (b & 1) res = (res % mod * a % mod) % mod;
b >>= 1;
a = (a % mod * a % mod) % mod;
}
return res;
}