Language: C++

#include <cstdio>
#include <numeric>
#include <cmath>
#include <iostream>
#include <set>
#include <array>
using i64 = long long;
using namespace std;

class ModInt {
public:
  int value;
  static const int mod = 1e9 + 7;
  ModInt(int value = 0) {
    this->value = normalize(value);
  }  
  ModInt& operator+=(const ModInt& other) {
      this->value = (*this + other).value;
      return *this;
  }
  ModInt& operator-=(const ModInt& other) {
      this->value = (*this - other).value;
      return *this;
  }
  ModInt& operator*=(const ModInt& other) {
      this->value = (*this * other).value;
      return *this;
  }
  ModInt& operator/=(const ModInt& other) {
      this->value = (*this / other).value;
      return *this;
  }
  int inverse(int a) const {
    int m = mod;
    int u = 0, v = 1;
    while (a != 0) {
        int t = m / a;
        m -= t * a; swap(a, m);
        u -= t * v; swap(u, v);
    }
    assert(m == 1);
    return u;
  }
  ModInt operator+(const ModInt &R) const {
    return ModInt(ModInt::normalize(this->value + R.value));
  } 
  ModInt operator-(const ModInt &R) const {
    return ModInt(ModInt::normalize(this->value - R.value));
  } 
  ModInt operator*(const ModInt &R) const {
    return ModInt(ModInt::normalize((i64) this->value * R.value));
  } 
  ModInt operator/(const ModInt &R) const {
    return ModInt(ModInt::normalize((i64) this->value * inverse(R.value)));
  }
  int normalize(i64 value) const {
    while (value < 0) value += mod;
    value %= mod;
    return value;
  }
};

ostream& operator<<(ostream& os, ModInt const &m) {
  os << (m.value);
  return os;
}

istream& operator>>(istream& is, ModInt& r) {
    is >> r.value;
    r.value = r.normalize(r.value);
    return is;
}

template<class T, int N>
using A = array<T, N>;

template<class T, int N, int M>
struct mat : A<A<T, N>, M> {
    using A<A<T, N>, M>::A;
    using A<A<T, N>, M>::size;
    template<class U> mat(vector<vector <U>>& a){
      for (int i = 0; i < a.size(); i++) 
        for (int j = 0; j < a[0].size(); j++)
          (*this)[i][j] = a[i][j];
    }
    void unit(int v) {
      for (int i = 0; i < min(height(), width()); i++)
        (*this)[i][i] = v;
    }
    void zero() { 
      for (int i = 0; i < height(); i++)
        for (int j = 0; j < width(); j++) 
          (*this)[i][j] = 0;
    }
    mat(int v = 0) { 
      if (v > 0) unit(v);
    }
    int height() const {
      return size();
    }
    int width() const { 
      return (*this)[0].size();
    }
    T sum() const {
      T tot = 0;
      for (int i = 0; i < height(); i++) {
        for (int j = 0; j < width(); j++) {
          tot += (*this)[i][j];
        }
      }
      return tot;
    }
    mat operator+(const mat &r) const {
      assert(height() == r.height() && width() == r.width());
      mat res = mat();
      res.zero();
      for (int i = 0; i < height(); i++) 
        for (int j = 0; j < width(); j++)
          res[i][j] = (this)[i][j] + r[i][j];
      return res;
    }
    mat operator-(const mat &r) const {
      assert(height() == r.height() && width() == r.width());
      mat res = mat();
      res.zero();        
      for (int i = 0; i < height(); i++) 
        for (int j = 0; j < width(); j++)
          res[i][j] = (this)[i][j] - r[i][j];
      return res;
    }
    mat operator*(const mat &r) const {
      assert(width() == r.height());
      mat res = mat();
      res.zero();
      for (int i = 0; i < height(); i++)
        for (int j = 0; j < r.width(); j++)
          for (int k = 0; k < width(); k++)
            res[i][j] += (*this)[i][k] * r[k][j];
      return res;
    }
    mat &operator+=( mat &r) { 
      return *this = *this + r;
    }
    mat &operator-=( mat &r) {
      return *this = *this - r;
    }
    mat &operator*=( mat &r) {
      return *this = *this * r;
    }
    mat operator^(i64 n) const {
      assert(height() == width());
      mat x = *this;
      mat r(1);
      while (n) {
          if (n & 1) r *= x;
          x *= x;
          n >>= 1;
      }
      return r;
    }
};

template<typename T, int H, int W> ostream &operator<<(ostream &os, mat<T, H, W> vec) { 
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) { 
      os << vec[i][j];
    }
    os << endl;
  }
  return os;
}

const int maxn = 21;
int n;
mat<ModInt, 50, 50> a;

int main() {
  int n;
  i64 k;
  cin >> n >> k;
  for (int i = 0; i < n; i++) 
    for (int j = 0; j < n; j++)
      cin >> a[i][j];
  cout << (a^k).sum() << endl;
}


Compiler output:
a.cc: In member function 'int ModInt::inverse(int) const':
a.cc:41:5: error: 'assert' was not declared in this scope
   41 |     assert(m == 1);
      |     ^~~~~~
a.cc:7:1: note: 'assert' is defined in header '<cassert>'; this is probably fixable by adding '#include <cassert>'
    6 | #include <array>
  +++ |+#include <cassert>
    7 | using i64 = long long;
a.cc: At global scope:
a.cc:81:33: error: expected ')' before '<' token
   81 |     template<class U> mat(vector<vector <U>>& a){
      |                          ~      ^
      |                                 )
a.cc: In instantiation of 'mat<T, N, M> mat<T, N, M>::operator^(i64) const [with T = ModInt; int N = 50; int M = 50; i64 = long long int]':
a.cc:184:14:   required from here
  184 |   cout << (a^k).sum() << endl;
      |              ^
a.cc:151:13: error: 'assert' was not declared in this scope
  151 |       assert(height() == width());
      |       ~~~~~~^~~~~~~~~~~~~~~~~~~~~
a.cc:151:13: note: 'assert' is defined in header '<cassert>'; this is probably fixable by adding '#include <cassert>'