Problem p03177 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define SPEED                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define FOR(i, a, b) for (ll i = a; i < b; ++i)
#define RFOR(i, b, a) for (ll i = b; i >= a; --i)
#define ALL(x) x.begin(), x.end()
#define DEBUG(args...)                                                         \
  {                                                                            \
    string _s = #args;                                                         \
    replace(ALL(_s), ' ', '\0');                                               \
    replace(ALL(_s), ',', ' ');                                                \
    stringstream _ss(_s);                                                      \
    istream_iterator<string> _it(_ss);                                         \
    _debug(_it, args);                                                         \
  }
#define endl "\n"
#define F first
#define S second
#define pb(x) push_back(x)
#define mp(x, y) make_pair(x, y)

using namespace std;
using namespace __gnu_pbds;

template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

void _debug(istream_iterator<string>) {}
template <typename T, typename... Args>
void _debug(istream_iterator<string> it, T first, Args... args) {
  cerr << ">> " << *it << " : " << first << endl;
  _debug(++it, args...);
}
template <typename T1, typename T2>
inline ostream &operator<<(ostream &out, const pair<T1, T2> &p) {
  return out << "(" << p.F << ", " << p.S << ")";
}
template <typename T>
inline ostream &operator<<(ostream &out, const vector<T> &v) {
  if (v.empty())
    return out << "[]";
  else {
    out << '[';
    for (auto &e : v) {
      out << e << ", ";
    }
    return out << "\b\b]";
  }
}
template <typename T>
inline ostream &operator<<(ostream &out, const set<T> &s) {
  if (s.empty())
    return out << "{}";
  else {
    out << '{';
    for (auto &e : s) {
      out << e << ", ";
    }
    return out << "\b\b}";
  }
}
template <typename T>
inline ostream &operator<<(ostream &out, const unordered_set<T> &s) {
  return out << set<T>(ALL(s));
}
template <typename T1, typename T2>
inline ostream &operator<<(ostream &out, const map<T1, T2> &m) {
  if (m.empty())
    return out << "{}";
  out << '{';
  for (auto &p : m) {
    out << p << ", ";
  }
  return out << "\b\b}";
}
template <typename T1, typename T2>
inline ostream &operator<<(ostream &out, const unordered_map<T1, T2> &m) {
  return out << map<T1, T2>(ALL(m));
}
template <typename T>
inline ostream &operator<<(ostream &out, const ordered_set<T> &s) {
  return out << set<T>(ALL(s));
}

typedef long long ll;
typedef long double ld;
typedef vector<long long> vll;
typedef pair<ll, ll> pll;
typedef vector<pair<ll, ll>> vpll;
typedef unordered_map<ll, ll> STll;
/************************************** MAIN PROGRAM
 * ********************************************/
const int MAX = 51;
bool a[MAX][MAX];
const int MOD = 1e9 + 7;
ll n, k;
map<ll, ll> cache[MAX][MAX];

ll mul(ll a, ll b) { return (a % MOD) * (b % MOD) % MOD; }

ll ans(int i, int j, ll k) {
  if (cache[i][j].count(k)) {
    return cache[i][j][k];
  }
  if (k == 1) {
    return a[i][j];
  }
  ll val = 0;
  FOR(r, 0, n) {
    val += mul(ans(i, r, k / 2), ans(r, j, (k + 1) / 2));
    if (val >= MOD) {
      val -= MOD;
    }
  }
  return cache[i][j][k] = val;
}

int main() {
  // freopen("input.txt", "r", stdin);
  SPEED

  cin >> n >> k;
  FOR(i, 0, n) {
    FOR(j, 0, n) { cin >> a[i][j]; }
  }
  ll result = 0;
  FOR(i, 0, n) {
    FOR(j, 0, n) {
      result += ans(i, j, k);
      if (result >= MOD) {
        result -= MOD;
      }
    }
  }
  cout << result;
}
/************************************** END OF PROGRAM
 * ******************************************/


Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define SPEED                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define FOR(i, a, b) for (ll i = a; i < b; ++i)
#define RFOR(i, b, a) for (ll i = b; i >= a; --i)
#define ALL(x) x.begin(), x.end()
#define DEBUG(args...)                                                         \
  {                                                                            \
    string _s = #args;                                                         \
    replace(ALL(_s), ' ', '\0');                                               \
    replace(ALL(_s), ',', ' ');                                                \
    stringstream _ss(_s);                                                      \
    istream_iterator<string> _it(_ss);                                         \
    _debug(_it, args);                                                         \
  }
#define endl "\n"
#define F first
#define S second
#define pb(x) push_back(x)
#define mp(x, y) make_pair(x, y)

using namespace std;
using namespace __gnu_pbds;

template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

void _debug(istream_iterator<string>) {}
template <typename T, typename... Args>
void _debug(istream_iterator<string> it, T first, Args... args) {
  cerr << ">> " << *it << " : " << first << endl;
  _debug(++it, args...);
}
template <typename T1, typename T2>
inline ostream &operator<<(ostream &out, const pair<T1, T2> &p) {
  return out << "(" << p.F << ", " << p.S << ")";
}
template <typename T>
inline ostream &operator<<(ostream &out, const vector<T> &v) {
  if (v.empty())
    return out << "[]";
  else {
    out << '[';
    for (auto &e : v) {
      out << e << ", ";
    }
    return out << "\b\b]";
  }
}
template <typename T>
inline ostream &operator<<(ostream &out, const set<T> &s) {
  if (s.empty())
    return out << "{}";
  else {
    out << '{';
    for (auto &e : s) {
      out << e << ", ";
    }
    return out << "\b\b}";
  }
}
template <typename T>
inline ostream &operator<<(ostream &out, const unordered_set<T> &s) {
  return out << set<T>(ALL(s));
}
template <typename T1, typename T2>
inline ostream &operator<<(ostream &out, const map<T1, T2> &m) {
  if (m.empty())
    return out << "{}";
  out << '{';
  for (auto &p : m) {
    out << p << ", ";
  }
  return out << "\b\b}";
}
template <typename T1, typename T2>
inline ostream &operator<<(ostream &out, const unordered_map<T1, T2> &m) {
  return out << map<T1, T2>(ALL(m));
}
template <typename T>
inline ostream &operator<<(ostream &out, const ordered_set<T> &s) {
  return out << set<T>(ALL(s));
}

typedef long long ll;
typedef long double ld;
typedef vector<long long> vll;
typedef pair<ll, ll> pll;
typedef vector<pair<ll, ll>> vpll;
typedef unordered_map<ll, ll> STll;
/************************************** MAIN PROGRAM
 * ********************************************/
const int MAX = 51;
bool a[MAX][MAX];
const int MOD = 1e9 + 7;
ll n, k;
map<ll, ll> cache[MAX][MAX];

ll mul(ll a, ll b) { return (a % MOD) * (b % MOD) % MOD; }

ll ans(int i, int j, ll k) {
  if (cache[i][j].count(k)) {
    return cache[i][j][k];
  }
  if (k == 0) {
    return 0;
  }
  if (k == 1) {
    return a[i][j];
  }
  ll val = 0;
  FOR(r, 0, n) {
    val += mul(ans(i, r, k / 2), ans(r, j, (k + 1) / 2));
    if (val >= MOD) {
      val -= MOD;
    }
  }
  return cache[i][j][k] = val;
}

int main() {
  // freopen("input.txt", "r", stdin);
  SPEED

  cin >> n >> k;
  FOR(i, 0, n) {
    FOR(j, 0, n) { cin >> a[i][j]; }
  }
  ll result = 0;
  FOR(i, 0, n) {
    FOR(j, 0, n) {
      result += ans(i, j, k);
      if (result >= MOD) {
        result -= MOD;
      }
    }
  }
  cout << result;
}
/************************************** END OF PROGRAM
 * ******************************************/