#/ref https://atcoder.jp/contests/abc193/submissions/20516429 def _inv_gcd(a: int, b: int) -> tuple: a %= b if a == 0: return(b, 0) s = b t = a m0 = 0 m1 = 1 while t: u = s//t s -= t*u m0 -= m1*u s, t = t, s m0, m1 = m1, m0 if m0 < 0: m0 += b//s return(s, m0) def CRT(R: list, M: list): """ Chinese Remainder Theorem Arguments: R: list of remainders M: list of modulo all_same(x%ri(mod mi) for i in range(len(r))) Return: x: s.t x==ri(mod mi) for all i in range(len(r))(0