Problem p03167 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define INF 1e9
#define MAX 105
#define xx first
#define yy second
#define pb push_back
#define mp make_pair
#define ull long long
#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define nl '\n'
#define zai << ' ' <<
#define all(a) a.begin(), a.end()
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<ii> vii;

const int MOD = 1000000007;

bool a[MAX][MAX];
int dp[MAX][MAX];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  char tmp;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> tmp;
      if (tmp == '#')
        a[i][j] = 1;
    }
  }
  if (!a[0][0])
    dp[0][0] = 1;
  else {
    cout << 0;
    return 0;
  }
  for (int i = 1; i < m; i++) {
    if (!a[0][i] && dp[0][i - 1] != INF)
      dp[0][i] = 1;
    else
      dp[0][i] = INF;
  }
  for (int i = 1; i < n; i++) {
    if (!a[i][0] && dp[i - 1][0] != INF)
      dp[i][0] = 1;
    else
      dp[i][0] = INF;
  }
  for (int i = 1; i < n; i++) {
    for (int j = 1; j < m; j++) {
      if (a[i][j] == 0) {
        if (dp[i - 1][j] == INF && dp[i][j - 1] == INF) {
          dp[i][j] = INF;
        } else {
          if (dp[i - 1][j] == INF && dp[i][j - 1] != INF) {
            dp[i][j] = (dp[i][j - 1]) % MOD;
          } else if (dp[i - 1][j] != INF && dp[i][j - 1] == INF) {
            dp[i][j] = (dp[i - 1][j]) % MOD;
          } else if (dp[i - 1][j] != INF && dp[i][j - 1] != INF) {
            dp[i][j] = (dp[i - 1][j] + dp[i][j - 1]) % MOD;
          }
        }
      } else {
        dp[i][j] = INF;
      }
    }
  }
  if (dp[n - 1][m - 1] != INF)
    cout << dp[n - 1][m - 1];
  else
    cout << 0;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define INF 1e9
#define MAX 1005
#define xx first
#define yy second
#define pb push_back
#define mp make_pair
#define ull long long
#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define nl '\n'
#define zai << ' ' <<
#define all(a) a.begin(), a.end()
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<ii> vii;

const int MOD = 1000000007;

bool a[MAX][MAX];
int dp[MAX][MAX];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  char tmp;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> tmp;
      if (tmp == '#')
        a[i][j] = 1;
    }
  }
  if (!a[0][0])
    dp[0][0] = 1;
  else {
    cout << 0;
    return 0;
  }
  for (int i = 1; i < m; i++) {
    if (!a[0][i] && dp[0][i - 1] != INF)
      dp[0][i] = 1;
    else
      dp[0][i] = INF;
  }
  for (int i = 1; i < n; i++) {
    if (!a[i][0] && dp[i - 1][0] != INF)
      dp[i][0] = 1;
    else
      dp[i][0] = INF;
  }
  for (int i = 1; i < n; i++) {
    for (int j = 1; j < m; j++) {
      if (a[i][j] == 0) {
        if (dp[i - 1][j] == INF && dp[i][j - 1] == INF) {
          dp[i][j] = INF;
        } else {
          if (dp[i - 1][j] == INF && dp[i][j - 1] != INF) {
            dp[i][j] = (dp[i][j - 1]) % MOD;
          } else if (dp[i - 1][j] != INF && dp[i][j - 1] == INF) {
            dp[i][j] = (dp[i - 1][j]) % MOD;
          } else if (dp[i - 1][j] != INF && dp[i][j - 1] != INF) {
            dp[i][j] = (dp[i - 1][j] + dp[i][j - 1]) % MOD;
          }
        }
      } else {
        dp[i][j] = INF;
      }
    }
  }
  if (dp[n - 1][m - 1] != INF)
    cout << dp[n - 1][m - 1];
  else
    cout << 0;
  return 0;
}