void exgcd(LL a, LL b, LL& d, LL& x, LL& y) {
if(b == 0) {
d = a; x = 1; y = 0;
} else {
exgcd(b, a%b, d, y, x);
y -= x*(a/b);
}
}
LL inv(LL a, LL mod) {
LL d, x, y;
exgcd(a, mod, d, x, y);
return d == 1 ? (x+mod)%mod : -1;
}