Problem p03167 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

const int mod = 1'000'000'007;
struct mint {
  ll x;
  mint(ll x = 0) : x(x % mod) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const {
    mint res(*this);
    return res += a;
  }
  mint operator-(const mint a) const {
    mint res(*this);
    return res -= a;
  }
  mint operator*(const mint a) const {
    mint res(*this);
    return res *= a;
  }
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint a) { return (*this) *= a.inv(); }
  mint operator/(const mint a) const {
    mint res(*this);
    return res /= a;
  }
};

bool fi[1001][1001];
mint dp[2][1001];

int main() {
  int h, w;
  cin >> h >> w;
  string s;
  rep(i, h) {
    cin >> s;
    rep(j, w) if (s.compare(j, 1, ".") == 0) fi[i][j] = false;
    else fi[i][j] = true;
  }
  rep(i, h) fi[i][w] = true;
  rep(i, w - 1) fi[h][i] = true;
  fi[h][w - 1] = false;
  //  rep(i, h+1) {rep(j, w+1) cout << fi[i][j]; cout << endl;}
  rep(i, w + 1) dp[0][i] = 1;

  int odd = (h - 1) % 2;
  int j, k;
  for (int i = h - 1; i >= 0; i--) {
    j = (i + 1 + odd) % 2;
    k = (i + odd) % 2;
    for (int l = w - 1; l >= 0; l--) {
      mint d = 0;
      if (fi[i][l] == true)
        dp[j][l] = d;
      else {
        if (fi[i + 1][l] == false)
          d += dp[k][l];
        if (fi[i][l + 1] == false)
          d += dp[j][l + 1];
        dp[j][l] = d;
      }
    }
    //    rep(m, w) cout << dp[j][m]; cout << endl;
  }
  cout << dp[(odd - 1) % 2][0].x << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

const int mod = 1'000'000'007;
struct mint {
  ll x;
  mint(ll x = 0) : x(x % mod) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const {
    mint res(*this);
    return res += a;
  }
  mint operator-(const mint a) const {
    mint res(*this);
    return res -= a;
  }
  mint operator*(const mint a) const {
    mint res(*this);
    return res *= a;
  }
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint a) { return (*this) *= a.inv(); }
  mint operator/(const mint a) const {
    mint res(*this);
    return res /= a;
  }
};

bool fi[1001][1001];
mint dp[2][1001];

int main() {
  int h, w;
  cin >> h >> w;
  string s;
  rep(i, h) {
    cin >> s;
    rep(j, w) if (s.compare(j, 1, ".") == 0) fi[i][j] = false;
    else fi[i][j] = true;
  }
  rep(i, h) fi[i][w] = true;
  rep(i, w - 1) fi[h][i] = true;
  fi[h][w - 1] = false;
  //  rep(i, h+1) {rep(j, w+1) cout << fi[i][j]; cout << endl;}
  rep(i, w + 1) dp[0][i] = 1;

  int odd = (h - 1) % 2;
  int j, k;
  for (int i = h - 1; i >= 0; i--) {
    j = (i + 1 + odd) % 2;
    k = (i + odd) % 2;
    for (int l = w - 1; l >= 0; l--) {
      mint d = 0;
      if (fi[i][l] == true)
        dp[j][l] = d;
      else {
        if (fi[i + 1][l] == false)
          d += dp[k][l];
        if (fi[i][l + 1] == false)
          d += dp[j][l + 1];
        dp[j][l] = d;
      }
    }
    //    rep(m, w) cout << dp[j][m]; cout << endl;
  }
  cout << dp[(odd + 1) % 2][0].x << endl;
}