let rec pow_mod base exp m =
  if exp = 0 then 1
  else if exp mod 2 = 0 then
    let half = pow_mod base (exp / 2) m in
    (half * half) mod m
  else
    (base * pow_mod base (exp - 1) m) mod m

;;

pow_mod 2 30 1000003 + pow_mod 3 20 13 + pow_mod 5 17 100
