Problem p02611 (C++) — buggy submission, verdict: Runtime Error

#include "bits/stdc++.h"
using namespace std;
#ifndef LOCAL
#define endl '\\n'
#endif

#define fr(i, a, b) for (int i = a; i <= b; i++)
#define rf(i, a, b) for (int i = a; i >= b; i--)
#define pf push_front
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(x) (int)x.size()
#define lb lower_bound
#define ub upper_bound

typedef long long ll;
typedef long double f80;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

int pct(int x) { return __builtin_popcount(x); }
int pct(ll x) { return __builtin_popcountll(x); }
int bt(int x) { return 31 - __builtin_clz(x); }  // floor(log2(x))
int bt(ll x) { return 63 - __builtin_clzll(x); } // floor(log2(x))
int cdiv(int a, int b) { return a / b + !(a < 0 || a % b == 0); }
ll cdiv(ll a, ll b) { return a / b + !(a < 0 || a % b == 0); }
int nxt_C(int x) {
  int c = x & -x, r = x + c;
  return (((r ^ x) >> 2) / c) | r;
}
ll nxt_C(ll x) {
  ll c = x & -x, r = x + c;
  return (((r ^ x) >> 2) / c) | r;
}

vector<int> get_bits(int mask) {
  vector<int> bb;
  while (mask) {
    int b = bt(mask);
    bb.pb(b);
    mask ^= (1 << b);
  }
  reverse(all(bb));
  return bb;
}

int get_mask(vector<int> v) {
  int mask = 0;
  for (int x : v) {
    mask ^= (1 << x);
  }
  return mask;
}

template <typename T> void uniq(vector<T> &v) {
  sort(all(v));
  v.resize(unique(all(v)) - v.begin());
}

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

ll rand(ll l, ll r) {
  uniform_int_distribution<ll> uid(l, r);
  return uid(rng);
}

void sc() {}

template <typename Head, typename... Tail> void sc(Head &H, Tail &...T) {
  cin >> H;
  sc(T...);
}

#ifdef LOCAL
#define debug(...)                                                             \\
  cerr << "[L:" << __LINE__ << "][" << #__VA_ARGS__ << "]:",                   \\
      debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

// #ifndef LOCAL
// string to_string(__int128 x) {
// 	string s = "";
// 	bool neg = 0;
// 	if(x < 0) { s += "-"; neg = 1; x = -x; }
// 	if(!x) s += '0';
// 	while(x) {
// 		int rem = x % 10;
// 		s += to_string(rem);
// 		x /= 10;
// 	}
// 	reverse(s.begin() + neg, s.end());
// 	return s;
// }
// #endif

const int mod = 1e9 + 7; // 998244353;

int pwr(int a, ll b) {
  int ans = 1;
  while (b) {
    if (b & 1)
      ans = (ans * 1LL * a) % mod;
    a = (a * 1LL * a) % mod;
    b >>= 1;
  }
  return ans;
}

/*
        Lookout for overflows!!
        Check array sizes!!
        Clear before test cases!!
        Use the correct modulo!!
        Check for corner cases!!
        Are you forgetting something?!
        Read problem statement carefully!!!
*/

template <typename T> T inverse(T a, T m) {
  T u = 0, v = 1;
  while (a != 0) {
    T t = m / a;
    m -= t * a;
    swap(a, m);
    u -= t * v;
    swap(u, v);
  }
  assert(m == 1);
  return u;
}

template <typename T> class Modular {
public:
  using Type = typename decay<decltype(T::value)>::type;

  constexpr Modular() : value() {}
  template <typename U> Modular(const U &x) { value = normalize(x); }

  template <typename U> static Type normalize(const U &x) {
    Type v;
    if (-mod() <= x && x < mod())
      v = static_cast<Type>(x);
    else
      v = static_cast<Type>(x % mod());
    if (v < 0)
      v += mod();
    return v;
  }

  const Type &operator()() const { return value; }
  template <typename U> explicit operator U() const {
    return static_cast<U>(value);
  }
  constexpr static Type mod() { return T::value; }

  Modular &operator+=(const Modular &other) {
    if ((value += other.value) >= mod())
      value -= mod();
    return *this;
  }
  Modular &operator-=(const Modular &other) {
    if ((value -= other.value) < 0)
      value += mod();
    return *this;
  }
  template <typename U> Modular &operator+=(const U &other) {
    return *this += Modular(other);
  }
  template <typename U> Modular &operator-=(const U &other) {
    return *this -= Modular(other);
  }
  Modular &operator++() { return *this += 1; }
  Modular &operator--() { return *this -= 1; }
  Modular operator++(int) {
    Modular result(*this);
    *this += 1;
    return result;
  }
  Modular operator--(int) {
    Modular result(*this);
    *this -= 1;
    return result;
  }
  Modular operator-() const { return Modular(-value); }

  template <typename U = T>
  typename enable_if<is_same<typename Modular<U>::Type, int>::value,
                     Modular>::type &
  operator*=(const Modular &rhs) {
#ifdef _WIN32
    uint64_t x = static_cast<int64_t>(value) * static_cast<int64_t>(rhs.value);
    uint32_t xh = static_cast<uint32_t>(x >> 32), xl = static_cast<uint32_t>(x),
             d, m;
    asm("divl %4; \\n\\t" : "=a"(d), "=d"(m) : "d"(xh), "a"(xl), "r"(mod()));
    value = m;
#else
    value = normalize(static_cast<int64_t>(value) *
                      static_cast<int64_t>(rhs.value));
#endif
    return *this;
  }
  template <typename U = T>
  typename enable_if<is_same<typename Modular<U>::Type, int64_t>::value,
                     Modular>::type &
  operator*=(const Modular &rhs) {
    int64_t q = static_cast<int64_t>(static_cast<long double>(value) *
                                     rhs.value / mod());
    value = normalize(value * rhs.value - q * mod());
    return *this;
  }
  template <typename U = T>
  typename enable_if<!is_integral<typename Modular<U>::Type>::value,
                     Modular>::type &
  operator*=(const Modular &rhs) {
    value = normalize(value * rhs.value);
    return *this;
  }

  Modular &operator/=(const Modular &other) {
    return *this *= Modular(inverse(other.value, mod()));
  }

  template <typename U> friend const Modular<U> &abs(const Modular<U> &v) {
    return v;
  }

  template <typename U>
  friend bool operator==(const Modular<U> &lhs, const Modular<U> &rhs);

  template <typename U>
  friend bool operator<(const Modular<U> &lhs, const Modular<U> &rhs);

  template <typename U>
  friend std::istream &operator>>(std::istream &stream, Modular<U> &number);

private:
  Type value;
};

template <typename T>
bool operator==(const Modular<T> &lhs, const Modular<T> &rhs) {
  return lhs.value == rhs.value;
}
template <typename T, typename U>
bool operator==(const Modular<T> &lhs, U rhs) {
  return lhs == Modular<T>(rhs);
}
template <typename T, typename U>
bool operator==(U lhs, const Modular<T> &rhs) {
  return Modular<T>(lhs) == rhs;
}

template <typename T>
bool operator!=(const Modular<T> &lhs, const Modular<T> &rhs) {
  return !(lhs == rhs);
}
template <typename T, typename U>
bool operator!=(const Modular<T> &lhs, U rhs) {
  return !(lhs == rhs);
}
template <typename T, typename U>
bool operator!=(U lhs, const Modular<T> &rhs) {
  return !(lhs == rhs);
}

template <typename T>
bool operator<(const Modular<T> &lhs, const Modular<T> &rhs) {
  return lhs.value < rhs.value;
}

template <typename T>
Modular<T> operator+(const Modular<T> &lhs, const Modular<T> &rhs) {
  return Modular<T>(lhs) += rhs;
}
template <typename T, typename U>
Modular<T> operator+(const Modular<T> &lhs, U rhs) {
  return Modular<T>(lhs) += rhs;
}
template <typename T, typename U>
Modular<T> operator+(U lhs, const Modular<T> &rhs) {
  return Modular<T>(lhs) += rhs;
}

template <typename T>
Modular<T> operator-(const Modular<T> &lhs, const Modular<T> &rhs) {
  return Modular<T>(lhs) -= rhs;
}
template <typename T, typename U>
Modular<T> operator-(const Modular<T> &lhs, U rhs) {
  return Modular<T>(lhs) -= rhs;
}
template <typename T, typename U>
Modular<T> operator-(U lhs, const Modular<T> &rhs) {
  return Modular<T>(lhs) -= rhs;
}

template <typename T>
Modular<T> operator*(const Modular<T> &lhs, const Modular<T> &rhs) {
  return Modular<T>(lhs) *= rhs;
}
template <typename T, typename U>
Modular<T> operator*(const Modular<T> &lhs, U rhs) {
  return Modular<T>(lhs) *= rhs;
}
template <typename T, typename U>
Modular<T> operator*(U lhs, const Modular<T> &rhs) {
  return Modular<T>(lhs) *= rhs;
}

template <typename T>
Modular<T> operator/(const Modular<T> &lhs, const Modular<T> &rhs) {
  return Modular<T>(lhs) /= rhs;
}
template <typename T, typename U>
Modular<T> operator/(const Modular<T> &lhs, U rhs) {
  return Modular<T>(lhs) /= rhs;
}
template <typename T, typename U>
Modular<T> operator/(U lhs, const Modular<T> &rhs) {
  return Modular<T>(lhs) /= rhs;
}

template <typename T, typename U>
Modular<T> power(const Modular<T> &a, const U &b) {
  assert(b >= 0);
  Modular<T> x = a, res = 1;
  U p = b;
  while (p > 0) {
    if (p & 1)
      res *= x;
    x *= x;
    p >>= 1;
  }
  return res;
}

template <typename T> bool IsZero(const Modular<T> &number) {
  return number() == 0;
}

template <typename T> string to_string(const Modular<T> &number) {
  return to_string(number());
}

template <typename T>
std::ostream &operator<<(std::ostream &stream, const Modular<T> &number) {
  return stream << number();
}

template <typename T>
std::istream &operator>>(std::istream &stream, Modular<T> &number) {
  typename common_type<typename Modular<T>::Type, int64_t>::type x;
  stream >> x;
  number.value = Modular<T>::normalize(x);
  return stream;
}

/*
using ModType = int;

struct VarMod { static ModType value; };
ModType VarMod::value;
ModType& md = VarMod::value;
using Mint = Modular<VarMod>;

*/

constexpr int md = 1e9 + 7;
using Mint = Modular<std::integral_constant<decay<decltype(md)>::type, md>>;

Mint pwr(Mint a, ll k) {
  Mint ans = 1;
  while (k) {
    if (k & 1)
      ans = ans * a;
    a = a * a;
    k >>= 1;
  }
  return ans;
}

template <const int maxn, const int mod> struct linear_solver {
  static const long long sqmod = (long long)mod * mod;
  int n;
  int a[maxn], h[maxn], s[maxn], t[maxn];
  long long t_[maxn];

  inline int fpow(int a, long long b) {
    int res = 1;
    while (b) {
      if (b & 1)
        res = (long long)res * a % mod;
      a = (long long)a * a % mod;
      b >>= 1;
    }
    return res;
  }
  inline vector<int> BM(vector<int> x) {
    vector<int> ls, cur;
    int lf, ld;
    for (int i = 0; i < (int)x.size(); i++) {
      long long t = 0;
      for (int j = 0; j < (int)cur.size(); j++) {
        t += (long long)x[i - j - 1] * cur[j];
        t -= sqmod <= t ? sqmod : 0;
      }
      t %= mod;
      if ((t - x[i]) % mod == 0)
        continue;
      if (!cur.size()) {
        cur.resize(i + 1);
        lf = i;
        ld = t - x[i];
        ld += ld < 0 ? mod : 0;
        continue;
      }
      int k = (long long)(t - x[i] + mod) * fpow(ld, mod - 2) % mod;
      vector<int> c(i - lf - 1);
      c.push_back(k);
      for (int j = 0; j < (int)ls.size(); j++) {
        c.push_back((long long)k * (mod - ls[j]) % mod);
      }
      if (c.size() < cur.size())
        c.resize(cur.size());
      for (int j = 0; j < (int)cur.size(); j++) {
        c[j] += cur[j];
        c[j] -= mod <= c[j] ? mod : 0;
      }
      if (i - lf + (int)ls.size() >= (int)cur.size()) {
        ls = cur, lf = i;
        ld = t - x[i];
        ld += ld < 0 ? mod : 0;
      }
      cur = c;
    }
    for (int i = 0; i < (int)cur.size(); i++) {
      cur[i] = (cur[i] % mod + mod) % mod;
    }
    return cur;
  }
  inline void mult(int *p, int *q) {
    for (int i = 0; i < n + n; i++)
      t_[i] = 0;
    for (int i = 0; i < n; i++)
      if (p[i]) {
        for (int j = 0; j < n; j++) {
          t_[i + j] += (long long)p[i] * q[j];
          t_[i + j] -= sqmod <= t_[i + j] ? sqmod : 0;
        }
      }
    for (int i = n + n - 1; n <= i; i--)
      if (t_[i]) {
        t_[i] %= mod;
        for (int j = n - 1; ~j; j--) {
          t_[i - j - 1] += t_[i] * h[j];
          t_[i - j - 1] -= sqmod <= t_[i - j - 1] ? sqmod : 0;
        }
      }
    for (int i = 0; i < n; i++)
      p[i] = t_[i] % mod;
  }
  inline long long calc(long long k) {
    for (int i = n; ~i; i--) {
      s[i] = t[i] = 0;
    }
    s[0] = 1;
    if (n != 1) {
      t[1] = 1;
    } else {
      t[0] = h[0];
    }
    while (k) {
      if (k & 1)
        mult(s, t);
      mult(t, t);
      k >>= 1;
    }
    long long sum = 0;
    for (int i = 0; i < n; i++) {
      sum = (sum + (long long)s[i] * a[i]) % mod;
    }
    return (sum % mod + mod) % mod;
  }
  inline int calc(vector<int> x, long long k) {
    if (k < (int)x.size())
      return x[k];
    vector<int> v = BM(x);
    n = v.size();
    if (!n)
      return 0;
    for (int i = 0; i < n; i++)
      h[i] = v[i], a[i] = x[i];
    return calc(k);
  }
};
linear_solver<200, (int)1e9 + 7> ls;

const int N = 200;
Mint ncr[N][N];
Mint ways[N];

void solve() {
  fr(i, 0, N - 1) {
    ncr[i][0] = 1;
    fr(j, 1, i) { ncr[i][j] = ncr[i - 1][j] + ncr[i - 1][j - 1]; }
  }
  vector<int> vv;
  fr(i, 0, N - 5) {
    ways[i] = ncr[i + 4][4];
    if (i > 0)
      ways[i] += ways[i - 1];
  }
  for (int n = 0; n <= 50; n++) {
    Mint ans = 0;
    fr(d1, 1, n) {
      fr(d2, 1, n - d1) {
        fr(d3, 1, n - d1 - d2) {
          fr(d4, 1, n - d1 - d2 - d3) {
            fr(d5, 1, n - d1 - d2 - d3 - d4) {
              int z = (n - d1 - d2 - d3 - d4 - d5) / 2;
              Mint val = Mint(d1) * Mint(d2) * Mint(d3) * Mint(d4) * Mint(d5);
              assert(z >= 0);
              ans += val * ways[z];
            }
          }
        }
      }
    }
    vv.pb((int)ans);
  }
  int t;
  sc(t);
  while (t--) {
    int n;
    sc(n);
    cout << ls.calc(vv, n) << endl;
  }
}

int main() {
  ios ::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  // cin >> t;
  for (int tt = 1; tt <= t; tt++) {
    solve();
  }
  return 0;
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
#ifndef LOCAL
#define endl '\\n'
#endif

#define fr(i, a, b) for (int i = a; i <= b; i++)
#define rf(i, a, b) for (int i = a; i >= b; i--)
#define pf push_front
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(x) (int)x.size()
#define lb lower_bound
#define ub upper_bound

typedef long long ll;
typedef long double f80;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

int pct(int x) { return __builtin_popcount(x); }
int pct(ll x) { return __builtin_popcountll(x); }
int bt(int x) { return 31 - __builtin_clz(x); }  // floor(log2(x))
int bt(ll x) { return 63 - __builtin_clzll(x); } // floor(log2(x))
int cdiv(int a, int b) { return a / b + !(a < 0 || a % b == 0); }
ll cdiv(ll a, ll b) { return a / b + !(a < 0 || a % b == 0); }
int nxt_C(int x) {
  int c = x & -x, r = x + c;
  return (((r ^ x) >> 2) / c) | r;
}
ll nxt_C(ll x) {
  ll c = x & -x, r = x + c;
  return (((r ^ x) >> 2) / c) | r;
}

vector<int> get_bits(int mask) {
  vector<int> bb;
  while (mask) {
    int b = bt(mask);
    bb.pb(b);
    mask ^= (1 << b);
  }
  reverse(all(bb));
  return bb;
}

int get_mask(vector<int> v) {
  int mask = 0;
  for (int x : v) {
    mask ^= (1 << x);
  }
  return mask;
}

template <typename T> void uniq(vector<T> &v) {
  sort(all(v));
  v.resize(unique(all(v)) - v.begin());
}

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

ll rand(ll l, ll r) {
  uniform_int_distribution<ll> uid(l, r);
  return uid(rng);
}

void sc() {}

template <typename Head, typename... Tail> void sc(Head &H, Tail &...T) {
  cin >> H;
  sc(T...);
}

#ifdef LOCAL
#define debug(...)                                                             \\
  cerr << "[L:" << __LINE__ << "][" << #__VA_ARGS__ << "]:",                   \\
      debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

// #ifndef LOCAL
// string to_string(__int128 x) {
// 	string s = "";
// 	bool neg = 0;
// 	if(x < 0) { s += "-"; neg = 1; x = -x; }
// 	if(!x) s += '0';
// 	while(x) {
// 		int rem = x % 10;
// 		s += to_string(rem);
// 		x /= 10;
// 	}
// 	reverse(s.begin() + neg, s.end());
// 	return s;
// }
// #endif

const int mod = 1e9 + 7; // 998244353;

int pwr(int a, ll b) {
  int ans = 1;
  while (b) {
    if (b & 1)
      ans = (ans * 1LL * a) % mod;
    a = (a * 1LL * a) % mod;
    b >>= 1;
  }
  return ans;
}

/*
        Lookout for overflows!!
        Check array sizes!!
        Clear before test cases!!
        Use the correct modulo!!
        Check for corner cases!!
        Are you forgetting something?!
        Read problem statement carefully!!!
*/

template <typename T> T inverse(T a, T m) {
  T u = 0, v = 1;
  while (a != 0) {
    T t = m / a;
    m -= t * a;
    swap(a, m);
    u -= t * v;
    swap(u, v);
  }
  assert(m == 1);
  return u;
}

template <typename T> class Modular {
public:
  using Type = typename decay<decltype(T::value)>::type;

  constexpr Modular() : value() {}
  template <typename U> Modular(const U &x) { value = normalize(x); }

  template <typename U> static Type normalize(const U &x) {
    Type v;
    if (-mod() <= x && x < mod())
      v = static_cast<Type>(x);
    else
      v = static_cast<Type>(x % mod());
    if (v < 0)
      v += mod();
    return v;
  }

  const Type &operator()() const { return value; }
  template <typename U> explicit operator U() const {
    return static_cast<U>(value);
  }
  constexpr static Type mod() { return T::value; }

  Modular &operator+=(const Modular &other) {
    if ((value += other.value) >= mod())
      value -= mod();
    return *this;
  }
  Modular &operator-=(const Modular &other) {
    if ((value -= other.value) < 0)
      value += mod();
    return *this;
  }
  template <typename U> Modular &operator+=(const U &other) {
    return *this += Modular(other);
  }
  template <typename U> Modular &operator-=(const U &other) {
    return *this -= Modular(other);
  }
  Modular &operator++() { return *this += 1; }
  Modular &operator--() { return *this -= 1; }
  Modular operator++(int) {
    Modular result(*this);
    *this += 1;
    return result;
  }
  Modular operator--(int) {
    Modular result(*this);
    *this -= 1;
    return result;
  }
  Modular operator-() const { return Modular(-value); }

  template <typename U = T>
  typename enable_if<is_same<typename Modular<U>::Type, int>::value,
                     Modular>::type &
  operator*=(const Modular &rhs) {
#ifdef _WIN32
    uint64_t x = static_cast<int64_t>(value) * static_cast<int64_t>(rhs.value);
    uint32_t xh = static_cast<uint32_t>(x >> 32), xl = static_cast<uint32_t>(x),
             d, m;
    asm("divl %4; \\n\\t" : "=a"(d), "=d"(m) : "d"(xh), "a"(xl), "r"(mod()));
    value = m;
#else
    value = normalize(static_cast<int64_t>(value) *
                      static_cast<int64_t>(rhs.value));
#endif
    return *this;
  }
  template <typename U = T>
  typename enable_if<is_same<typename Modular<U>::Type, int64_t>::value,
                     Modular>::type &
  operator*=(const Modular &rhs) {
    int64_t q = static_cast<int64_t>(static_cast<long double>(value) *
                                     rhs.value / mod());
    value = normalize(value * rhs.value - q * mod());
    return *this;
  }
  template <typename U = T>
  typename enable_if<!is_integral<typename Modular<U>::Type>::value,
                     Modular>::type &
  operator*=(const Modular &rhs) {
    value = normalize(value * rhs.value);
    return *this;
  }

  Modular &operator/=(const Modular &other) {
    return *this *= Modular(inverse(other.value, mod()));
  }

  template <typename U> friend const Modular<U> &abs(const Modular<U> &v) {
    return v;
  }

  template <typename U>
  friend bool operator==(const Modular<U> &lhs, const Modular<U> &rhs);

  template <typename U>
  friend bool operator<(const Modular<U> &lhs, const Modular<U> &rhs);

  template <typename U>
  friend std::istream &operator>>(std::istream &stream, Modular<U> &number);

private:
  Type value;
};

template <typename T>
bool operator==(const Modular<T> &lhs, const Modular<T> &rhs) {
  return lhs.value == rhs.value;
}
template <typename T, typename U>
bool operator==(const Modular<T> &lhs, U rhs) {
  return lhs == Modular<T>(rhs);
}
template <typename T, typename U>
bool operator==(U lhs, const Modular<T> &rhs) {
  return Modular<T>(lhs) == rhs;
}

template <typename T>
bool operator!=(const Modular<T> &lhs, const Modular<T> &rhs) {
  return !(lhs == rhs);
}
template <typename T, typename U>
bool operator!=(const Modular<T> &lhs, U rhs) {
  return !(lhs == rhs);
}
template <typename T, typename U>
bool operator!=(U lhs, const Modular<T> &rhs) {
  return !(lhs == rhs);
}

template <typename T>
bool operator<(const Modular<T> &lhs, const Modular<T> &rhs) {
  return lhs.value < rhs.value;
}

template <typename T>
Modular<T> operator+(const Modular<T> &lhs, const Modular<T> &rhs) {
  return Modular<T>(lhs) += rhs;
}
template <typename T, typename U>
Modular<T> operator+(const Modular<T> &lhs, U rhs) {
  return Modular<T>(lhs) += rhs;
}
template <typename T, typename U>
Modular<T> operator+(U lhs, const Modular<T> &rhs) {
  return Modular<T>(lhs) += rhs;
}

template <typename T>
Modular<T> operator-(const Modular<T> &lhs, const Modular<T> &rhs) {
  return Modular<T>(lhs) -= rhs;
}
template <typename T, typename U>
Modular<T> operator-(const Modular<T> &lhs, U rhs) {
  return Modular<T>(lhs) -= rhs;
}
template <typename T, typename U>
Modular<T> operator-(U lhs, const Modular<T> &rhs) {
  return Modular<T>(lhs) -= rhs;
}

template <typename T>
Modular<T> operator*(const Modular<T> &lhs, const Modular<T> &rhs) {
  return Modular<T>(lhs) *= rhs;
}
template <typename T, typename U>
Modular<T> operator*(const Modular<T> &lhs, U rhs) {
  return Modular<T>(lhs) *= rhs;
}
template <typename T, typename U>
Modular<T> operator*(U lhs, const Modular<T> &rhs) {
  return Modular<T>(lhs) *= rhs;
}

template <typename T>
Modular<T> operator/(const Modular<T> &lhs, const Modular<T> &rhs) {
  return Modular<T>(lhs) /= rhs;
}
template <typename T, typename U>
Modular<T> operator/(const Modular<T> &lhs, U rhs) {
  return Modular<T>(lhs) /= rhs;
}
template <typename T, typename U>
Modular<T> operator/(U lhs, const Modular<T> &rhs) {
  return Modular<T>(lhs) /= rhs;
}

template <typename T, typename U>
Modular<T> power(const Modular<T> &a, const U &b) {
  assert(b >= 0);
  Modular<T> x = a, res = 1;
  U p = b;
  while (p > 0) {
    if (p & 1)
      res *= x;
    x *= x;
    p >>= 1;
  }
  return res;
}

template <typename T> bool IsZero(const Modular<T> &number) {
  return number() == 0;
}

template <typename T> string to_string(const Modular<T> &number) {
  return to_string(number());
}

template <typename T>
std::ostream &operator<<(std::ostream &stream, const Modular<T> &number) {
  return stream << number();
}

template <typename T>
std::istream &operator>>(std::istream &stream, Modular<T> &number) {
  typename common_type<typename Modular<T>::Type, int64_t>::type x;
  stream >> x;
  number.value = Modular<T>::normalize(x);
  return stream;
}

/*
using ModType = int;

struct VarMod { static ModType value; };
ModType VarMod::value;
ModType& md = VarMod::value;
using Mint = Modular<VarMod>;

*/

constexpr int md = 1e9 + 7;
using Mint = Modular<std::integral_constant<decay<decltype(md)>::type, md>>;

Mint pwr(Mint a, ll k) {
  Mint ans = 1;
  while (k) {
    if (k & 1)
      ans = ans * a;
    a = a * a;
    k >>= 1;
  }
  return ans;
}

template <const int maxn, const int mod> struct linear_solver {
  static const long long sqmod = (long long)mod * mod;
  int n;
  int a[maxn], h[maxn], s[maxn], t[maxn];
  long long t_[maxn];

  inline int fpow(int a, long long b) {
    int res = 1;
    while (b) {
      if (b & 1)
        res = (long long)res * a % mod;
      a = (long long)a * a % mod;
      b >>= 1;
    }
    return res;
  }
  inline vector<int> BM(vector<int> x) {
    vector<int> ls, cur;
    int lf, ld;
    for (int i = 0; i < (int)x.size(); i++) {
      long long t = 0;
      for (int j = 0; j < (int)cur.size(); j++) {
        t += (long long)x[i - j - 1] * cur[j];
        t -= sqmod <= t ? sqmod : 0;
      }
      t %= mod;
      if ((t - x[i]) % mod == 0)
        continue;
      if (!cur.size()) {
        cur.resize(i + 1);
        lf = i;
        ld = t - x[i];
        ld += ld < 0 ? mod : 0;
        continue;
      }
      int k = (long long)(t - x[i] + mod) * fpow(ld, mod - 2) % mod;
      vector<int> c(i - lf - 1);
      c.push_back(k);
      for (int j = 0; j < (int)ls.size(); j++) {
        c.push_back((long long)k * (mod - ls[j]) % mod);
      }
      if (c.size() < cur.size())
        c.resize(cur.size());
      for (int j = 0; j < (int)cur.size(); j++) {
        c[j] += cur[j];
        c[j] -= mod <= c[j] ? mod : 0;
      }
      if (i - lf + (int)ls.size() >= (int)cur.size()) {
        ls = cur, lf = i;
        ld = t - x[i];
        ld += ld < 0 ? mod : 0;
      }
      cur = c;
    }
    for (int i = 0; i < (int)cur.size(); i++) {
      cur[i] = (cur[i] % mod + mod) % mod;
    }
    return cur;
  }
  inline void mult(int *p, int *q) {
    for (int i = 0; i < n + n; i++)
      t_[i] = 0;
    for (int i = 0; i < n; i++)
      if (p[i]) {
        for (int j = 0; j < n; j++) {
          t_[i + j] += (long long)p[i] * q[j];
          t_[i + j] -= sqmod <= t_[i + j] ? sqmod : 0;
        }
      }
    for (int i = n + n - 1; n <= i; i--)
      if (t_[i]) {
        t_[i] %= mod;
        for (int j = n - 1; ~j; j--) {
          t_[i - j - 1] += t_[i] * h[j];
          t_[i - j - 1] -= sqmod <= t_[i - j - 1] ? sqmod : 0;
        }
      }
    for (int i = 0; i < n; i++)
      p[i] = t_[i] % mod;
  }
  inline long long calc(long long k) {
    for (int i = n; ~i; i--) {
      s[i] = t[i] = 0;
    }
    s[0] = 1;
    if (n != 1) {
      t[1] = 1;
    } else {
      t[0] = h[0];
    }
    while (k) {
      if (k & 1)
        mult(s, t);
      mult(t, t);
      k >>= 1;
    }
    long long sum = 0;
    for (int i = 0; i < n; i++) {
      sum = (sum + (long long)s[i] * a[i]) % mod;
    }
    return (sum % mod + mod) % mod;
  }
  inline int calc(vector<int> x, long long k) {
    if (k < (int)x.size())
      return x[k];
    vector<int> v = BM(x);
    n = v.size();
    if (!n)
      return 0;
    for (int i = 0; i < n; i++)
      h[i] = v[i], a[i] = x[i];
    return calc(k);
  }
};
linear_solver<1 << 18, (int)1e9 + 7> ls;

const int N = 105;
Mint ncr[N][N];
Mint ways[N];

void solve() {
  fr(i, 0, N - 1) {
    ncr[i][0] = 1;
    fr(j, 1, i) { ncr[i][j] = ncr[i - 1][j] + ncr[i - 1][j - 1]; }
  }
  vector<pair<Mint, Mint>> v;
  vector<int> vv;
  fr(i, 0, N - 1) {
    ways[i] = ncr[i + 4][4];
    if (i > 0)
      ways[i] += ways[i - 1];
  }
  for (int n = 0; n <= 50; n++) {
    Mint ans = 0;
    fr(d1, 1, n) {
      fr(d2, 1, n - d1) {
        fr(d3, 1, n - d1 - d2) {
          fr(d4, 1, n - d1 - d2 - d3) {
            fr(d5, 1, n - d1 - d2 - d3 - d4) {
              int z = (n - d1 - d2 - d3 - d4 - d5) / 2;
              Mint val = Mint(d1) * Mint(d2) * Mint(d3) * Mint(d4) * Mint(d5);
              ans += val * ways[z];
            }
          }
        }
      }
    }
    v.pb({n, ans});
    vv.pb((int)ans);
    debug(n, ans);
  }
  int t;
  sc(t);
  while (t--) {
    int n;
    sc(n);
    cout << ls.calc(vv, n) << endl;
  }
}

int main() {
  ios ::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  // cin >> t;
  for (int tt = 1; tt <= t; tt++) {
    solve();
  }
  return 0;
}