Problem p03167 (C++)

Buggy submission (status: Runtime Error):
#define _CRT_SECURE_NO_WARNINGS

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
#define read freopen("in.in", "r", stdin)
#define write freopen("out.out", "w", stdout)
#define FastIO ios::sync_with_stdio(0)

#define All(a) a.begin(), a.end()
#define PI 3.1415926535897932385
#define INF 1000000000
#define eps 1e-7
#define Clear(a) memset(a, 0, sizeof(a))
#define MOD 1000000007
typedef vector<int> vi;
typedef pair<pair<int, int>, int> pii;
typedef unsigned long long ull;

int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};

ll GCD(ll a, ll b) {
  if (b == 0)
    return a;
  return GCD(b, a % b);
}
ll LCM(ll a, ll b) { return (a / GCD(a, b)) * b; }

pair<ll, pair<ll, ll>> extendedEuclid(ll a, ll b) {
  if (a == 0)
    return make_pair(b, make_pair(0, 1));
  pair<ll, pair<ll, ll>> p;
  p = extendedEuclid(b % a, a);
  return make_pair(
      p.first,
      make_pair(p.second.second - p.second.first * (b / a), p.second.first));
}

ll modInverse(ll a, ll m) {
  return (extendedEuclid(a, m).second.first + m) % m;
}

ll inverseMod(ll x, ll y, ll z) { return ((x % z) * modInverse(y, z)) % z; }
ll BigMod(ll x, ll y) {
  if (y == 0)
    return 1;
  if (x == 1)
    return 1;
  if (y == 1)
    return x;
  ll ans = BigMod(x, y / 2) % MOD;
  if (y & 1)
    return ((x % MOD * ans % MOD) % MOD * ans % MOD) % MOD;
  return (ans % MOD * ans % MOD) % MOD;
}

int n, m;
int in[1001][1001];
int dp[1001][1001];
int val(int x, int y) {
  return x >= 0 && y >= 0 && x < n && y < m && in[x][y] == 0;
}
int solve(int x, int y) {
  if (x == n - 1 && y == m - 1)
    return 1;
  ;
  if (dp[x][y] != -1)
    return dp[x][y];
  int ans = 0;
  for (int c = 0; c < 2; c++) {
    int xx = x + dx[c], yy = y + dy[c];
    if (val(xx, yy))
      ans += solve(xx, yy);
    ans %= MOD;
  }
  return dp[x][y] = ans % MOD;
}
int main() {
  FastIO;
#ifndef ONLINE_JUDGE
  freopen("in.in", "r", stdin);
  // freopen("out.out", "w", stdout);
#endif

  cin >> n >> m;
  for (int c = 0; c < n; c++) {
    for (int i = 0; i < m; i++) {
      char x;
      cin >> x;
      if (x == '.')
        in[c][i] = 0;
      else
        in[c][i] = 1;
    }
  }
  memset(dp, -1, sizeof dp);
  cout << solve(0, 0) % MOD << " ";
  // cout << dp[n - 1][m - 1]+1 << endl;
  return 0;
}

Fixed submission:
#define _CRT_SECURE_NO_WARNINGS

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
#define read freopen("in.in", "r", stdin)
#define write freopen("out.out", "w", stdout)
#define FastIO ios::sync_with_stdio(0)

#define All(a) a.begin(), a.end()
#define PI 3.1415926535897932385
#define INF 1000000000
#define eps 1e-7
#define Clear(a) memset(a, 0, sizeof(a))
#define MOD 1000000007
typedef vector<int> vi;
typedef pair<pair<int, int>, int> pii;
typedef unsigned long long ull;

int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};

ll GCD(ll a, ll b) {
  if (b == 0)
    return a;
  return GCD(b, a % b);
}
ll LCM(ll a, ll b) { return (a / GCD(a, b)) * b; }

pair<ll, pair<ll, ll>> extendedEuclid(ll a, ll b) {
  if (a == 0)
    return make_pair(b, make_pair(0, 1));
  pair<ll, pair<ll, ll>> p;
  p = extendedEuclid(b % a, a);
  return make_pair(
      p.first,
      make_pair(p.second.second - p.second.first * (b / a), p.second.first));
}

ll modInverse(ll a, ll m) {
  return (extendedEuclid(a, m).second.first + m) % m;
}

ll inverseMod(ll x, ll y, ll z) { return ((x % z) * modInverse(y, z)) % z; }
ll BigMod(ll x, ll y) {
  if (y == 0)
    return 1;
  if (x == 1)
    return 1;
  if (y == 1)
    return x;
  ll ans = BigMod(x, y / 2) % MOD;
  if (y & 1)
    return ((x % MOD * ans % MOD) % MOD * ans % MOD) % MOD;
  return (ans % MOD * ans % MOD) % MOD;
}

int n, m;
int in[1001][1001];
int dp[1001][1001];
int val(int x, int y) {
  return x >= 0 && y >= 0 && x < n && y < m && in[x][y] == 0;
}
int solve(int x, int y) {
  if (x == n - 1 && y == m - 1)
    return 1;
  ;
  if (dp[x][y] != -1)
    return dp[x][y];
  int ans = 0;
  for (int c = 0; c < 2; c++) {
    int xx = x + dx[c], yy = y + dy[c];
    if (val(xx, yy))
      ans += solve(xx, yy);
    ans %= MOD;
  }
  return dp[x][y] = ans % MOD;
}
int main() {
  FastIO;

  cin >> n >> m;
  for (int c = 0; c < n; c++) {
    for (int i = 0; i < m; i++) {
      char x;
      cin >> x;
      if (x == '.')
        in[c][i] = 0;
      else
        in[c][i] = 1;
    }
  }
  memset(dp, -1, sizeof dp);
  cout << solve(0, 0) % MOD << " ";
  // cout << dp[n - 1][m - 1]+1 << endl;
  return 0;
}