Problem p01507 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <valarray>
#include <vector>
using namespace std;
typedef long long int ll;
typedef unsigned int uint;
typedef unsigned char uchar;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;

#define REP(i, x) for (int i = 0; i < (int)(x); i++)
#define REPS(i, x) for (int i = 1; i <= (int)(x); i++)
#define RREP(i, x) for (int i = ((int)(x)-1); i >= 0; i--)
#define RREPS(i, x) for (int i = ((int)(x)); i > 0; i--)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define RFOR(i, c)                                                             \\
  for (__typeof((c).rbegin()) i = (c).rbegin(); i != (c).rend(); i++)
#define ALL(container) (container).begin(), (container).end()
#define RALL(container) (container).rbegin(), (container).rend()
#define SZ(container) ((int)container.size())
#define mp(a, b) make_pair(a, b)
#define pb push_back
#define eb emplace_back
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &t) {
  os << "[";
  FOR(it, t) {
    if (it != t.begin())
      os << ",";
    os << *it;
  }
  os << "]";
  return os;
}
template <class T> ostream &operator<<(ostream &os, const set<T> &t) {
  os << "{";
  FOR(it, t) {
    if (it != t.begin())
      os << ",";
    os << *it;
  }
  os << "}";
  return os;
}
template <class S, class T>
ostream &operator<<(ostream &os, const map<S, T> &t) {
  os << "{";
  FOR(it, t) {
    if (it != t.begin())
      os << ",";
    os << *it;
  }
  os << "}";
  return os;
}
template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> &t) {
  return os << "(" << t.first << "," << t.second << ")";
}
template <class S, class T>
pair<S, T> operator+(const pair<S, T> &s, const pair<S, T> &t) {
  return pair<S, T>(s.first + t.first, s.second + t.second);
}
template <class S, class T>
pair<S, T> operator-(const pair<S, T> &s, const pair<S, T> &t) {
  return pair<S, T>(s.first - t.first, s.second - t.second);
}

const int INF = 1 << 28;
const double EPS = 1e-8;
const int MOD = 1000000007;

typedef unordered_map<int, int> Array;
typedef vector<Array> Matrix;
ostream &operator<<(ostream &os, const Array &t) {
  os << "[";
  FOR(it, t) {
    if (it != t.begin())
      os << ",";
    os << *it;
  }
  os << "]";
  return os;
}
ostream &operator<<(ostream &os, const Matrix &t) {
  FOR(it, t) os << *it << endl;
  return os;
}

int T, n, m;

inline ll mod_inv(ll a, ll m) {
  ll b = m, u = 1, v = 0;
  while (b) {
    ll t = a / b;
    swap(a -= t * b, b);
    swap(u -= t * v, v);
  }
  return (u % m + m) % m;
}

int det(Matrix mat) {
  int n = mat.size();
  ll ans = 1;
  REP(i, n) {
    ans = ans * mat[i][i] % MOD;
    if (!ans)
      return ans;
    ll inv = mod_inv(mat[i][i], MOD);
    for (int j = i + 1; j < n; j++)
      if (mat[j].count(i)) {
        ll coef = (MOD - mat[j][i] * inv % MOD) % MOD;
        for (auto it : mat[i]) {
          auto &jt = mat[j][it.first];
          jt = (jt + coef * it.second) % MOD;
          if (jt == 0)
            mat[j].erase(it.first);
        }
      }
  }
  return ans;
}

int main(int argc, char *argv[]) {
  ios::sync_with_stdio(false);
  int T = 1;
  while (cin >> n >> m, n) {
    vector<string> s(n);
    REP(i, n) cin >> s[i];
    vi to(n * m);
    int c = 0;
    Matrix mat;
    REP(i, n * m) {
      int x = i % m;
      int y = i / m;
      if (s[y][x] == '#')
        continue;
      mat.eb();
      if (x && s[y][x - 1] != '#') {
        mat[c][c]++;
        mat[c - 1][c - 1]++;
        mat[c][c - 1] = mat[c - 1][c] = MOD - 1;
      }
      if (y && s[y - 1][x] != '#') {
        mat[c][c]++;
        mat[to[i - m]][to[i - m]]++;
        mat[c][to[i - m]] = mat[to[i - m]][c] = MOD - 1;
      }
      to[i] = c++;
    }
    REP(i, c - 1) mat[i].erase(c - 1);
    mat.pop_back();
    cout << "Case " << T++ << ": " << det(mat) << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <valarray>
#include <vector>
using namespace std;
typedef long long int ll;
typedef unsigned int uint;
typedef unsigned char uchar;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;

#define REP(i, x) for (int i = 0; i < (int)(x); i++)
#define REPS(i, x) for (int i = 1; i <= (int)(x); i++)
#define RREP(i, x) for (int i = ((int)(x)-1); i >= 0; i--)
#define RREPS(i, x) for (int i = ((int)(x)); i > 0; i--)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define RFOR(i, c)                                                             \\
  for (__typeof((c).rbegin()) i = (c).rbegin(); i != (c).rend(); i++)
#define ALL(container) (container).begin(), (container).end()
#define RALL(container) (container).rbegin(), (container).rend()
#define SZ(container) ((int)container.size())
#define mp(a, b) make_pair(a, b)
#define pb push_back
#define eb emplace_back
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &t) {
  os << "[";
  FOR(it, t) {
    if (it != t.begin())
      os << ",";
    os << *it;
  }
  os << "]";
  return os;
}
template <class T> ostream &operator<<(ostream &os, const set<T> &t) {
  os << "{";
  FOR(it, t) {
    if (it != t.begin())
      os << ",";
    os << *it;
  }
  os << "}";
  return os;
}
template <class S, class T>
ostream &operator<<(ostream &os, const map<S, T> &t) {
  os << "{";
  FOR(it, t) {
    if (it != t.begin())
      os << ",";
    os << *it;
  }
  os << "}";
  return os;
}
template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> &t) {
  return os << "(" << t.first << "," << t.second << ")";
}
template <class S, class T>
pair<S, T> operator+(const pair<S, T> &s, const pair<S, T> &t) {
  return pair<S, T>(s.first + t.first, s.second + t.second);
}
template <class S, class T>
pair<S, T> operator-(const pair<S, T> &s, const pair<S, T> &t) {
  return pair<S, T>(s.first - t.first, s.second - t.second);
}

const int INF = 1 << 28;
const double EPS = 1e-8;
const int MOD = 1000000007;

typedef map<int, int> Array;
typedef vector<Array> Matrix;
ostream &operator<<(ostream &os, const Array &t) {
  os << "[";
  FOR(it, t) {
    if (it != t.begin())
      os << ",";
    os << *it;
  }
  os << "]";
  return os;
}
ostream &operator<<(ostream &os, const Matrix &t) {
  FOR(it, t) os << *it << endl;
  return os;
}

int T, n, m;

inline ll mod_inv(ll a, ll m) {
  ll b = m, u = 1, v = 0;
  while (b) {
    ll t = a / b;
    swap(a -= t * b, b);
    swap(u -= t * v, v);
  }
  return (u % m + m) % m;
}

int det(Matrix mat) {
  int n = mat.size();
  ll ans = 1;
  REP(i, n) {
    ans = ans * mat[i][i] % MOD;
    if (!ans)
      return ans;
    ll inv = mod_inv(mat[i][i], MOD);
    for (int j = i + 1; j < n; j++)
      if (mat[j][i]) {
        ll coef = (MOD - mat[j][i] * inv % MOD) % MOD;
        for (auto it : mat[i])
          mat[j][it.first] = (mat[j][it.first] + coef * it.second) % MOD;
      }
  }
  return ans;
}

int main(int argc, char *argv[]) {
  ios::sync_with_stdio(false);
  int T = 1;
  while (cin >> n >> m, n) {
    vector<string> s(n);
    REP(i, n) cin >> s[i];
    vi to(n * m);
    int c = 0;
    Matrix mat;
    REP(i, n * m) {
      int x = i % m;
      int y = i / m;
      if (s[y][x] == '#')
        continue;
      mat.eb();
      if (x && s[y][x - 1] != '#') {
        mat[i][i]++;
        mat[i - 1][i - 1]++;
        mat[c][c - 1] = mat[c - 1][c] = MOD - 1;
      }
      if (y && s[y - 1][x] != '#') {
        mat[i][i]++;
        mat[to[i - m]][to[i - m]]++;
        mat[c][to[i - m]] = mat[to[i - m]][c] = MOD - 1;
      }
      to[i] = c++;
    }
    REP(i, c - 1) mat[i].erase(c - 1);
    mat.pop_back();
    cout << "Case " << T++ << ": " << det(mat) << endl;
  }
  return 0;
}