/* -*- coding: utf-8 -*- * * 1704.cc: No.1704 Many Bus Stops (easy) - yukicoder */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; /* constant */ const int N = 9; const int MOD = 1000000007; /* typedef */ template struct MI { int v; MI(): v() {} MI(int _v): v(_v % MOD) {} MI(long long _v): v(_v % MOD) {} MI operator+(const MI m) const { return MI((v + m.v) % MOD); } MI operator-(const MI m) const { return MI((v + MOD - m.v) % MOD); } MI operator*(const MI m) const { return MI((long long)v * m.v % MOD); } MI &operator+=(const MI m) { return (*this = *this + m); } MI &operator-=(const MI m) { return (*this = *this - m); } MI &operator*=(const MI m) { return (*this = *this * m); } MI pow(int n) const { // a^n % MOD MI pm = 1, a = *this; while (n > 0) { if (n & 1) pm *= a; a *= a; n >>= 1; } return pm; } MI inv() const { return pow(MOD - 2); } MI operator/(const MI m) const { return *this * m.inv(); } MI &operator/=(const MI m) { return (*this = *this / m); } }; typedef MI mi; typedef mi vec[N]; typedef vec mat[N]; /* global variables */ /* subroutines */ inline void initvec(vec a) { memset(a, 0, sizeof(vec)); } inline void initmat(mat a) { memset(a, 0, sizeof(mat)); } inline void unitmat(mat a) { initmat(a); for (int i = 0; i < N; i++) a[i][i] = 1; } inline void copymat(const mat a, mat b) { memcpy(b, a, sizeof(mat)); } inline void mulmat(const mat a, const mat b, mat c) { for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) { c[i][j] = 0; for (int k = 0; k < N; k++) c[i][j] += a[i][k] * b[k][j]; } } inline void powmat(const mat a, int b, mat c) { mat s, t; copymat(a, s); unitmat(c); while (b > 0) { if (b & 1) { mulmat(c, s, t); copymat(t, c); } mulmat(s, s, t); copymat(t, s); b >>= 1; } } inline void mulmatvec(const mat a, const vec b, vec c) { for (int i = 0; i < N; i++) { c[i] = 0; for (int j = 0; j < N; j++) c[i] += a[i][j] * b[j]; } } /* main */ int main() { mi inv3 = mi(3).inv(); // vec = A, A->B, A->C, B->A, B, B->C, C->A, C->B, C mat ma = { { inv3, 0, 0, 1, 0, 0, 1, 0, 0 }, { inv3, 0, 0, 0, 0, 0, 0, 0, 0 }, { inv3, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, inv3, 0, 0, 0, 0 }, { 0, 1, 0, 0, inv3, 0, 0, 1, 0 }, { 0, 0, 0, 0, inv3, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, inv3 }, { 0, 0, 0, 0, 0, 0, 0, 0, inv3 }, { 0, 0, 1, 0, 0, 1, 0, 0, inv3 }, }; int tn; scanf("%d", &tn); while (tn--) { int n; scanf("%d", &n); mat mb; powmat(ma, n, mb); printf("%d\n", mb[0][0].v); } return 0; }