matrix matrix::operator*(const matrix &mat) const { matrix res; for (registerint i = 0; i < l; i++) for (registerint j = 0; j < l; j++) for (registerint k = 0; k < l; k++) res.val[i][j] = (res.val[i][j] + val[i][k] * mat.val[k][j]) % MOD; return res; }
template <typename T> T ksm(T a, longlong x) { T ans = a; x--; if (x <= 0) return a; for (; x; x >>= 1) { if (x & 1) ans = ans * a; a = a * a; } return ans; }