Problem p03177 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

// typedef
typedef long long LL;
typedef vector<LL> VL;
typedef vector<VL> VVL;
typedef vector<string> VS;
typedef pair<LL, LL> PLL;

// conversion
inline LL toLong(string s) {
  LL v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

// math
//-------------------------------------------
template <class T> inline T sqr(T x) { return x * x; }

// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) LL((a).size())
#define EACH(i, c) for (auto i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

// repetition
#define FOR(i, a, b) for (LL i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define UNTIL(p) while (!(p))

// constant
const double EPS = 1e-5;
const double PI = acos(-1.0);

// debug
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \
       << " " << __FILE__ << endl;

#define PUTS(x) cout << (x) << endl;

const LL MOD = 1e9 + 7;
const LL M = 100;
LL n;
vector<vector<LL>> link;

LL dp[50][50][M] = {0};
map<LL, LL> memo[50][50];

LL f(LL i, LL j, LL k) {
  if (k < M) {
    return dp[i][j][k];
  }
  if (EXIST(memo[i][j], k)) {
    return memo[i][j][k];
  }
  LL x = k / 2;
  LL y = k - x;
  LL sum = 0;
  REP(t, n) {
    sum += (f(i, t, x) * f(t, j, y)) % MOD;
    sum %= MOD;
  }
  memo[i][j].insert(MP(k, sum));
  return sum;
}

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  LL k;
  cin >> n >> k;
  link.resize(n);
  REP(i, n) {
    REP(j, n) {
      LL a;
      cin >> a;
      if (a == 1)
        link[i].PB(j);
    }
  }

  REP(i, n) dp[i][i][0] = 1;
  REP(len, M) {
    REP(i, n) {
      for (LL to : link[i]) {
        REP(st, n) {
          dp[st][to][len + 1] += dp[st][i][len];
          dp[st][to][len + 1] %= MOD;
        }
      }
    }
  }

  LL ans = 0;
  REP(i, n) REP(j, n) {
    ans += f(i, j, k);
    ans %= MOD;
  }
  PUTS(ans);
}

Fixed submission:
#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

// typedef
typedef long long LL;
typedef vector<LL> VL;
typedef vector<VL> VVL;
typedef vector<string> VS;
typedef pair<LL, LL> PLL;

// conversion
inline LL toLong(string s) {
  LL v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

// math
//-------------------------------------------
template <class T> inline T sqr(T x) { return x * x; }

// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) LL((a).size())
#define EACH(i, c) for (auto i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

// repetition
#define FOR(i, a, b) for (LL i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define UNTIL(p) while (!(p))

// constant
const double EPS = 1e-5;
const double PI = acos(-1.0);

// debug
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \
       << " " << __FILE__ << endl;

#define PUTS(x) cout << (x) << endl;

const LL MOD = 1e9 + 7;
const LL M = 100;
LL n;
vector<vector<LL>> link;

LL dp[50][50][M + 1] = {0};
map<LL, LL> memo[50][50];

LL f(LL i, LL j, LL k) {
  if (k < M) {
    return dp[i][j][k];
  }
  if (EXIST(memo[i][j], k)) {
    return memo[i][j][k];
  }
  LL x = k / 2;
  LL y = k - x;
  LL sum = 0;
  REP(t, n) {
    sum += (f(i, t, x) * f(t, j, y)) % MOD;
    sum %= MOD;
  }
  memo[i][j].insert(MP(k, sum));
  return sum;
}

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  LL k;
  cin >> n >> k;
  link.resize(n);
  REP(i, n) {
    REP(j, n) {
      LL a;
      cin >> a;
      if (a == 1)
        link[i].PB(j);
    }
  }

  REP(i, n) dp[i][i][0] = 1;
  REP(len, M) {
    REP(i, n) {
      for (LL to : link[i]) {
        REP(st, n) {
          dp[st][to][len + 1] += dp[st][i][len];
          dp[st][to][len + 1] %= MOD;
        }
      }
    }
  }

  LL ans = 0;
  REP(i, n) REP(j, n) {
    ans += f(i, j, k);
    ans %= MOD;
  }
  PUTS(ans);
}