import operator import functools @functools.total_ordering class Mod: __slots__ = ['val','mod'] def __init__(self, val, mod): if not isinstance(val, int): raise ValueError('Value must be integer') if not isinstance(mod, int) or mod<=0: raise ValueError('Modulo must be positive integer') self.val = val % mod self.mod = mod def __repr__(self): return 'Mod({}, {})'.format(self.val, self.mod) def __int__(self): return self.val def __eq__(self, other): if isinstance(other, Mod): if self.mod == other.mod: return self.val==other.val else: return NotImplemented elif isinstance(other, int): return self.val == other else: return NotImplemented def __lt__(self, other): if isinstance(other, Mod): if self.mod == other.mod: return self.val