#include //#include //using namespace atcoder; #pragma GCC target ("avx2") #pragma GCC optimization ("O3") #pragma GCC optimization ("unroll-loops") using namespace std; typedef vector VI; typedef vector VVI; typedef vector VS; typedef pair PII; typedef pair pii; typedef pair PLL; typedef pair TIII; typedef long long ll; typedef long double ld; typedef unsigned long long ull; #define FOR(i, s, n) for (int i = s; i < (int)n; ++i) #define REP(i, n) FOR(i, 0, n) #define rep(i, a, b) for (int i = a; i < (b); ++i) #define trav(a, x) for (auto &a : x) #define all(x) x.begin(), x.end() #define MOD 1000000007 template inline bool chmax(T1 &a, T2 b) {if (a < b) {a = b; return true;} return false;} template inline bool chmin(T1 &a, T2 b) {if (a > b) {a = b; return true;} return false;} const double EPS = 1e-12, PI = acos(-1); const double pi = 3.141592653589793238462643383279; //ここから編集 typedef string::const_iterator State; ll GCD(ll a, ll b){ return (b==0)?a:GCD(b, a%b); } ll LCM(ll a, ll b){ return a/GCD(a, b) * b; } #include #include #include template struct Modint{ int x; Modint(long y = 0) : x(y >= 0 ? y % mod : (mod - (-y) % mod)) {} Modint& operator++() { x++; if(x == mod) x = 0; return *this; } Modint& operator--() { if(x == 0) x = mod; x--; return *this; } Modint& operator+=(const Modint& a) { x += a.x; if(x >= mod) x -= mod; return *this; } Modint& operator-=(const Modint& a) { x += mod - a.x; if(x >= mod) x -= mod; return *this; } Modint& operator*=(const Modint& a) { x = (1LL) * x * a.x % mod; return *this; } Modint& operator/=(const Modint& a) { x *= a.inv(); return *this; } Modint operator+() const { return *this; } Modint operator-() const { return Modint(-x); } Modint operator+(const Modint& a) const { return Modint(*this) += a; } Modint operator-(const Modint& a) const { return Modint(*this) -= a; } Modint operator*(const Modint& a) const { return Modint(*this) *= a; } Modint operator/(const Modint& a) const { return Modint(*this) /= a; } Modint operator==(const Modint& a) const { return x == a.x; } Modint operator!=(const Modint& a) const { return x != a.x; } Modint pow(long long n) const { Modint x = *this, r = 1; while(n) { if(n & 1) r *= x; x *= x; n >>= 1; } return r; } Modint inv() const { int u = 1, v = 0, a = x, b = mod, t; while(b) { t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } return Modint(u); } friend std::ostream& operator<< (std::ostream& os, const Modint& a) { return os << a.x; } }; template struct Matrix{ int row, col; std::vector> A; Matrix() { row = col = 1; } Matrix(int h, int w, T val = 0) : row(h), col(w), A(row, std::vector(col, val)){} Matrix(const std::vector> &v) : row(v.size()), col(v[0].size()), A(v){} int GetRow() const { return row; } int GetCol() const { return col; } const std::vector& operator[](int i) const { return A[i]; } std::vector& operator[](int i) { return A[i]; } Matrix E(int n) { Matrix M(n, n); for(int i=0; i 0) { if(n&1) B = B * (*this); *this = (*this) * (*this); n >>= 1; } return *this = B; } Matrix operator+(const Matrix& B){ return Matrix(*this) += B; } Matrix operator-(const Matrix& B){ return Matrix(*this) -= B; } Matrix operator*(const Matrix& B){ return Matrix(*this) *= B; } Matrix operator^(long long n){ return Matrix(*this) ^= n; } friend std::ostream& operator<< (std::ostream& os, const Matrix& m) { for(int i=0; i> M >> K; Matrix> m(M, M, 1); for(int i=0; i