/**
* @verified
*
* - https://atcoder.jp/contests/arc050/tasks/arc050_c
*
- https://atcoder.jp/contests/abc129/tasks/abc129_f
*
*/
class ModIntFactory {
private final ModArithmetic ma;
private final int mod;
public ModIntFactory(int mod) {
this.ma = ModArithmetic.of(mod);
this.mod = mod;
}
public ModInt create(long value) {
if ((value %= mod) = mod ? res - mod : res;
}
public int sub(int a, int b) {
int res = a - b;
return res = 1) {
long t = a / b;
a -= t * b;
int tmp1 = a; a = b; b = tmp1;
u -= t * v;
long tmp2 = u; u = v; v = tmp2;
}
u %= mod;
if (a != 1) {
throw new ArithmeticException("divide by zero");
}
return (int) (u 0) {
long lsb = b & -b;
for (; lsb != idx; idx = mod ? res - mod : res;
}
public int sub(int a, int b) {
int res = a - b;
return res = 1) {
long t = a / b;
a -= t * b;
int tmp1 = a; a = b; b = tmp1;
u -= t * v;
long tmp2 = u; u = v; v = tmp2;
}
u %= mod;
if (a != 1) {
throw new ArithmeticException("divide by zero");
}
return (int) (u 0) {
long lsb = b & -b;
for (; lsb != idx; idx >= 1;
s >> 32;
return (int) (x (a*mod + b)^2 = p*mod + q
* => p = mod*a^2 + 2ab + floor(b^2/mod)
*/
long a = (1l >> 32;
ml = m & mask;
}
private int reduce(long x) {
long z = (x & mask) * ml;
z = (x & mask) * mh + (x >>> 32) * ml + (z >>> 32);
z = (x >>> 32) * mh + (z >>> 32);
x -= z * mod;
return (int) (x = mod ? sum - mod : sum;
}
public int sub(int a, int b) {
int sum = a - b;
return sum = 1) {
long t = a / b;
a -= t * b;
int tmp1 = a; a = b; b = tmp1;
u -= t * v;
long tmp2 = u; u = v; v = tmp2;
}
u %= mod;
if (a != 1) {
throw new ArithmeticException("divide by zero");
}
return (int) (u 0) {
long lsb = b & -b;
for (; lsb != idx; idx