let rec ext_gcd a b =
  if b = 0 then (a, 1, 0)
  else
    let (g, x1, y1) = ext_gcd b (a mod b) in
    (g, y1, x1 - (a / b) * y1)

let mod_inverse a m =
  let (_, x, _) = ext_gcd a m in
  ((x mod m) + m) mod m

;;

mod_inverse 3 11 + mod_inverse 5 26 + mod_inverse 7 13
