#include #include #include using namespace std; using int64 = long long; const int64 MOD = 1e9 + 7; template struct Matrix { const int M; const int N; std::vector> elts; Matrix(int m, int n, T v = 0) : M(m), N(n), elts(M, std::vector(N, v)) {} template Matrix(int m, int n, InputIt first, InputIt last) : M(m), N(n), elts(M, std::vector(N)) { for (int i = 0; i < M; i++) { std::copy(first + (i * n), first + ((i + 1) * n), elts[i].begin()); } } const std::vector& operator[](int m) const { return elts[m]; } Matrix operator*(const Matrix& m) const { Matrix res(M, m.N, 0); for (int i = 0; i < M; i++) { for (int j = 0; j < m.N; j++) { for (int k = 0; k < N; k++) { (res.elts[i][j] += (elts[i][k] * m.elts[k][j]) % MOD) %= MOD; } } } return res; } Matrix identity() { Matrix m(N, N, 0); for (int i = 0; i < N; i++) m.elts[i][i] = 1; return m; } Matrix pow(int64 n) { if (n == 0) { return identity(); } else if (n % 2 == 1) { return *this * pow(n - 1); } else { Matrix m = pow(n / 2); return m * m; } } }; int main() { int64 N; cin >> N; vector x{ 1, 0, 0, 1 }, A{ 1, 1, 2, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 1, 2, 1 }; Matrix xm(4, 1, x.begin(), x.end()); Matrix Am(4, 4, A.begin(), A.end()); auto bm = Am.pow(N - 1) * xm; cout << bm[3][0] << endl; return 0; }