def chinese_remainder_theorem2(n1: int, r1: int, n2: int, r2: int) -> int:
    x, y = invert_modulo(n1, n2), invert_modulo(n2, n1)
    m = n1 * n2
    n = r2 * x * n1 + r1 * y * n2
    return (n % m + m) % m