Problem p03003 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <deque>
#include <fstream>
#include <functional>
#include <initializer_list>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
constexpr long long int MOD = 1000000007LL;
long long int search(const int s, const int t, const std::vector<int> &s_list,
                     const std::vector<int> &t_list,
                     std::vector<std::vector<long long int>> &memo) {
  if (s >= s_list.size() || t >= t_list.size())
    return 1;
  else if (memo[s][t] >= 0)
    return memo[s][t];
  else if (s_list[s] == t_list[t])
    return memo[s][t] = (search(s + 1, t, s_list, t_list, memo) +
                         search(s, t + 1, s_list, t_list, memo)) %
                        MOD;
  else
    return memo[s][t] = (search(s + 1, t, s_list, t_list, memo) +
                         search(s, t + 1, s_list, t_list, memo) -
                         search(s + 1, t + 1, s_list, t_list, memo)) %
                        MOD;
}
int main() {
  int n, m;
  std::cin >> n >> m;
  std::vector<int> s_list(n), t_list(m);
  for (auto &s : s_list)
    std::cin >> s;
  for (auto &t : t_list)
    std::cin >> t;
  std::vector<std::vector<long long int>> memo(
      n, std::vector<long long int>(m, -1));
  std::cout << search(0, 0, s_list, t_list, memo) << std::endl;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <deque>
#include <fstream>
#include <functional>
#include <initializer_list>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
constexpr long long int MOD = 1000000007LL;
long long int search(const int s, const int t, const std::vector<int> &s_list,
                     const std::vector<int> &t_list,
                     std::vector<std::vector<long long int>> &memo) {
  if (s >= s_list.size() || t >= t_list.size())
    return 1;
  else if (memo[s][t] >= 0)
    return memo[s][t];
  else if (s_list[s] == t_list[t])
    return memo[s][t] = (search(s + 1, t, s_list, t_list, memo) +
                         search(s, t + 1, s_list, t_list, memo)) %
                        MOD;
  else
    return memo[s][t] = (search(s + 1, t, s_list, t_list, memo) +
                         search(s, t + 1, s_list, t_list, memo) -
                         search(s + 1, t + 1, s_list, t_list, memo)) %
                        MOD;
}
int main() {
  int n, m;
  std::cin >> n >> m;
  std::vector<int> s_list(n), t_list(m);
  for (auto &s : s_list)
    std::cin >> s;
  for (auto &t : t_list)
    std::cin >> t;
  std::vector<std::vector<long long int>> memo(
      n + 1, std::vector<long long int>(m + 1, -1));
  for (auto i = 0; i <= m; ++i) {
    memo[n][i] = 1;
  }
  for (auto i = 0; i <= n; ++i) {
    memo[i][m] = 1;
  }
  for (int i = n - 1; i >= 0; --i) {
    for (int j = m - 1; j >= 0; --j) {
      if (s_list[i] == t_list[j]) {
        memo[i][j] = (memo[i + 1][j] + memo[i][j + 1]) % MOD;
      } else {
        memo[i][j] =
            (memo[i + 1][j] + memo[i][j + 1] - memo[i + 1][j + 1] + MOD) % MOD;
      }
    }
  }
  std::cout << memo[0][0] << std::endl;
}