Problem p03003 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using std::cerr;
using std::cin;
using std::cout;
using std::endl;

const int64_t MOD = 1000000000 + 7;

int main(void) {
  cout << std::fixed << std::setprecision(10);
  cin.tie(0);
  std::ios::sync_with_stdio(false);

  int64_t n, m;
  cin >> n >> m;
  std::vector<int64_t> s, t;
  for (int i = 0; i < n; i++) {
    int m1;
    cin >> m1;
    s.push_back(m1);
  }
  for (int i = 0; i < m; i++) {
    int m1;
    cin >> m1;
    t.push_back(m1);
  }

  int64_t dp[2001][2001] = {};
  int64_t cum[2001][2001] = {};

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (s[i] == t[j]) {
        if (i == 0 || j == 0) {
          dp[i][j] = 1;
        } else {
          dp[i][j] = 1 + cum[i - 1][j - 1];
        }
      } else {
        dp[i][j] = 0;
      }
      // assert(dp[i][j]<INT64_MAX&&0<=dp[i][j]);
      dp[i][j] %= MOD;

      if (i == 0 && j == 0) {
        cum[i][j] = dp[i][j];
      } else if (i == 0) {
        cum[i][j] = dp[i][j] + cum[i][j - 1];
      } else if (j == 0) {
        cum[i][j] = dp[i][j] + cum[i - 1][j];
      } else {
        cum[i][j] =
            cum[i][j - 1] + cum[i - 1][j] - cum[i - 1][j - 1] + dp[i][j];
      }

      // assert(cum[i][j]<INT64_MAX&&0<=cum[i][j]);
      cum[i][j] %= MOD;
    }
  }

  int64_t result = 1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      result += dp[i][j];
      result %= MOD;
    }
  }
  result %= MOD;
  assert(result < MOD && 0 <= result);
  cout << result << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using std::cerr;
using std::cin;
using std::cout;
using std::endl;

const int64_t MOD = 1000000000 + 7;

int main(void) {
  cout << std::fixed << std::setprecision(10);
  cin.tie(0);
  std::ios::sync_with_stdio(false);

  int64_t n, m;
  cin >> n >> m;
  std::vector<int64_t> s, t;
  for (int i = 0; i < n; i++) {
    int m1;
    cin >> m1;
    s.push_back(m1);
  }
  for (int i = 0; i < m; i++) {
    int m1;
    cin >> m1;
    t.push_back(m1);
  }

  int64_t dp[2001][2001] = {};
  int64_t cum[2001][2001] = {};

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (s[i] == t[j]) {
        if (i == 0 || j == 0) {
          dp[i][j] = 1;
        } else {
          dp[i][j] = 1 + cum[i - 1][j - 1];
        }
      } else {
        dp[i][j] = 0;
      }
      // assert(dp[i][j]<INT64_MAX&&0<=dp[i][j]);
      dp[i][j] %= MOD;

      if (i == 0 && j == 0) {
        cum[i][j] = dp[i][j];
      } else if (i == 0) {
        cum[i][j] = dp[i][j] + cum[i][j - 1];
      } else if (j == 0) {
        cum[i][j] = dp[i][j] + cum[i - 1][j];
      } else {
        cum[i][j] =
            cum[i][j - 1] + cum[i - 1][j] - cum[i - 1][j - 1] + dp[i][j];
      }

      // assert(cum[i][j]<INT64_MAX&&0<=cum[i][j]);
      if (cum[i][j] < 0)
        cum[i][j] += MOD;
      cum[i][j] %= MOD;
    }
  }

  int64_t result = 1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      result += dp[i][j];
      result %= MOD;
    }
  }
  result %= MOD;
  assert(result < MOD && 0 <= result);
  cout << result << endl;

  return 0;
}