Source code for titan_pylib.math.mod_int_1000000007
1from typing import Union
2
3_titan_pylib_ModInt1000000007_MOD = 1000000007
4
5
[docs]
6class ModInt1000000007:
7
8 __slots__ = "val"
9
10 @staticmethod
11 def _inv(a: int) -> int:
12 res = 1
13 b = _titan_pylib_ModInt1000000007_MOD - 2
14 while b:
15 if b & 1:
16 res = res * a % _titan_pylib_ModInt1000000007_MOD
17 a = a * a % _titan_pylib_ModInt1000000007_MOD
18 b >>= 1
19 return res
20
[docs]
21 @staticmethod
22 def get_mod() -> int:
23 return _titan_pylib_ModInt1000000007_MOD
24
25 def __init__(self, val: int) -> None:
26 self.val = (
27 val
28 if 0 <= val < _titan_pylib_ModInt1000000007_MOD
29 else val % _titan_pylib_ModInt1000000007_MOD
30 )
31
32 def __add__(self, other: Union[int, "ModInt1000000007"]) -> "ModInt1000000007":
33 return ModInt1000000007(self.val + int(other))
34
35 def __sub__(self, other: Union[int, "ModInt1000000007"]) -> "ModInt1000000007":
36 return ModInt1000000007(self.val - int(other))
37
38 def __mul__(self, other: Union[int, "ModInt1000000007"]) -> "ModInt1000000007":
39 return ModInt1000000007(self.val * int(other))
40
41 def __pow__(self, other: Union[int, "ModInt1000000007"]) -> "ModInt1000000007":
42 return ModInt1000000007(
43 pow(self.val, int(other), _titan_pylib_ModInt1000000007_MOD)
44 )
45
46 def __truediv__(self, other: Union[int, "ModInt1000000007"]) -> "ModInt1000000007":
47 return ModInt1000000007(self.val * (self._inv(int(other))))
48
49 # __iadd__ = __add__
50 # __isub__ = __sub__
51 # __imul__ = __mul__
52 # __ipow__ = __pow__
53 # __itruediv__ = __truediv__
54
55 def __iadd__(self, other: Union[int, "ModInt1000000007"]) -> "ModInt1000000007":
56 self.val += int(other)
57 self.val %= _titan_pylib_ModInt1000000007_MOD
58 return self
59
60 def __isub__(self, other: Union[int, "ModInt1000000007"]) -> "ModInt1000000007":
61 self.val -= int(other)
62 self.val %= _titan_pylib_ModInt1000000007_MOD
63 return self
64
65 def __imul__(self, other: Union[int, "ModInt1000000007"]) -> "ModInt1000000007":
66 self.val *= int(other)
67 self.val %= _titan_pylib_ModInt1000000007_MOD
68 return self
69
70 def __ipow__(self, other: Union[int, "ModInt1000000007"]) -> "ModInt1000000007":
71 self.val = pow(self.val, int(other), _titan_pylib_ModInt1000000007_MOD)
72 return self
73
74 def __itruediv__(self, other: Union[int, "ModInt1000000007"]) -> "ModInt1000000007":
75 self.val *= self._inv(int(other))
76 self.val %= _titan_pylib_ModInt1000000007_MOD
77 return self
78
79 __radd__ = __add__
80 __rmul__ = __mul__
81
82 def __rsub__(self, other: Union[int, "ModInt1000000007"]) -> "ModInt1000000007":
83 return ModInt1000000007(int(other) - self.val)
84
85 def __rpow__(self, other: Union[int, "ModInt1000000007"]) -> "ModInt1000000007":
86 return ModInt1000000007(
87 pow(int(other), self.val, _titan_pylib_ModInt1000000007_MOD)
88 )
89
90 def __rtruediv__(self, other: Union[int, "ModInt1000000007"]) -> "ModInt1000000007":
91 return ModInt1000000007(int(other) * self._inv(self.val))
92
93 def __eq__(self, other: Union[int, "ModInt1000000007"]) -> bool:
94 return self.val == int(other)
95
96 def __lt__(self, other: Union[int, "ModInt1000000007"]) -> bool:
97 return self.val < int(other)
98
99 def __le__(self, other: Union[int, "ModInt1000000007"]) -> bool:
100 return self.val <= int(other)
101
102 def __gt__(self, other: Union[int, "ModInt1000000007"]) -> bool:
103 return self.val > int(other)
104
105 def __ge__(self, other: Union[int, "ModInt1000000007"]) -> bool:
106 return self.val >= int(other)
107
108 def __ne__(self, other: Union[int, "ModInt1000000007"]) -> bool:
109 return self.val != int(other)
110
111 def __neg__(self) -> "ModInt1000000007":
112 return ModInt1000000007(-self.val)
113
114 def __pos__(self) -> "ModInt1000000007":
115 return ModInt1000000007(self.val)
116
117 def __int__(self) -> int:
118 return self.val
119
120 def __str__(self) -> str:
121 return str(self.val)
122
123 def __repr__(self):
124 # return f'ModInt1000000007({self})'
125 return f"{self}"