Problem p03003 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define INF 1 << 30
#define endl '\n'
#define maxn 2048
#define FASTIO ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
typedef long long ll;
typedef long double ld;
const double PI = acos(-1.0);
#define dbg(x) cerr << #x << " = " << x << endl;
#define dbg2(x, y) cerr << #x << " = " << x << ", " << #y << " = " << y << endl;
#define dbg3(x, y, z)                                                          \
  cerr << #x << " = " << x << ", " << #y << " = " << y << ", " << #z << " = "  \
       << z << endl;

const ll mod = 1e9 + 7;
ll X[maxn], Y[maxn];
ll dp[maxn][maxn];
ll n, m;
ll fact[maxn], inv[maxn], finv[maxn];

void pre_calc() {
  fact[0] = fact[1] = 1;
  inv[1] = 1;
  finv[0] = finv[1] = 1;

  for (int i = 2; i < maxn; i++) {
    fact[i] = i * fact[i - 1] % mod;
    inv[i] = mod - mod / i * inv[mod % i] % mod;
    finv[i] = inv[i] * finv[i - 1] % mod;
  }
}

ll calc(ll n, ll k) {
  if (n < k || n < 0)
    return 0;
  return fact[n] * finv[n - k] % mod * finv[k] % mod;
}

ll solve(int i, int j) {
  if (i == n || j == m)
    return 0;
  if (dp[i][j] > 0)
    return dp[i][j];
  if (X[i] == Y[j])
    dp[i][j] = (1 + solve(i + 1, j) + solve(i, j + 1)) % mod;
  else
    dp[i][j] =
        (solve(i + 1, j) + solve(i, j + 1) - solve(i + 1, j + 1) + mod) % mod;
  return dp[i][j];
}

int main() {
  FASTIO
  /*
  //double start_time = clock();
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
  freopen("error.txt", "w", stderr);
#endif
//*/
  cin >> n >> m;
  for (ll i = 0; i < n; i++)
    cin >> X[i];

  for (ll i = 0; i < m; i++)
    cin >> Y[i];

  memset(dp, -1, sizeof dp);
  cout << (solve(0, 0) + 1) % mod << endl;
  // double end_time = clock();
  // prllf( "Time = %lf ms\n", ( (end_time - start_time) /
  // CLOCKS_PER_SEC)*1000);

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define INF 1 << 30
#define endl '\n'
#define maxn 2048
#define FASTIO ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
typedef long long ll;
typedef long double ld;
const double PI = acos(-1.0);
#define dbg(x) cerr << #x << " = " << x << endl;
#define dbg2(x, y) cerr << #x << " = " << x << ", " << #y << " = " << y << endl;
#define dbg3(x, y, z)                                                          \
  cerr << #x << " = " << x << ", " << #y << " = " << y << ", " << #z << " = "  \
       << z << endl;

const ll mod = 1e9 + 7;
ll X[maxn], Y[maxn];
ll dp[maxn][maxn];
ll n, m;
ll fact[maxn], inv[maxn], finv[maxn];

void pre_calc() {
  fact[0] = fact[1] = 1;
  inv[1] = 1;
  finv[0] = finv[1] = 1;

  for (int i = 2; i < maxn; i++) {
    fact[i] = i * fact[i - 1] % mod;
    inv[i] = mod - mod / i * inv[mod % i] % mod;
    finv[i] = inv[i] * finv[i - 1] % mod;
  }
}

ll calc(ll n, ll k) {
  if (n < k || n < 0)
    return 0;
  return fact[n] * finv[n - k] % mod * finv[k] % mod;
}

ll solve(int i, int j) {
  if (i == n || j == m)
    return 0;
  if (dp[i][j] >= 0)
    return dp[i][j];
  if (X[i] == Y[j])
    dp[i][j] = (1 + solve(i + 1, j) + solve(i, j + 1)) % mod;
  else
    dp[i][j] =
        (solve(i + 1, j) + solve(i, j + 1) - solve(i + 1, j + 1) + mod) % mod;
  return dp[i][j];
}

int main() {
  FASTIO
  /*
  //double start_time = clock();
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
  freopen("error.txt", "w", stderr);
#endif
//*/
  cin >> n >> m;
  for (ll i = 0; i < n; i++)
    cin >> X[i];

  for (ll i = 0; i < m; i++)
    cin >> Y[i];

  memset(dp, -1, sizeof dp);
  cout << (solve(0, 0) + 1) % mod << endl;
  // double end_time = clock();
  // prllf( "Time = %lf ms\n", ( (end_time - start_time) /
  // CLOCKS_PER_SEC)*1000);

  return 0;
}