Problem p04024 (C++)

Buggy submission (status: Runtime Error):
/*
Author: QAQ-Automaton
LANG: C++
PROG: f.cpp
Mail: cnyalilk@vip.qq.com
*/
#include <bits/stdc++.h>
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define DEBUG printf("Passing [%s] in LINE %lld\n", __FUNCTION__, __LINE__)
#define Debug debug("Passing [%s] in LINE %lld\n", __FUNCTION__, __LINE__)
#define all(x) x.begin(), x.end()
#define x first
#define y second
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pii;
const signed inf = 0x3f3f3f3f;
const double eps = 1e-8;
const double pi = acos(-1.0);
template <class T> ll chkmin(T &a, T b) { return a > b ? a = b, 1 : 0; }
template <class T> ll chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template <class T> T sqr(T a) { return a * a; }
template <class T> T mmin(T a, T b) { return a < b ? a : b; }
template <class T> T mmax(T a, T b) { return a > b ? a : b; }
template <class T> T aabs(T a) { return a < 0 ? -a : a; }
template <class T> ll dcmp(T a, T b) { return a > b; }
template <ll *a> ll cmp_a(ll x, ll y) { return a[x] < a[y]; }
#define min mmin
#define max mmax
#define abs aabs
namespace io {
const ll SIZE = (1 << 21) + 1;
char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1, c,
                                       qu[55];
ll f, qr;
// getchar
#define gc()                                                                   \
  (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),                 \
               (iS == iT ? EOF : *iS++))                                       \
            : *iS++)
// prll the remaining part
inline void flush() {
  fwrite(obuf, 1, oS - obuf, stdout);
  oS = obuf;
}
// putchar
inline void putc(char x) {
  *oS++ = x;
  if (oS == oT)
    flush();
}
// input a signed lleger
inline void read(signed &x) {
  for (f = 1, c = gc(); c < '0' || c > '9'; c = gc())
    if (c == '-')
      f = -1;
  for (x = 0; c <= '9' && c >= '0'; c = gc())
    x = x * 10 + (c & 15);
  x *= f;
}

inline void read(long long &x) {
  for (f = 1, c = gc(); c < '0' || c > '9'; c = gc())
    if (c == '-')
      f = -1;
  for (x = 0; c <= '9' && c >= '0'; c = gc())
    x = x * 10 + (c & 15);
  x *= f;
}
inline void read(char &x) { x = gc(); }
inline void read(char *x) {
  while ((*x = gc()) == '\n' || *x == ' ' || *x == '\r')
    ;
  while (!(*x == '\n' || *x == ' ' || *x == '\r'))
    *(++x) = gc();
  *x = 0;
}
template <typename A, typename... B> inline void read(A &x, B &...y) {
  read(x);
  read(y...);
}
// prll a signed lleger
inline void write(signed x) {
  if (!x)
    putc('0');
  if (x < 0)
    putc('-'), x = -x;
  while (x)
    qu[++qr] = x % 10 + '0', x /= 10;
  while (qr)
    putc(qu[qr--]);
}

inline void write(long long x) {
  if (!x)
    putc('0');
  if (x < 0)
    putc('-'), x = -x;
  while (x)
    qu[++qr] = x % 10 + '0', x /= 10;
  while (qr)
    putc(qu[qr--]);
}
inline void write(char x) { putc(x); }
inline void write(const char *x) {
  while (*x) {
    putc(*x);
    ++x;
  }
}
inline void write(char *x) {
  while (*x) {
    putc(*x);
    ++x;
  }
}
template <typename A, typename... B> inline void write(A x, B... y) {
  write(x);
  write(y...);
}
// no need to call flush at the end manually!
struct Flusher_ {
  ~Flusher_() { flush(); }
} io_flusher_;
} // namespace io
using io ::putc;
using io ::read;
using io ::write;
char s[1005][1005];
ll hs, ls, v, a, b;
ll n, m, k;
const ll p = 1000000007;
ll fpm(ll a, ll b) {
  return b ? b & 1 ? fpm(a, b - 1) * a % p : sqr(fpm(a, b >> 1)) % p : 1;
}
ll calc(ll n) {
  ll s = v * fpm(a, p - 2) % p;
  return b * fpm(a, n) % p * (fpm(s, n + 1) - 1) % p * fpm(s - 1, p - 2) % p;
}
int main() {
#ifdef QAQAutoMaton
  freopen("f.in", "r", stdin);
  freopen("f.out", "w", stdout);
#endif
  scanf("%lld%lld%lld\n", &n, &m, &k);
  for (ll i = 1; i <= n; ++i) {
    scanf("%s", s[i] + 1);
    if (s[i][1] == '#' && s[i][m] == '#')
      ++hs;
    for (ll j = 1; j <= m; ++j)
      v += s[i][j] == '#';
  }
  for (ll i = 1; i <= m; ++i)
    if (s[1][i] == '#' && s[n][i] == '#')
      ++ls;
  ll f = fpm(v, k - 1);
  if (!hs && !ls)
    write(f, '\n');
  else if (hs && ls)
    write("1\n");
  else {
    a = hs + ls;
    if (hs) {
      for (ll i = 1; i <= n; ++i)
        for (ll j = 1; j < m; ++j)
          if (s[i][j] == '#' && s[i][j + 1] == '#')
            ++b;
    } else {
      for (ll i = 1; i < n; ++i)
        for (ll j = 1; j <= m; ++j)
          if (s[i][j] == '#' && s[i + 1][j] == '#')
            ++b;
    }
    write((f + p - calc(k - 2)) % p, '\n');
  }
  return 0;
}


Fixed submission:
/*
Author: QAQ-Automaton
LANG: C++
PROG: f.cpp
Mail: cnyalilk@vip.qq.com
*/
#include <bits/stdc++.h>
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define DEBUG printf("Passing [%s] in LINE %lld\n", __FUNCTION__, __LINE__)
#define Debug debug("Passing [%s] in LINE %lld\n", __FUNCTION__, __LINE__)
#define all(x) x.begin(), x.end()
#define x first
#define y second
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pii;
const signed inf = 0x3f3f3f3f;
const double eps = 1e-8;
const double pi = acos(-1.0);
template <class T> ll chkmin(T &a, T b) { return a > b ? a = b, 1 : 0; }
template <class T> ll chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template <class T> T sqr(T a) { return a * a; }
template <class T> T mmin(T a, T b) { return a < b ? a : b; }
template <class T> T mmax(T a, T b) { return a > b ? a : b; }
template <class T> T aabs(T a) { return a < 0 ? -a : a; }
template <class T> ll dcmp(T a, T b) { return a > b; }
template <ll *a> ll cmp_a(ll x, ll y) { return a[x] < a[y]; }
#define min mmin
#define max mmax
#define abs aabs
namespace io {
const ll SIZE = (1 << 21) + 1;
char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1, c,
                                       qu[55];
ll f, qr;
// getchar
#define gc()                                                                   \
  (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),                 \
               (iS == iT ? EOF : *iS++))                                       \
            : *iS++)
// prll the remaining part
inline void flush() {
  fwrite(obuf, 1, oS - obuf, stdout);
  oS = obuf;
}
// putchar
inline void putc(char x) {
  *oS++ = x;
  if (oS == oT)
    flush();
}
// input a signed lleger
inline void read(signed &x) {
  for (f = 1, c = gc(); c < '0' || c > '9'; c = gc())
    if (c == '-')
      f = -1;
  for (x = 0; c <= '9' && c >= '0'; c = gc())
    x = x * 10 + (c & 15);
  x *= f;
}

inline void read(long long &x) {
  for (f = 1, c = gc(); c < '0' || c > '9'; c = gc())
    if (c == '-')
      f = -1;
  for (x = 0; c <= '9' && c >= '0'; c = gc())
    x = x * 10 + (c & 15);
  x *= f;
}
inline void read(char &x) { x = gc(); }
inline void read(char *x) {
  while ((*x = gc()) == '\n' || *x == ' ' || *x == '\r')
    ;
  while (!(*x == '\n' || *x == ' ' || *x == '\r'))
    *(++x) = gc();
  *x = 0;
}
template <typename A, typename... B> inline void read(A &x, B &...y) {
  read(x);
  read(y...);
}
// prll a signed lleger
inline void write(signed x) {
  if (!x)
    putc('0');
  if (x < 0)
    putc('-'), x = -x;
  while (x)
    qu[++qr] = x % 10 + '0', x /= 10;
  while (qr)
    putc(qu[qr--]);
}

inline void write(long long x) {
  if (!x)
    putc('0');
  if (x < 0)
    putc('-'), x = -x;
  while (x)
    qu[++qr] = x % 10 + '0', x /= 10;
  while (qr)
    putc(qu[qr--]);
}
inline void write(char x) { putc(x); }
inline void write(const char *x) {
  while (*x) {
    putc(*x);
    ++x;
  }
}
inline void write(char *x) {
  while (*x) {
    putc(*x);
    ++x;
  }
}
template <typename A, typename... B> inline void write(A x, B... y) {
  write(x);
  write(y...);
}
// no need to call flush at the end manually!
struct Flusher_ {
  ~Flusher_() { flush(); }
} io_flusher_;
} // namespace io
using io ::putc;
using io ::read;
using io ::write;
char s[1005][1005];
ll hs, ls, v, a, b;
ll n, m, k;
const ll p = 1000000007;
ll fpm(ll a, ll b) {
  return b ? b & 1 ? fpm(a, b - 1) * a % p : sqr(fpm(a, b >> 1)) % p : 1;
}
ll calc(ll n) {
  ll s = v * fpm(a, p - 2) % p;
  return b * fpm(a, n) % p * (fpm(s, n + 1) - 1) % p * fpm(s - 1, p - 2) % p;
}
int main() {
#ifdef QAQAutoMaton
  freopen("f.in", "r", stdin);
  freopen("f.out", "w", stdout);
#endif
  scanf("%lld%lld%lld\n", &n, &m, &k);
  if (k <= 1) {
    write("1\n");
    return 0;
  }
  for (ll i = 1; i <= n; ++i) {
    scanf("%s", s[i] + 1);
    if (s[i][1] == '#' && s[i][m] == '#')
      ++hs;
    for (ll j = 1; j <= m; ++j)
      v += s[i][j] == '#';
  }
  for (ll i = 1; i <= m; ++i)
    if (s[1][i] == '#' && s[n][i] == '#')
      ++ls;
  ll f = fpm(v, k - 1);
  if (!hs && !ls)
    write(f, '\n');
  else if (hs && ls)
    write("1\n");
  else {
    a = hs + ls;
    if (hs) {
      for (ll i = 1; i <= n; ++i)
        for (ll j = 1; j < m; ++j)
          if (s[i][j] == '#' && s[i][j + 1] == '#')
            ++b;
    } else {
      for (ll i = 1; i < n; ++i)
        for (ll j = 1; j <= m; ++j)
          if (s[i][j] == '#' && s[i + 1][j] == '#')
            ++b;
    }
    write((f + p - calc(k - 2)) % p, '\n');
  }
  return 0;
}