Problem p03167 (C++)

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

#define make_it_fast                                                           \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)

#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define ll long long
#define ld long double
#define endl "\n"
#define ff first
#define ss second

void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V> void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}

template <typename T> void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x)
    cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}

void _print() { cerr << "]\n"; }

template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cerr << ", ";
  _print(v...);
}

#ifndef ONLINE_JUDGE
#define debug(x...)                                                            \
  cerr << "[" << #x << "] = [";                                                \
  _print(x)
#else
#define debug(x...) 20
#endif

ll power(ll a, ll b, ll m = 1e9 + 7) {
  a %= m;
  if (b == 1)
    return a;
  if (b == 0)
    return 1;
  ll ret = power(a, b / 2);
  ret = (ret % m * ret % m) % m;
  if (b & 1)
    ret = (ret % m * a % m) % m;
  return ret;
}

ll lcm(ll a, ll b) { return (a * b) / (__gcd(a, b)); }

const ll N = 1e3, M = 1e9 + 7;
ll dp[N][N];
char a[N][N];

void update(ll n, ll m) {
  if (n == 0 || m == 0)
    return;
  int i;
  for (i = n; i >= 1; i--) {
    if (a[i][m] == '#')
      continue;

    if (a[i + 1][m] != '#')
      dp[i][m] += dp[i + 1][m];

    if (a[i][m + 1] != '#')
      dp[i][m] += dp[i][m + 1];

    dp[i][m] %= M;
  }
  for (i = m - 1; i >= 1; i--) {
    if (a[n][i] == '#')
      continue;

    if (a[n][i + 1] != '#')
      dp[n][i] += dp[n][i + 1];

    if (a[n + 1][i] != '#')
      dp[n][i] += dp[n + 1][i];

    dp[n][i] %= M;
  }
  update(n - 1, m - 1);
}

void solve() {
  ll n, m, i, j;
  cin >> n >> m;
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      cin >> a[i][j];
    }
  }
  memset(dp, 0, sizeof(dp));
  dp[n][m] = 1;
  for (i = n - 1; i >= 1; i--) {
    if (a[i][m] != '#')
      dp[i][m] = dp[i + 1][m];
  }
  for (i = m - 1; i >= 1; i--) {
    if (a[n][i] != '#')
      dp[n][i] = dp[n][i + 1];
  }
  update(n - 1, m - 1);
  // for(i=1;i<=n;i++)
  // {
  //     for(j=1;j<=m;j++)
  //     cout<<dp[i][j]<<" ";
  //     cout<<endl;
  // }
  cout << dp[1][1] << endl;
}

int main() {
  make_it_fast;
  int TEST_CASES = 1;
  // cin>>TEST_CASES;
  while (TEST_CASES--) {
    solve();
  }
  return 0;
}


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

#define make_it_fast                                                           \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)

#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define ll long long
#define ld long double
#define endl "\n"
#define ff first
#define ss second

void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V> void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}

template <typename T> void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x)
    cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}

void _print() { cerr << "]\n"; }

template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cerr << ", ";
  _print(v...);
}

#ifndef ONLINE_JUDGE
#define debug(x...)                                                            \
  cerr << "[" << #x << "] = [";                                                \
  _print(x)
#else
#define debug(x...) 20
#endif

ll power(ll a, ll b, ll m = 1e9 + 7) {
  a %= m;
  if (b == 1)
    return a;
  if (b == 0)
    return 1;
  ll ret = power(a, b / 2);
  ret = (ret % m * ret % m) % m;
  if (b & 1)
    ret = (ret % m * a % m) % m;
  return ret;
}

ll lcm(ll a, ll b) { return (a * b) / (__gcd(a, b)); }

const ll N = 1e3 + 5, M = 1e9 + 7;
ll dp[N][N];
char a[N][N];

void update(ll n, ll m) {
  if (n == 0 || m == 0)
    return;
  int i;
  for (i = n; i >= 1; i--) {
    if (a[i][m] == '#')
      continue;

    if (a[i + 1][m] != '#')
      dp[i][m] += dp[i + 1][m];

    if (a[i][m + 1] != '#')
      dp[i][m] += dp[i][m + 1];

    dp[i][m] %= M;
  }
  for (i = m - 1; i >= 1; i--) {
    if (a[n][i] == '#')
      continue;

    if (a[n][i + 1] != '#')
      dp[n][i] += dp[n][i + 1];

    if (a[n + 1][i] != '#')
      dp[n][i] += dp[n + 1][i];

    dp[n][i] %= M;
  }
  update(n - 1, m - 1);
}

void solve() {
  ll n, m, i, j;
  cin >> n >> m;
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      cin >> a[i][j];
    }
  }
  memset(dp, 0, sizeof(dp));
  dp[n][m] = 1;
  for (i = n - 1; i >= 1; i--) {
    if (a[i][m] != '#')
      dp[i][m] = dp[i + 1][m];
  }
  for (i = m - 1; i >= 1; i--) {
    if (a[n][i] != '#')
      dp[n][i] = dp[n][i + 1];
  }
  update(n - 1, m - 1);
  // for(i=1;i<=n;i++)
  // {
  //     for(j=1;j<=m;j++)
  //     cout<<dp[i][j]<<" ";
  //     cout<<endl;
  // }
  cout << dp[1][1] << endl;
}

int main() {
  make_it_fast;
  int TEST_CASES = 1;
  // cin>>TEST_CASES;
  while (TEST_CASES--) {
    solve();
  }
  return 0;
}