Problem p03167 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define ll long long
#define F(i, a, b) for (i = a; i <= b; i++)
#define RF(i, a, b) for (i = a; i >= b; i--)
#define sz(a) sizeof(a)
#define next_line cout << '\n'
#define space cout << ' '
#define deb(a) cout << #a << ' ' << a
using namespace std;

typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<char, int> pci;
typedef map<int, int> mii;
typedef map<ll, ll> mll;
typedef map<char, int> mci;
typedef unordered_map<int, int> umii;
typedef unordered_map<ll, ll> umll;
typedef unordered_map<char, int> umci;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;
ll mod = 1e9 + 7LL;
vector<ll> dp(1000001, -1LL);

ll n, m, i, j;
ll NumberOfWays(ll st_i, ll st_j, vector<vector<char>> Grid) {
  if ((st_i >= n) || (st_j >= m))
    return 0LL;
  ll indx = (st_i * m + st_j);
  if (dp[indx] != -1LL)
    return dp[indx];
  if (Grid[st_i][st_j] == '#')
    return 0LL;

  if ((st_i == (n - 1LL)) && (st_j == (m - 1LL)))
    return 1LL;
  // deb(indx);space;deb(dp[indx]);
  // next_line;
  dp[indx] = (NumberOfWays(st_i, st_j + 1LL, Grid) +
              NumberOfWays(st_i + 1LL, st_j, Grid)) %
             mod;
  return dp[indx];
}

void solve() {
  cin >> n >> m;
  vector<vector<char>> Grid(n, vector<char>(m));
  F(i, 0LL, n - 1LL) {
    F(j, 0LL, m - 1LL) { cin >> Grid[i][j]; }
  }
  cout << NumberOfWays(0LL, 0LL, Grid) << '\n';
}

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  // #ifndef ONLINE_JUDGE
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  // #else
  // #endif
  solve();
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define F(i, a, b) for (i = a; i <= b; i++)
#define RF(i, a, b) for (i = a; i >= b; i--)
#define sz(a) sizeof(a)
#define next_line cout << '\n'
#define space cout << ' '
#define deb(a) cout << #a << ' ' << a
using namespace std;

typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<char, int> pci;
typedef map<int, int> mii;
typedef map<ll, ll> mll;
typedef map<char, int> mci;
typedef unordered_map<int, int> umii;
typedef unordered_map<ll, ll> umll;
typedef unordered_map<char, int> umci;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;
ll mod = 1e9 + 7LL;
vector<ll> dp(1000001, -1LL);

ll n, m, i, j;
ll NumberOfWays(ll st_i, ll st_j, vector<vector<char>> &Grid) {
  if ((st_i >= n) || (st_j >= m))
    return 0LL;
  ll indx = (st_i * m + st_j);
  if (dp[indx] != -1LL)
    return dp[indx];
  if (Grid[st_i][st_j] == '#')
    return 0LL;

  if ((st_i == (n - 1LL)) && (st_j == (m - 1LL)))
    return 1LL;
  // deb(indx);space;deb(dp[indx]);
  // next_line;
  dp[indx] = (NumberOfWays(st_i, st_j + 1LL, Grid) +
              NumberOfWays(st_i + 1LL, st_j, Grid)) %
             mod;
  return dp[indx];
}

void solve() {
  cin >> n >> m;
  vector<vector<char>> Grid(n, vector<char>(m));
  F(i, 0LL, n - 1LL) {
    F(j, 0LL, m - 1LL) { cin >> Grid[i][j]; }
  }
  cout << NumberOfWays(0LL, 0LL, Grid) << '\n';
}

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  // #ifndef ONLINE_JUDGE
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  // #else
  // #endif
  solve();
}