public long fastPower(long a, long b, long num) { long result = 1; while (b > 0) { if ((b & 1) == 1) { result = (result * a) % num; } a = (a * a) % num; b >>= 1; } return result; }