Problem p03167 (C++)

Buggy submission (status: Runtime Error):
// Grid 1
// dp conteset h
#include <bits/stdc++.h>

#define C continue;
#define R return

#define D double
#define I insert
#define ll long long
#define ld long double

#define ull unsigned long long
#define ui unsigned int

#define pb push_back
#define pf push_front

#define vi vector<int>
#define vc vector<char>
#define vs vector<string>
#define vb vector<bool>
#define vd vector<D>
#define vll vector<ll>
#define vull vector<ull>
#define vld vector<ld>
#define PQ priority_queue

#define vvi vector<vector<int>>
#define vvb vector<vector<bool>>
#define vvc vector<vector<char>>
#define vvll vector<vector<ll>>
#define vvd vector<vector<D>>
#define vvld vector<vector<ld>>

#define all(v) (v).begin(), (v).end()
#define allrev(v) (v).rbegin(), (v).rend()
#define allcomp(v) v.begin(), v.end(), comp
#define allrevcomp(v) v.rbegin(), v.rend(), comp

#define pii pair<int, int>
#define pll pair<ll, ll>
#define pld pair<ld, ld>
#define pDD pair<D, D>
#define pipii pair<int, pii>
#define pcpii pair<char, pii>

#define vpld vector<pld>
#define vpii vector<pii>
#define vpll vector<pll>
#define vpDD vector<pDD>

#define vvpii vector<vector<pii>>
#define vpipii vector<pipii>

#define F first
#define S second
#define mp make_pair

#define unm unordered_map
#define unmii unm<int, int>
#define unmll unm<ll, ll>
#define unms unm<string, int>
#define unmci unm<char, int>

#define sortvia sort(a.begin(), a.end());
#define sortvib sort(b.begin(), b.end());
#define revsorta sort(a.begin(), a.end(), greater<int>());
#define revsortb sort(b.begin(), b.end(), greater<>());
#define loop(q, n) for (int i = q; i < n; i++)
#define loop2(q, n) for (int j = 1; j < n; j++)
#define test                                                                   \
  int t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
#define nextline "\n"
#define tab "\t"
#define space " "
// vector<vector<int> > vec( n , vector<int> (m, 0));
// YES
// NO
// cout
// true
// false
// yes
// no

const ll mod9 = 1e9 + 7;
const ll maxsize = 2e9 + 1;
// const ll mod =998244353;
const ll mod2 = 1073741824;
const ll INF = 1e18L + 5;
const int two_pow_fiv = 200008;
using namespace std;

int n, m;
vector<string> grid;
ll dp[1001][1001];
bool allowed(int i, int j) {
  if (i >= n or j >= m)
    return false;
  else if (grid[i][j] == '#')
    return false;
  return true;
}

int dfs(int a, int b) {
  if (dp[a][b] > -1)
    return dp[a][b];
  // if(a==n-1 and b=m-1)return dp[n-1][m-1];
  // first we try with dp if not then we perform dp
  dp[a][b] = (dfs(a + 1, b) % mod9 + dfs(a, b + 1) % mod9) % mod9;
  return dp[a][b];
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  cin >> n >> m;
  grid.resize(n + 1);
  string inp;
  loop(0, n) { cin >> grid[i]; }
  for (int i = 0; i <= n + 1; i++) {
    for (int j = 0; j <= m + 1; j++) {
      if (!allowed(i, j))
        dp[i][j] = 0;
      else
        dp[i][j] = -1;
    }
  }
  dp[n - 1][m - 1] = 1;
  cout << dfs(0, 0);

  return 0;
}

Fixed submission:
// Grid 1
// dp conteset h
#include <bits/stdc++.h>

#define C continue;
#define R return

#define D double
#define I insert
#define ll long long
#define ld long double

#define ull unsigned long long
#define ui unsigned int

#define pb push_back
#define pf push_front

#define vi vector<int>
#define vc vector<char>
#define vs vector<string>
#define vb vector<bool>
#define vd vector<D>
#define vll vector<ll>
#define vull vector<ull>
#define vld vector<ld>
#define PQ priority_queue

#define vvi vector<vector<int>>
#define vvb vector<vector<bool>>
#define vvc vector<vector<char>>
#define vvll vector<vector<ll>>
#define vvd vector<vector<D>>
#define vvld vector<vector<ld>>

#define all(v) (v).begin(), (v).end()
#define allrev(v) (v).rbegin(), (v).rend()
#define allcomp(v) v.begin(), v.end(), comp
#define allrevcomp(v) v.rbegin(), v.rend(), comp

#define pii pair<int, int>
#define pll pair<ll, ll>
#define pld pair<ld, ld>
#define pDD pair<D, D>
#define pipii pair<int, pii>
#define pcpii pair<char, pii>

#define vpld vector<pld>
#define vpii vector<pii>
#define vpll vector<pll>
#define vpDD vector<pDD>

#define vvpii vector<vector<pii>>
#define vpipii vector<pipii>

#define F first
#define S second
#define mp make_pair

#define unm unordered_map
#define unmii unm<int, int>
#define unmll unm<ll, ll>
#define unms unm<string, int>
#define unmci unm<char, int>

#define sortvia sort(a.begin(), a.end());
#define sortvib sort(b.begin(), b.end());
#define revsorta sort(a.begin(), a.end(), greater<int>());
#define revsortb sort(b.begin(), b.end(), greater<>());
#define loop(q, n) for (int i = q; i < n; i++)
#define loop2(q, n) for (int j = 1; j < n; j++)
#define test                                                                   \
  int t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
#define nextline "\n"
#define tab "\t"
#define space " "
// vector<vector<int> > vec( n , vector<int> (m, 0));
// YES
// NO
// cout
// true
// false
// yes
// no

const ll mod9 = 1e9 + 7;
const ll maxsize = 2e9 + 1;
// const ll mod =998244353;
const ll mod2 = 1073741824;
const ll INF = 1e18L + 5;
const int two_pow_fiv = 200008;
using namespace std;

int n, m;
vector<string> grid;
ll dp[1005][1005];
bool allowed(int i, int j) {
  if (i >= n or j >= m)
    return false;
  else if (grid[i][j] == '#')
    return false;
  return true;
}

int dfs(int a, int b) {
  if (dp[a][b] > -1)
    return dp[a][b];
  // if(a==n-1 and b=m-1)return dp[n-1][m-1];
  // first we try with dp if not then we perform dp
  dp[a][b] = (dfs(a + 1, b) % mod9 + dfs(a, b + 1) % mod9) % mod9;
  return dp[a][b];
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  cin >> n >> m;
  grid.resize(n + 1);
  string inp;
  loop(0, n) { cin >> grid[i]; }
  for (int i = 0; i <= n + 1; i++) {
    for (int j = 0; j <= m + 1; j++) {
      if (!allowed(i, j))
        dp[i][j] = 0;
      else
        dp[i][j] = -1;
    }
  }
  dp[n - 1][m - 1] = 1;
  cout << dfs(0, 0);

  return 0;
}