Problem p03154 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <chrono>
#include <complex>
#include <cstdio>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define REP(i, x, y) for (ll i = x; i <= y; i++)
#define SIZE(a) ll(a.size())
#define vll vector<ll>
#define MEMSET(a, n, m)                                                        \
  for (ll i = 0; i <= n; i++)                                                  \
  a[i] = m
#define BIT(n) (ll(1) << n)
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
#define UNIQUE_ARRAY(a, x) unique(a + 1, a + x + 1) - a - 1
#define SORT(a, n) sort(a + 1, a + n + 1)
#define SORT_O(a, n, order) sort(a + 1, a + n + 1, order)
#define PER(i, y, x) for (ll i = y; i >= x; i--)
typedef long long ll;
using namespace std;

struct edge {
  long long to;
  long long len;
  bool operator<(const edge &rhs) const { return len > rhs.len; }
};

ll const MOD = 1e9 + 7;
ll mod_p(ll x, ll y) {
  x %= MOD;
  y %= MOD;
  return (x + y + MOD) % MOD;
}

ll mod_m(ll x, ll y) {
  x %= MOD;
  y %= MOD;
  return x * y % MOD;
}

ll mod_pow(ll x, ll t) {
  x %= MOD;
  if (t == 0) {
    return 1;
  } else {
    ll v = mod_pow(x, t / 2);
    if (t % 2 == 0) {
      return v * v % MOD;
    } else {
      return v * v % MOD * x % MOD;
    }
  }
}

ll mod_inv(ll x) { return mod_pow(x, MOD - 2); }

ll const MAX = 1e7 + 5;
ll fct[MAX];
void init() {
  fct[0] = 1;
  REP(i, 1, MAX - 1) { fct[i] = mod_m(fct[i - 1], i); }
}

int main() {
  init();
  ll h, w, k;
  cin >> h >> w >> k;
  ll a = h + w;
  ll x, y, z;
  x = mod_m(mod_inv(2), mod_m(k + 1, k));
  y = k;
  z = 0;
  REP(i, 1, k) { z = mod_p(z, mod_m(i, i - 1)); }
  z = mod_m(z, mod_m(h, w));
  z = mod_m(z, mod_m(mod_inv(a), mod_inv(a - 1)));
  ll ans = mod_m(mod_m((x + y + z), fct[a]), mod_inv(fct[a - k]));
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <chrono>
#include <complex>
#include <cstdio>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define REP(i, x, y) for (ll i = x; i <= y; i++)
#define SIZE(a) ll(a.size())
#define vll vector<ll>
#define MEMSET(a, n, m)                                                        \
  for (ll i = 0; i <= n; i++)                                                  \
  a[i] = m
#define BIT(n) (ll(1) << n)
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
#define UNIQUE_ARRAY(a, x) unique(a + 1, a + x + 1) - a - 1
#define SORT(a, n) sort(a + 1, a + n + 1)
#define SORT_O(a, n, order) sort(a + 1, a + n + 1, order)
#define PER(i, y, x) for (ll i = y; i >= x; i--)
typedef long long ll;
using namespace std;

struct edge {
  long long to;
  long long len;
  bool operator<(const edge &rhs) const { return len > rhs.len; }
};

ll const MOD = 1e9 + 7;
ll mod_p(ll x, ll y) {
  x %= MOD;
  y %= MOD;
  return (x + y + MOD) % MOD;
}

ll mod_m(ll x, ll y) {
  x %= MOD;
  y %= MOD;
  return x * y % MOD;
}

ll mod_pow(ll x, ll t) {
  x %= MOD;
  if (t == 0) {
    return 1;
  } else {
    ll v = mod_pow(x, t / 2);
    if (t % 2 == 0) {
      return v * v % MOD;
    } else {
      return v * v % MOD * x % MOD;
    }
  }
}

ll mod_inv(ll x) { return mod_pow(x, MOD - 2); }

ll const MAX = 2e7 + 5;
ll fct[MAX];
void init() {
  fct[0] = 1;
  REP(i, 1, MAX - 1) { fct[i] = mod_m(fct[i - 1], i); }
}

int main() {
  init();
  ll h, w, k;
  cin >> h >> w >> k;
  ll a = h + w;
  ll x, y, z;
  x = mod_m(mod_inv(2), mod_m(k + 1, k));
  y = k;
  z = 0;
  REP(i, 1, k) { z = mod_p(z, mod_m(i, i - 1)); }
  z = mod_m(z, mod_m(h, w));
  z = mod_m(z, mod_m(mod_inv(a), mod_inv(a - 1)));
  ll ans = mod_m(mod_m((x + y + z), fct[a]), mod_inv(fct[a - k]));
  cout << ans << endl;
}