Problem p03167 (C++)

Buggy submission (status: Time Limit Exceeded):
// --------------------<optimizations>--------------------
#pragma GCC optimize("O3")

//(UNCOMMENT WHEN HAVING LOTS OF RECURSIONS)\
#pragma comment(linker, "/stack:200000000")
//(UNCOMMENT WHEN TRYING TO BRUTEFORCE WITH A LOT OF LOOPS)\
#pragma GCC optimize("unroll-loops")

// -------------------</optimizations>--------------------
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define d long double
#define pii pair<int, int>
#define pb push_back
#define pf push_front
#define cases                                                                  \
  int tc;                                                                      \
  cin >> tc;                                                                   \
  while (tc--)
#define N 100003
#define M 1003
#define inf LLONG_MAX
#define hahaha                                                                 \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
int mod = 1e9 + 7;
// int mod=998244353;
char a[M][M];
int n, m, ans = 0, dp[M][M];

int recur(int i, int j, int &cnt) {
  if (dp[i][j])
    return dp[i][j];

  if (i == n and j == m) {
    cnt = (cnt + 1) % mod;
    return dp[i][j] = cnt;
  }
  if (a[i][j + 1] == '.')
    dp[i][j] = (dp[i][j] + recur(i, j + 1, cnt)) % mod;
  if (a[i + 1][j] == '.')
    dp[i][j] = (dp[i][j] + recur(i + 1, j, cnt)) % mod;

  return dp[i][j];
}

signed main() {
  hahaha

      memset(dp, 0, sizeof dp);
  cin >> n >> m;
  int i, j, k;
  for (i = 1; i <= n; ++i)
    for (j = 1; j <= m; ++j)
      cin >> a[i][j];

  int cnt = 0;
  cout << recur(1, 1, cnt);

  return 0;
}


Fixed submission:
// --------------------<optimizations>--------------------
#pragma GCC optimize("O3")

//(UNCOMMENT WHEN HAVING LOTS OF RECURSIONS)\
#pragma comment(linker, "/stack:200000000")
//(UNCOMMENT WHEN TRYING TO BRUTEFORCE WITH A LOT OF LOOPS)\
#pragma GCC optimize("unroll-loops")

// -------------------</optimizations>--------------------
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define d long double
#define pii pair<int, int>
#define pb push_back
#define pf push_front
#define cases                                                                  \
  int tc;                                                                      \
  cin >> tc;                                                                   \
  while (tc--)
#define N 100003
#define M 1003
#define inf LLONG_MAX
#define hahaha                                                                 \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
int mod = 1e9 + 7;
// int mod=998244353;
char a[M][M];
int n, m, ans = 0, dp[M][M];

int recur(int i, int j, int &cnt) {
  if (dp[i][j])
    return dp[i][j];

  if (i == n and j == m) {
    cnt = (cnt + 1) % mod;
    return dp[i][j] = cnt;
  }
  if (a[i][j + 1] == '.')
    dp[i][j] = (dp[i][j] + recur(i, j + 1, cnt)) % mod;
  if (a[i + 1][j] == '.')
    dp[i][j] = (dp[i][j] + recur(i + 1, j, cnt)) % mod;

  return dp[i][j];
}

signed main() {
  hahaha

      memset(dp, 0, sizeof dp);
  cin >> n >> m;
  int i, j, k;
  for (i = 1; i <= n; ++i)
    for (j = 1; j <= m; ++j)
      cin >> a[i][j];

  dp[1][1] = 1;
  for (i = 1; i <= n; ++i)
    for (j = 1; j <= m; ++j)
      if (a[i][j] == '.')
        dp[i][j] = (dp[i][j] + dp[i - 1][j]) % mod,
        dp[i][j] = (dp[i][j] + dp[i][j - 1]) % mod;

  cout << dp[n][m];
  // int cnt=0;
  // cout<<recur(1,1,cnt);

  return 0;
}