Problem p03167 (C++)

Buggy submission (status: Runtime Error):
//<============ Author --> @venom ============>//
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
using namespace __gnu_pbds;

#define fo(i, n) for (int i = 0; i < n; i++)
#define int long long
#define pb emplace_back
#define mp make_pair
#define ff first
#define ss second
#define all(x) x.begin(), x.end()
#define sortall(x) sort(all(x))
#define sortrev(x) sort(all(x), greater<int>())
#define w(x)                                                                   \
  int x;                                                                       \
  cin >> x;                                                                    \
  while (x--)
#define trunc(x, y) fixed << setprecision(y) << x
#define mk(arr, n, type) type *arr = new type[n];
#define PI 3.1415926535897932384626
#define mod 1000000007
#define inf 1e9 + 5
#define FIO                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
typedef pair<int, int> pi;
typedef vector<int> vi;
typedef vector<pi> vpi;
typedef vector<vi> vvi;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set;
const int N = 3e5, M = N;
int mpow(int base, int exp);
int gcd(int a, int b) {
  if (a == 0)
    return b;
  return gcd(b % a, a);
}
//<----------------------------------------------------------------------------->//
void fastIO() {
  FIO;
#ifndef ONLINE_JUDGE
  // for getting input from input.txt
  freopen("input.txt", "r", stdin);
  // for writing output to output.txt
  freopen("output.txt", "w", stdout);
#endif
}

char grid[1002][1002];

int solve(int n, int m) {
  int dp[n + 1][m + 1];

  dp[n][m] = 1;

  for (int i = n; i >= 1; i--) {
    for (int j = m; j >= 1; j--) {
      if (i == n && j == m)
        continue;

      if (grid[i][j] == '#')
        dp[i][j] = 0;

      else
        dp[i][j] =
            (((i == n) ? 0 : dp[i + 1][j]) + ((j == m) ? 0 : dp[i][j + 1])) %
            mod;
    }
  }

  return dp[1][1];
}

int32_t main() {
  fastIO();

  int n, m;
  cin >> n >> m;

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> grid[i][j];
    }
  }

  cout << solve(n, m);

  return 0;
}

int mpow(int base, int exp) {
  base %= mod;
  int result = 1;
  while (exp > 0) {
    if (exp & 1)
      result = ((int)result * base) % mod;
    base = ((int)base * base) % mod;
    exp >>= 1;
  }
  return result;
}

Fixed submission:
//<============ Author --> @venom ============>//
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
using namespace __gnu_pbds;

#define fo(i, n) for (int i = 0; i < n; i++)
#define int long long
#define pb emplace_back
#define mp make_pair
#define ff first
#define ss second
#define all(x) x.begin(), x.end()
#define sortall(x) sort(all(x))
#define sortrev(x) sort(all(x), greater<int>())
#define w(x)                                                                   \
  int x;                                                                       \
  cin >> x;                                                                    \
  while (x--)
#define trunc(x, y) fixed << setprecision(y) << x
#define mk(arr, n, type) type *arr = new type[n];
#define PI 3.1415926535897932384626
#define mod 1000000007
#define inf 1e9 + 5
#define FIO                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
typedef pair<int, int> pi;
typedef vector<int> vi;
typedef vector<pi> vpi;
typedef vector<vi> vvi;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set;
const int N = 3e5, M = N;
int mpow(int base, int exp);
int gcd(int a, int b) {
  if (a == 0)
    return b;
  return gcd(b % a, a);
}
//<----------------------------------------------------------------------------->//
void fastIO() {
  FIO;
#ifndef ONLINE_JUDGE
  // for getting input from input.txt
  freopen("input.txt", "r", stdin);
  // for writing output to output.txt
  freopen("output.txt", "w", stdout);
#endif
}

char grid[1002][1002];

int solve(int n, int m) {
  int dp[n + 1][m + 1];

  dp[n][m] = 1;

  for (int i = n; i >= 1; i--) {
    for (int j = m; j >= 1; j--) {
      if (i == n && j == m)
        continue;

      if (grid[i][j] == '#')
        dp[i][j] = 0;

      else
        dp[i][j] =
            (((i == n) ? 0 : dp[i + 1][j]) + ((j == m) ? 0 : dp[i][j + 1])) %
            mod;
    }
  }

  return dp[1][1];
}

int32_t main() {
  FIO;

  int n, m;
  cin >> n >> m;

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> grid[i][j];
    }
  }

  cout << solve(n, m);

  return 0;
}

int mpow(int base, int exp) {
  base %= mod;
  int result = 1;
  while (exp > 0) {
    if (exp & 1)
      result = ((int)result * base) % mod;
    base = ((int)base * base) % mod;
    exp >>= 1;
  }
  return result;
}