Problem p03167 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define forloop for (int i = 0; i < n; i++)
#define pb push_back
#define ull unsigned long long
#define ll long long
#define mod 1000000007
#define MOD 998244353;
#define unmap unordered_map
#define pi 3.1415926536
#define testcases                                                              \
  ll t;                                                                        \
  cin >> t;                                                                    \
  while (t--)
#define mp make_pair
#define vcp vector<pair<ll, ll>>
#define preci(x, y) fixed << setprecision(y) << x
#define SORT_UNIQUE(c)                                                         \
  (sort(c.begin(), c.end()),                                                   \
   c.resize(distance(c.begin(), unique(c.begin(), c.end()))))
#define fast                                                                   \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define preinc priority_queue<ll, vector<ll>, greater<ll>>
#define predec priority_queue<ll>
#define vll vector<ll>
#define f(a, b) for (ll i = a; i <= b; i++)
ll path(vector<vector<char>> ch, int i, int j, vector<vector<ll>> &v, int n,
        int m) {
  if (i == n - 1 && j == m - 1)
    return 1;
  if (i >= n || j >= m)
    return 0;
  if (ch[i][j] == '#')
    return 0;
  if (v[i][j] != -1)
    return v[i][j];
  int ans = 0, ans1 = 0;
  if (i + 1 < n && ch[i + 1][j] != '#')
    ans = path(ch, i + 1, j, v, n, m) % mod;
  if (j + 1 < m && ch[i][j + 1] != '#')
    ans1 = path(ch, i, j + 1, v, n, m) % mod;
  v[i][j] = (ans + ans1) % mod;
  return v[i][j];
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("inp.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  fast int n, m;
  cin >> n >> m;
  vector<vector<char>> ch(n, vector<char>(m));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++)
      cin >> ch[i][j];
  }
  vector<vector<ll>> v(n + 1, vector<ll>(m + 1, -1));
  cout << path(ch, 0, 0, v, n, m);
  // your code goes here
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define forloop for (int i = 0; i < n; i++)
#define pb push_back
#define ull unsigned long long
#define ll long long
#define mod 1000000007
#define MOD 998244353;
#define unmap unordered_map
#define pi 3.1415926536
#define testcases                                                              \
  ll t;                                                                        \
  cin >> t;                                                                    \
  while (t--)
#define mp make_pair
#define vcp vector<pair<ll, ll>>
#define preci(x, y) fixed << setprecision(y) << x
#define SORT_UNIQUE(c)                                                         \
  (sort(c.begin(), c.end()),                                                   \
   c.resize(distance(c.begin(), unique(c.begin(), c.end()))))
#define fast                                                                   \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define preinc priority_queue<ll, vector<ll>, greater<ll>>
#define predec priority_queue<ll>
#define vll vector<ll>
#define f(a, b) for (ll i = a; i <= b; i++)
ll path(vector<vector<char>> &ch, int i, int j, vector<vector<ll>> &v, int n,
        int m) {
  if (i == n - 1 && j == m - 1)
    return 1;
  if (i >= n || j >= m)
    return 0;
  if (ch[i][j] == '#')
    return 0;
  if (v[i][j] != -1)
    return v[i][j];
  int ans = 0, ans1 = 0;
  if (i + 1 < n && ch[i + 1][j] != '#')
    ans = path(ch, i + 1, j, v, n, m) % mod;
  if (j + 1 < m && ch[i][j + 1] != '#')
    ans1 = path(ch, i, j + 1, v, n, m) % mod;
  v[i][j] = (ans + ans1) % mod;
  return v[i][j];
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("inp.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  fast int n, m;
  cin >> n >> m;
  vector<vector<char>> ch(n, vector<char>(m));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++)
      cin >> ch[i][j];
  }
  vector<vector<ll>> v(n + 1, vector<ll>(m + 1, -1));
  cout << path(ch, 0, 0, v, n, m);
  // your code goes here
  return 0;
}