prithivMLmods/Coder-Stat huggingface dataset:
Problem p03038 (C++)

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

typedef long long ll;
#define rep(i, n) for (int i = 0; i < n; i++)

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> a(N);
  rep(i, N) cin >> a[i];
  map<int, int, greater<int>> mp;
  rep(i, M) {
    int b, c;
    cin >> b >> c;
    mp[c] += b;
  }
  priority_queue<int> p_que;
  for (auto p : mp) {
    while (p.second != 0) {
      p_que.push(p.first);
      p.second--;
    }
  }
  sort(a.begin(), a.end());
  ll ans = 0;
  rep(i, N) {
    if (a[i] < p_que.top() && !p_que.empty()) {
      ans += p_que.top();
      p_que.pop();
    } else {
      ans += a[i];
    }
  }
  cout << ans << endl;
  return 0;
}

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

typedef long long ll;
#define rep(i, n) for (int i = 0; i < n; i++)

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> a(N);
  rep(i, N) cin >> a[i];
  map<int, int, greater<int>> mp;
  rep(i, M) {
    int b, c;
    cin >> b >> c;
    mp[c] += b;
  }
  priority_queue<int> p_que;
  for (auto p : mp) {
    while (p.second != 0 && p_que.size() < N) {
      p_que.push(p.first);
      p.second--;
    }
  }
  sort(a.begin(), a.end());
  ll ans = 0;
  rep(i, N) {
    if (a[i] < p_que.top() && !p_que.empty()) {
      ans += p_que.top();
      p_que.pop();
    } else {
      ans += a[i];
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03038 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <bitset>

using namespace std;

#define M 1000000007
#define pb push_back
#define f first
#define s second
#define rep(i, st, ed) for (ll i = st; i < ed; i++)
#define repn(i, st, ed) for (ll i = st; i <= ed; i++)
#define repb(i, ed, st) for (ll i = ed; i >= st; i--)
#define all(v) v.begin(), v.end()
typedef long long ll;
typedef unsigned long long int llt;
typedef long double ld;

const long double PI = 3.141592653589793238463;
const int N = 1e5 + 10;
const ll INF = 1LL << 60;

template <class T> void chmax(T &a, T b) {
  if (a < b)
    a = b;
}
template <class T> void chmin(T &a, T b) {
  if (a > b)
    a = b;
}

// Main Code

int main() {
  int n, m;
  cin >> n >> m;
  priority_queue<ll, vector<ll>, greater<ll>> pq;
  rep(i, 0, n) {
    ll x;
    cin >> x;
    pq.push(x);
  }
  vector<pair<ll, ll>> vp;
  rep(i, 0, m) {
    ll x, y;
    cin >> x >> y;
    vp.pb({y, x});
  }
  sort(all(vp), greater<pair<ll, ll>>());
  ll ans = 0;
  rep(i, 0, m) {
    if (pq.top() > vp[i].f || pq.empty())
      break;
    ll num = vp[i].f, cnt = vp[i].s;
    while (cnt-- && pq.top() <= num) {
      ans += num;
      pq.pop();
    }
  }
  while (!pq.empty()) {
    ans += pq.top();
    pq.pop();
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#include <bitset>

using namespace std;

#define M 1000000007
#define pb push_back
#define f first
#define s second
#define rep(i, st, ed) for (ll i = st; i < ed; i++)
#define repn(i, st, ed) for (ll i = st; i <= ed; i++)
#define repb(i, ed, st) for (ll i = ed; i >= st; i--)
#define all(v) v.begin(), v.end()
typedef long long ll;
typedef unsigned long long int llt;
typedef long double ld;

const long double PI = 3.141592653589793238463;
const int N = 1e5 + 10;
const ll INF = 1LL << 60;

template <class T> void chmax(T &a, T b) {
  if (a < b)
    a = b;
}
template <class T> void chmin(T &a, T b) {
  if (a > b)
    a = b;
}

// Main Code

int main() {
  int n, m;
  cin >> n >> m;
  priority_queue<ll, vector<ll>, greater<ll>> pq;
  rep(i, 0, n) {
    ll x;
    cin >> x;
    pq.push(x);
  }
  vector<pair<ll, ll>> vp;
  rep(i, 0, m) {
    ll x, y;
    cin >> x >> y;
    vp.pb({y, x});
  }
  sort(all(vp), greater<pair<ll, ll>>());
  ll ans = 0;
  rep(i, 0, m) {
    if (pq.top() > vp[i].f || pq.empty())
      break;
    ll num = vp[i].f, cnt = vp[i].s;
    while (cnt-- && pq.top() < num && !pq.empty()) {
      ans += num;
      pq.pop();
    }
  }
  while (!pq.empty()) {
    ans += pq.top();
    pq.pop();
  }
  cout << ans << endl;
}
Problem p03038 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <set>
#define LIMIT 100000
using namespace std;

struct card {
  long b;
  long c;
};

bool cmp(const card left, const card right) { return left.c < right.c; }

int main() {
  int n, m;
  long tmp;
  struct card input[LIMIT];
  long b, c;
  multiset<long> a;
  int i, j;

  cin >> n >> m;
  for (i = 0; i < n; i++) {
    cin >> tmp;
    a.insert(tmp);
  }

  for (i = 0; i < m; i++) {
    cin >> input[i].b >> input[i].c;
  }
  sort(input, input + m, cmp);

  for (i = 0; i < m; i++) {
    //		cin >> b >> c;
    b = input[i].b;
    c = input[i].c;
    multiset<long>::iterator k = a.begin();
    for (j = 0; j < b; j++) {
      if (*k >= c) {
        break;
      }
      a.erase(k++);
      a.insert(c);
    }
  }

  /*
  auto itr = a.begin();
  for (i = 0; i < n; i++, itr++) {
          cout << *itr << endl;
  }
  cout << endl;
  */

  auto itr = a.begin();
  long ans = 0;
  for (i = 0; i < n; i++) {
    ans += *itr;
    itr++;
  }
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <set>
#define LIMIT 100000
using namespace std;

struct card {
  long b;
  long c;
};

bool cmp(const card left, const card right) { return right.c < left.c; }

int main() {
  int n, m;
  long tmp;
  struct card input[LIMIT];
  long b, c;
  multiset<long> a;
  int i, j;

  cin >> n >> m;
  for (i = 0; i < n; i++) {
    cin >> tmp;
    a.insert(tmp);
  }

  for (i = 0; i < m; i++) {
    cin >> input[i].b >> input[i].c;
  }
  sort(input, input + m, cmp);

  for (i = 0; i < m; i++) {
    //		cin >> b >> c;
    b = input[i].b;
    c = input[i].c;
    multiset<long>::iterator k = a.begin();
    for (j = 0; j < b; j++) {
      if (*k >= c) {
        break;
      }
      a.erase(k++);
      a.insert(c);
    }
  }

  /*
  auto itr = a.begin();
  for (i = 0; i < n; i++, itr++) {
          cout << *itr << endl;
  }
  cout << endl;
  */

  auto itr = a.begin();
  long ans = 0;
  for (i = 0; i < n; i++) {
    ans += *itr;
    itr++;
  }
  cout << ans << endl;

  return 0;
}
Problem p03038 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 1e5 + 5;
struct line {
  int num, v;
} a[MAXN];
int cmp(line i, line j) { return i.v > j.v; }
int main() {
  int m, n, i;
  scanf("%d%d", &n, &m);
  for (i = 1; i <= n; i++) {
    cin >> a[i].v;
    a[i].num = 1;
  }
  for (i; i <= n + m; i++) {
    cin >> a[i].num;
    cin >> a[i].v;
  }
  sort(a + 1, a + n + m + 1, cmp);
  ll s = 0;
  int k = 0;
  for (i = 1; i <= n + m + 1; i++) {
    if (k + a[i].num <= n) {
      k += a[i].num;
      s += (ll)a[i].num * a[i].v;
    } else {
      s += (ll)(n - k) * a[i].v;
      break;
    }
  }
  cout << s << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 1e5 + 5;
struct line {
  int num, v;
} a[MAXN * 2];
int cmp(line i, line j) { return i.v > j.v; }
int main() {
  int m, n, i;
  scanf("%d%d", &n, &m);
  for (i = 1; i <= n; i++) {
    cin >> a[i].v;
    a[i].num = 1;
  }
  for (i; i <= n + m; i++) {
    cin >> a[i].num;
    cin >> a[i].v;
  }
  sort(a + 1, a + n + m + 1, cmp);
  ll s = 0;
  int k = 0;
  for (i = 1; i <= n + m + 1; i++) {
    if (k + a[i].num <= n) {
      k += a[i].num;
      s += (ll)a[i].num * a[i].v;
    } else {
      s += (ll)(n - k) * a[i].v;
      break;
    }
  }
  cout << s << endl;
  return 0;
}
Problem p03038 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int N, M;
  int long sum = 0;
  int count = 0;
  cin >> N >> M;
  vector<long int> A(N);
  vector<pair<long int, int>> BC(M);
  vector<long int> D(0);

  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
  }
  for (int i = 0; i < M; i++) {
    cin >> BC.at(i).second >> BC.at(i).first;
  }
  sort(BC.begin(), BC.end());
  reverse(BC.begin(), BC.end());
  sort(A.begin(), A.end());

  for (int j = 0; j < M; j++) {
    for (int k = 0; k < BC.at(j).second; k++) {
      count++;
      D.push_back(BC.at(j).first);
      if (count == N) {
        break;
      }
    }
    if (count == N) {
      break;
    }
  }

  // for (int i = 0; i < N; i++) {
  //    D.push_back(0);
  //}

  for (int i = 0; i < N; i++) {
    sum += max(D.at(i), A.at(i));
  }

  cout << sum << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int N, M;
  int long sum = 0;
  int count = 0;
  cin >> N >> M;
  vector<long int> A(N);
  vector<pair<long int, int>> BC(M);
  vector<long int> D(0);

  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
  }
  for (int i = 0; i < M; i++) {
    cin >> BC.at(i).second >> BC.at(i).first;
  }
  sort(BC.begin(), BC.end());
  reverse(BC.begin(), BC.end());
  sort(A.begin(), A.end());

  for (int j = 0; j < M; j++) {
    for (int k = 0; k < BC.at(j).second; k++) {
      count++;
      D.push_back(BC.at(j).first);
      if (count == N) {
        break;
      }
    }
    if (count == N) {
      break;
    }
  }

  for (int i = 0; i < N; i++) {
    D.push_back(0);
  }

  for (int i = 0; i < N; i++) {
    sum += max(D.at(i), A.at(i));
  }

  cout << sum << endl;
  return 0;
}
Problem p03038 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 100;
int n, m, a[N], z;
pair<int, int> v[N];
long long int sum = 0;
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a, a + n);
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    v[i] = make_pair(y, x);
  }
  sort(v, v + m);
  reverse(v, v + m);
  for (int i = 0; i < m; i++) {
    int c = 0;
    while (a[z] < v[i].first && c < v[i].second) {
      z++;
      c++;
    }
    sum += 1LL * c * v[i].first;
  }
  for (int i = z; i < n; i++)
    sum += a[i];
  cout << sum;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 100;
int n, m, a[N], z;
pair<int, int> v[N];
long long int sum = 0;
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a, a + n);
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    v[i] = make_pair(y, x);
  }
  sort(v, v + m);
  reverse(v, v + m);
  for (int i = 0; i < m; i++) {
    int c = 0;
    while (a[z] < v[i].first && c < v[i].second && z < n) {
      z++;
      c++;
    }
    sum += 1LL * c * v[i].first;
  }
  for (int i = z; i < n; i++)
    sum += a[i];
  cout << sum;
  return 0;
}
Problem p03038 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> lpair;
const ll MOD = 1e9 + 7;
const ll INF = 1e18;
#define rep(i, m, n) for (ll i = (m); i < (n); i++)
#define rrep(i, m, n) for (ll i = (m); i >= (n); i--)
#define print(x) cout << (x) << endl;
#define print2(x, y) cout << (x) << " " << (y) << endl;
#define printa(x, n)                                                           \
  for (ll i = 0; i < n; i++) {                                                 \
    cout << (x[i]) << " ";                                                     \
  }                                                                            \
  cout << endl;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll N, M;
  ll A[100010];
  cin >> N >> M;
  rep(i, 0, N) cin >> A[i];
  ll B[100010], C[100010];
  rep(i, 0, M) { cin >> B[i] >> C[i]; }
  sort(A, A + N);
  vector<lpair> lp;
  rep(i, 0, N) { lp.push_back(make_pair(C[i], B[i])); }
  sort(lp.begin(), lp.end(), greater<lpair>());
  ll pos = 0;
  rep(i, 0, M) {
    ll b = lp[i].second;
    ll c = lp[i].first;
    ll cnt = 0;
    while (cnt < b) {
      if (pos >= N)
        break;
      if (A[pos] < c) {
        A[pos] = c;
        pos++;
      } else {
        break;
      }
      cnt++;
    }
  }
  ll ans = 0;
  rep(i, 0, N) ans += A[i];
  print(ans);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> lpair;
const ll MOD = 1e9 + 7;
const ll INF = 1e18;
#define rep(i, m, n) for (ll i = (m); i < (n); i++)
#define rrep(i, m, n) for (ll i = (m); i >= (n); i--)
#define print(x) cout << (x) << endl;
#define print2(x, y) cout << (x) << " " << (y) << endl;
#define printa(x, n)                                                           \
  for (ll i = 0; i < n; i++) {                                                 \
    cout << (x[i]) << " ";                                                     \
  }                                                                            \
  cout << endl;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll N, M;
  ll A[100010];
  cin >> N >> M;
  rep(i, 0, N) cin >> A[i];
  ll B[100010], C[100010];
  rep(i, 0, M) { cin >> B[i] >> C[i]; }
  sort(A, A + N);
  vector<lpair> lp;
  rep(i, 0, M) { lp.push_back(make_pair(C[i], B[i])); }
  sort(lp.begin(), lp.end(), greater<lpair>());
  ll pos = 0;
  rep(i, 0, M) {
    ll b = lp[i].second;
    ll c = lp[i].first;
    ll cnt = 0;
    while (cnt < b) {
      if (pos >= N)
        break;
      if (A[pos] < c) {
        A[pos] = c;
        pos++;
      } else {
        break;
      }
      cnt++;
    }
  }
  ll ans = 0;
  rep(i, 0, N) ans += A[i];
  print(ans);
}
Problem p03039 (C++)

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

using namespace std;

const int MOD = 1e9 + 7, maxn = 1e5 + 9;
int64_t gt[maxn + 1], n, m, k;

int64_t Pow(int x, int y) {
  if (y == 0)
    return 1;
  else {
    int64_t tmp = Pow(x, y / 2);
    int64_t res = (tmp % MOD * tmp % MOD) % MOD;
    if (y % 2 == 0)
      return res;
    else
      return (res * x % MOD) % MOD;
  }
}

int64_t Comb(int n, int k) {
  int64_t a = gt[n];
  int64_t b = (gt[k] % MOD * gt[n - k] % MOD) % MOD;
  int64_t c =
      (a * Pow(b, MOD - 2)) % MOD; /// (a / b) % MOD = a * inverse(b) % MOD
  return c;
}

void Enter() { cin >> n >> m >> k; }

void Init() {
  gt[0] = 1;
  for (int i = 1; i <= maxn; i++)
    gt[i] = (gt[i - 1] % MOD * i % MOD) % MOD;
}

int64_t SumX() {
  int64_t s = 0;
  for (int i = 1; i <= m; i++) {
    int64_t l = i - 1, r = m - i;
    s = (s + l * i - r * i) % MOD;
  }
  s = (s % MOD * Pow(n, 2)) % MOD;
  return s;
}

int64_t SumY() {
  int64_t s = 0;
  for (int i = 1; i <= n; i++) {
    int64_t l = i - 1, r = n - i;
    s = (s + l * i - r * i) % MOD;
  }
  s = (s % MOD * Pow(m, 2)) % MOD;
  return s;
}

void Solve() {
  cout << ((SumX() + SumY()) % MOD * Comb(m * n - 2, k - 2)) % MOD;
}

int main() {
  Enter();
  Init();
  Solve();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

const int MOD = 1e9 + 7, maxn = 2e5 + 9;
int64_t gt[maxn + 1], n, m, k;

int64_t Pow(int x, int y) {
  if (y == 0)
    return 1;
  else {
    int64_t tmp = Pow(x, y / 2);
    int64_t res = (tmp % MOD * tmp % MOD) % MOD;
    if (y % 2 == 0)
      return res;
    else
      return (res * x % MOD) % MOD;
  }
}

int64_t Comb(int n, int k) {
  int64_t a = gt[n];
  int64_t b = (gt[k] % MOD * gt[n - k] % MOD) % MOD;
  int64_t c =
      (a * Pow(b, MOD - 2)) % MOD; /// (a / b) % MOD = a * inverse(b) % MOD
  return c;
}

void Enter() { cin >> n >> m >> k; }

void Init() {
  gt[0] = 1;
  for (int i = 1; i <= maxn; i++)
    gt[i] = (gt[i - 1] % MOD * i % MOD) % MOD;
}

int64_t SumX() {
  int64_t s = 0;
  for (int i = 1; i <= m; i++) {
    int64_t l = i - 1, r = m - i;
    s = (s + l * i - r * i) % MOD;
  }
  s = (s % MOD * Pow(n, 2)) % MOD;
  return s;
}

int64_t SumY() {
  int64_t s = 0;
  for (int i = 1; i <= n; i++) {
    int64_t l = i - 1, r = n - i;
    s = (s + l * i - r * i) % MOD;
  }
  s = (s % MOD * Pow(m, 2)) % MOD;
  return s;
}

void Solve() {
  cout << ((SumX() + SumY()) % MOD * Comb(m * n - 2, k - 2)) % MOD;
}

int main() {
  Enter();
  Init();
  Solve();
  return 0;
}
Problem p03039 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define syosu(x) fixed << setprecision(x)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<string> vs;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<pll> vpll;
typedef pair<int, P> pip;
typedef vector<pip> vip;
const int inf = 1 << 30;
const ll INF = 1ll << 60;
const double pi = acos(-1);
const double eps = 1e-8;
const ll mod = 1e9 + 7;
const int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, -1, 0, 1};

ll Pow(ll n, ll p) {
  ll r = 1;
  for (; p > 0; p >>= 1) {
    if (p & 1)
      r = (r * n) % mod;
    n = (n * n) % mod;
  }
  return r;
}

const int M = 200005;
ll F[M];

void Init() {
  F[0] = 1;
  for (int i = 1; i < M; i++)
    F[i] = F[i - 1] * i % mod;
}

ll Div(ll n, ll m) { return n * Pow(m, mod - 2) % mod; }

ll nCk(ll n, ll k) { return Div(F[n], F[n - k] * F[k] % mod); }

ll n, m, k;

int main() {
  Init();
  cin >> n >> m >> k;
  assert(n == 1 || m == 1);
  if (n == 1)
    swap(n, m);
  //	cout<<Div(nCk(n*m,k)*k%mod*(k-1)%mod*((n-1)*(n+1)*m+(m-1)*(m+1)*n)%mod,6*(n*m-1))<<endl;
  cout << Div(nCk(n, k) * k % mod * (k - 1) % mod * (n + 1) % mod, 6) << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define syosu(x) fixed << setprecision(x)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<string> vs;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<pll> vpll;
typedef pair<int, P> pip;
typedef vector<pip> vip;
const int inf = 1 << 30;
const ll INF = 1ll << 60;
const double pi = acos(-1);
const double eps = 1e-8;
const ll mod = 1e9 + 7;
const int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, -1, 0, 1};

ll Pow(ll n, ll p) {
  ll r = 1;
  for (; p > 0; p >>= 1) {
    if (p & 1)
      r = (r * n) % mod;
    n = (n * n) % mod;
  }
  return r;
}

const int M = 200005;
ll F[M];

void Init() {
  F[0] = 1;
  for (int i = 1; i < M; i++)
    F[i] = F[i - 1] * i % mod;
}

ll Div(ll n, ll m) { return n * Pow(m, mod - 2) % mod; }

ll nCk(ll n, ll k) { return Div(F[n], F[n - k] * F[k] % mod); }

ll n, m, k;

int main() {
  Init();
  cin >> n >> m >> k;
  cout << Div(((n - 1) * (n + 1) * m + (m - 1) * (m + 1) * n) % mod *
                  nCk(n * m, k) % mod * k % mod * (k - 1) % mod,
              6 * (n * m - 1))
       << endl;
}
Problem p03039 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

#define INF 1000000007
#define LINF (1LL << 62)

typedef long long i64;
typedef pair<i64, i64> P;

i64 n, m, k;

// コンビネーション
// けんちょんさんありがとう
const int MAX = 101010, MOD = INF;
long long fac[MAX], finv[MAX], inv[MAX];
// テーブルを作る前処理
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}
// 二項係数計算
long long C(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
////

int main() {
  cin >> n >> m >> k;

  i64 ans = 0, cnt = 0;
  for (i64 i = 1; i < n; i++) {
    cnt = i * (i + 1) / 2 % INF;
    cnt = cnt * m % INF * m % INF;
    ans += cnt;
    ans %= INF;
  }
  for (i64 i = 1; i < m; i++) {
    cnt = i * (i + 1) / 2 % INF;
    cnt = cnt * n % INF * n % INF;
    ans += cnt;
    ans %= INF;
  }

  COMinit();
  cout << C(n * m - 2, k - 2) * ans % INF << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

#define INF 1000000007
#define LINF (1LL << 62)

typedef long long i64;
typedef pair<i64, i64> P;

i64 n, m, k;

// コンビネーション
// けんちょんさんありがとう
const int MAX = 202020, MOD = INF;
long long fac[MAX], finv[MAX], inv[MAX];
// テーブルを作る前処理
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}
// 二項係数計算
long long C(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
////

int main() {
  cin >> n >> m >> k;

  i64 ans = 0, cnt = 0;
  for (i64 i = 1; i < n; i++) {
    cnt = i * (i + 1) / 2 % INF;
    cnt = cnt * m % INF * m % INF;
    ans += cnt;
    ans %= INF;
  }
  for (i64 i = 1; i < m; i++) {
    cnt = i * (i + 1) / 2 % INF;
    cnt = cnt * n % INF * n % INF;
    ans += cnt;
    ans %= INF;
  }

  COMinit();
  cout << C(n * m - 2, k - 2) * ans % INF << endl;

  return 0;
}
Problem p03039 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <iostream>
#define MN 200000
#define mod 1000000007

int fac[MN + 5], Inv[MN + 5], inv[MN + 5];
int n, m, k;

int C(int a, int b) {
  if (a < b)
    return 0;
  return 1ll * fac[a] * inv[b] % mod * inv[a - b] % mod;
}

int solve(int n, int m) {
  int ans = 0;
  int w = 0;
  for (int b = 1; b <= n; b++)
    w = (w + 1ll * b * (k - b) % mod * C(n * (m - 1), k - b) % mod * C(n, b)) %
        mod;
  for (int a = 1; a <= m; a++) {
    int P = 1ll * Inv[m - 1] * (a - 1 - (m - a) + mod) % mod;
    ans = (ans + 1ll * w * P % mod * a) % mod;
  }
  return ans;
}

int main() {
  scanf("%d%d%d", &n, &m, &k);

  fac[0] = Inv[0] = Inv[1] = inv[0] = 1;
  for (int i = 1; i <= MN; i++)
    fac[i] = 1ll * fac[i - 1] * i % mod;
  for (int i = 2; i <= MN; i++)
    Inv[i] = 1ll * (mod - mod / i) * Inv[mod % i] % mod;
  for (int i = 1; i <= MN; i++)
    inv[i] = 1ll * Inv[i] * inv[i - 1] % mod;

  printf("%d\n", (solve(n, m) + solve(m, n)) % mod);
}

Fixed submission:
#include <cstdio>
#include <iostream>
#define MN 200000
#define mod 1000000007

int fac[MN + 5], Inv[MN + 5], inv[MN + 5];
int n, m, k;

int C(int a, int b) {
  if (a < b)
    return 0;
  return 1ll * fac[a] * inv[b] % mod * inv[a - b] % mod;
}

int solve(int n, int m) {
  int ans = 0;
  int w = 0;
  for (int b = 1; b <= std::min(n, k); b++)
    w = (w + 1ll * b * (k - b) % mod * C(n * (m - 1), k - b) % mod * C(n, b)) %
        mod;
  for (int a = 1; a <= m; a++) {
    int P = 1ll * Inv[m - 1] * (a - 1 - (m - a) + mod) % mod;
    ans = (ans + 1ll * w * P % mod * a) % mod;
  }
  return ans;
}

int main() {
  scanf("%d%d%d", &n, &m, &k);

  fac[0] = Inv[0] = Inv[1] = inv[0] = 1;
  for (int i = 1; i <= MN; i++)
    fac[i] = 1ll * fac[i - 1] * i % mod;
  for (int i = 2; i <= MN; i++)
    Inv[i] = 1ll * (mod - mod / i) * Inv[mod % i] % mod;
  for (int i = 1; i <= MN; i++)
    inv[i] = 1ll * Inv[i] * inv[i - 1] % mod;

  printf("%d\n", (solve(n, m) + solve(m, n)) % mod);
}
Problem p03039 (C++)

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

#define ll long long
#define fornum(A, B, C) for (A = B; A < C; A++)
#define mp make_pair
#define pii pair<int, int>
#define pll pair<ll, ll>

using namespace std;

/////////////////////////////////////////////////////
#define MOD ((ll)1e9 + 7)

ll N, M, K, fa[101010];
ll i, j, k, l;

ll moddiv(ll a, ll b) {
  for (ll m = MOD - 2; m > 0; m /= 2) {
    if (m & 1) {
      a = a * b % MOD;
    }
    b = b * b % MOD;
  }
  return a;
}

int main() {
  scanf("%lld%lld%lld", &N, &M, &K);
  fa[0] = 1;
  fornum(i, 0, N * M) { fa[i + 1] = fa[i] * (i + 1) % MOD; }
  ll n = moddiv(((N * (N + 1) % MOD * (2 * N + 1) % MOD) + MOD -
                 (3 * N * (N + 1) % MOD)) %
                    MOD,
                12) *
         M % MOD * M % MOD;
  ll m = moddiv(((M * (M + 1) % MOD * (2 * M + 1) % MOD) + MOD -
                 (3 * M * (M + 1) % MOD)) %
                    MOD,
                12) *
         N % MOD * N % MOD;
  // printf("%lld %lld ", n, m);
  printf("%lld", (n + m) % MOD *
                     moddiv(fa[N * M - 2], fa[K - 2] * fa[N * M - K] % MOD) %
                     MOD);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

#define ll long long
#define fornum(A, B, C) for (A = B; A < C; A++)
#define mp make_pair
#define pii pair<int, int>
#define pll pair<ll, ll>

using namespace std;

/////////////////////////////////////////////////////
#define MOD ((ll)1e9 + 7)

ll N, M, K, fa[202020];
ll i, j, k, l;

ll moddiv(ll a, ll b) {
  for (ll m = MOD - 2; m > 0; m /= 2) {
    if (m & 1) {
      a = a * b % MOD;
    }
    b = b * b % MOD;
  }
  return a;
}

int main() {
  scanf("%lld%lld%lld", &N, &M, &K);
  fa[0] = 1;
  fornum(i, 0, N * M) { fa[i + 1] = fa[i] * (i + 1) % MOD; }
  ll n = moddiv(((N * (N + 1) % MOD * (2 * N + 1) % MOD) + MOD -
                 (3 * N * (N + 1) % MOD)) %
                    MOD,
                12) *
         M % MOD * M % MOD;
  ll m = moddiv(((M * (M + 1) % MOD * (2 * M + 1) % MOD) + MOD -
                 (3 * M * (M + 1) % MOD)) %
                    MOD,
                12) *
         N % MOD * N % MOD;
  // printf("%lld %lld ", n, m);
  printf("%lld", (n + m) % MOD *
                     moddiv(fa[N * M - 2], fa[K - 2] * fa[N * M - K] % MOD) %
                     MOD);
  return 0;
}
Problem p03039 (C++)

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

using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;

#define REP(i, n, m) for (ll i = n; i < (ll)m; ++i)
#define IREP(i, n, m) for (ll i = n - 1; i >= m; --i)
#define rep(i, n) REP(i, 0, n)
#define irep(i, n) IREP(i, n, 0)
#define all(v) v.begin(), v.end()

ll pow_mod(ll n, ll m, ll mod = 1000000007) {
  ll a = 1;
  while (m > 0) {
    if (m & 1)
      a = a * n % mod;
    n = n * n % mod;
    m >>= 1;
  }
  return a;
}

ll combi(ll n, ll m, ll mod = 1000000007) {
  static const ll N_MAX = 100000;
  static vll fac(N_MAX + 1, -1);
  static vll ifac(N_MAX + 1, -1);

  if (fac[0] < 0) {
    fac[0] = ifac[0] = 1;
    rep(i, N_MAX) {
      fac[i + 1] = fac[i] * (i + 1) % mod;
      ifac[i + 1] = ifac[i] * pow_mod(i + 1, mod - 2, mod) % mod;
    }
  }

  if (n <= 0 && m <= 0)
    return 1;
  if (n < m || n < 0)
    return 0;
  return (ifac[n - m] * ifac[m] % mod) * fac[n] % mod;
}

int main() {
  cout << setprecision(20);
  ll n, m, k;
  cin >> n >> m >> k;

  ll count = 0;
  REP(d, 1, n) {
    ll a = d * (n - d) * m * m % 1000000007;
    a = a * combi(n * m - 2, k - 2) % 1000000007;
    count = (count + a) % 1000000007;
  }
  REP(d, 1, m) {
    ll a = d * (m - d) * n * n % 1000000007;
    a = a * combi(n * m - 2, k - 2) % 1000000007;
    count = (count + a) % 1000000007;
  }

  cout << count << endl;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;

#define REP(i, n, m) for (ll i = n; i < (ll)m; ++i)
#define IREP(i, n, m) for (ll i = n - 1; i >= m; --i)
#define rep(i, n) REP(i, 0, n)
#define irep(i, n) IREP(i, n, 0)
#define all(v) v.begin(), v.end()

ll pow_mod(ll n, ll m, ll mod = 1000000007) {
  ll a = 1;
  while (m > 0) {
    if (m & 1)
      a = a * n % mod;
    n = n * n % mod;
    m >>= 1;
  }
  return a;
}

ll combi(ll n, ll m, ll mod = 1000000007) {
  static const ll N_MAX = 200000;
  static vll fac(N_MAX + 1, -1);
  static vll ifac(N_MAX + 1, -1);

  if (fac[0] < 0) {
    fac[0] = ifac[0] = 1;
    rep(i, N_MAX) {
      fac[i + 1] = fac[i] * (i + 1) % mod;
      ifac[i + 1] = ifac[i] * pow_mod(i + 1, mod - 2, mod) % mod;
    }
  }

  if (n <= 0 && m <= 0)
    return 1;
  if (n < m || n < 0)
    return 0;
  return (ifac[n - m] * ifac[m] % mod) * fac[n] % mod;
}

int main() {
  cout << setprecision(20);
  ll n, m, k;
  cin >> n >> m >> k;

  ll count = 0;
  REP(d, 1, n) {
    ll a = d * (n - d) * m * m % 1000000007;
    a = a * combi(n * m - 2, k - 2) % 1000000007;
    count = (count + a) % 1000000007;
  }
  REP(d, 1, m) {
    ll a = d * (m - d) * n * n % 1000000007;
    a = a * combi(n * m - 2, k - 2) % 1000000007;
    count = (count + a) % 1000000007;
  }

  cout << count << endl;
}
Problem p03039 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>

using namespace std;

#define int long long

int INF = 1e9 + 7;

int myPow(int n, int m) {
  int res = 1;
  while (m > 0) {
    if (m & 1) {
      res *= n;
      res %= INF;
    }
    n = n * n % INF;
    m >>= 1;
  }
  return res;
}

// n^(INF-2)
int invMod(int n) { return myPow(n, INF - 2); }

vector<int> frac((int)1e5);

void set_frac() {
  frac[0] = 1;
  for (int i = 1; i < frac.size(); i++) {
    frac[i] = frac[i - 1] * i % INF;
  }
}

// nCm = n!/m!/(n-m)!
int comb(int n, int m) {
  int res = frac[n];
  res = res * invMod(frac[m]) % INF;
  res = res * invMod(frac[n - m]) % INF;
  return res;
}

signed main() {
  int N, M, K;
  cin >> N >> M >> K;

  int sum_cost = 0;

  for (int i = 1; i < M; i++) {
    for (int j = 1; j < N; j++) {
      // diag
      sum_cost += (i + j) * (M - i) * (N - j) * 2;
      sum_cost %= INF;
    }
  }

  for (int i = 1; i < N; i++) {
    sum_cost += i * M * (N - i);
    sum_cost %= INF;
  }

  for (int i = 1; i < M; i++) {
    sum_cost += i * N * (M - i);
    sum_cost %= INF;
  }

  set_frac();
  cout << sum_cost * comb(N * M - 2, K - 2) % INF << endl;
}

Fixed submission:
#include <iostream>
#include <vector>

using namespace std;

#define int long long

int INF = 1e9 + 7;

int myPow(int n, int m) {
  int res = 1;
  while (m > 0) {
    if (m & 1) {
      res *= n;
      res %= INF;
    }
    n = n * n % INF;
    m >>= 1;
  }
  return res;
}

// n^(INF-2)
int invMod(int n) { return myPow(n, INF - 2); }

vector<int> frac((int)1e6);

void set_frac() {
  frac[0] = 1;
  for (int i = 1; i < frac.size(); i++) {
    frac[i] = frac[i - 1] * i % INF;
  }
}

// nCm = n!/m!/(n-m)!
int comb(int n, int m) {
  int res = frac[n];
  res = res * invMod(frac[m]) % INF;
  res = res * invMod(frac[n - m]) % INF;
  return res;
}

signed main() {
  int N, M, K;
  cin >> N >> M >> K;

  int sum_cost = 0;

  for (int i = 1; i < M; i++) {
    for (int j = 1; j < N; j++) {
      // diag
      sum_cost += (i + j) * (M - i) * (N - j) * 2;
      sum_cost %= INF;
    }
  }

  for (int i = 1; i < N; i++) {
    sum_cost += i * M * (N - i);
    sum_cost %= INF;
  }

  for (int i = 1; i < M; i++) {
    sum_cost += i * N * (M - i);
    sum_cost %= INF;
  }

  set_frac();
  cout << sum_cost * comb(N * M - 2, K - 2) % INF << endl;
}
Problem p03039 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;
ll kai[200001];
ll gyaku[200001];

// aのp乗を求める
ll jou(ll a, ll p) {
  ll ret = 1;
  ll b2[40];
  b2[0] = a;
  for (int i = 1; i < 40; i++) {
    b2[i] = b2[i - 1] * b2[i - 1];
    b2[i] %= MOD;
    if (b2[i] < 0)
      b2[i] += MOD;
  }

  for (ll bit = 0; bit < 40; bit++) {
    ll tmp = (p & (1LL << bit));
    if (tmp > 0) {
      ret *= b2[bit];
      ret %= MOD;
      if (ret < 0)
        ret += MOD;
    }
  }
  return ret;
}

int main() {
  int n, m, k;
  ll ans = 0;
  int cnt = 0;
  cin >> n >> m >> k;
  // 階乗
  kai[0] = 1;
  for (ll i = 1; i < 200001; i++) {
    kai[i] = kai[i - 1] * i;
    kai[i] %= MOD;
    if (kai[i] < 0)
      kai[i] += MOD;
  }
  // 逆元
  for (int i = 0; i < 200001; i++)
    gyaku[i] = jou(kai[i], MOD - 2);

  for (int i = 0; i < n * m; i++) {
    for (int j = i + 1; j < n * m; j++) {
      ll tmp = abs(i % m - j % m) + abs(i / m - j / m); // コストを求める
      // cerr<<tmp<<" ";
      tmp *= kai[n * m - 2];
      tmp %= MOD;
      tmp *= gyaku[k - 2];
      tmp %= MOD;
      tmp *= gyaku[n * m - k];
      tmp %= MOD;
      // cerr<<tmp<<endl;
      if (tmp < 0)
        tmp += MOD;
      ans += tmp;
      ans %= MOD;
    }
  }
  if (ans < 0)
    ans += MOD;
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;
ll kai[200001];
ll gyaku[200001];

// aのp乗を求める
ll jou(ll a, ll p) {
  ll ret = 1;
  ll b2[40];
  b2[0] = a;
  for (int i = 1; i < 40; i++) {
    b2[i] = b2[i - 1] * b2[i - 1];
    b2[i] %= MOD;
    if (b2[i] < 0)
      b2[i] += MOD;
  }

  for (ll bit = 0; bit < 40; bit++) {
    ll tmp = (p & (1LL << bit));
    if (tmp > 0) {
      ret *= b2[bit];
      ret %= MOD;
      if (ret < 0)
        ret += MOD;
    }
  }
  return ret;
}

int main() {
  int n, m, k;
  ll ans = 0;
  int cnt = 0;
  cin >> n >> m >> k;
  // 階乗
  kai[0] = 1;
  for (ll i = 1; i < 200001; i++) {
    kai[i] = kai[i - 1] * i;
    kai[i] %= MOD;
    if (kai[i] < 0)
      kai[i] += MOD;
  }
  // 逆元
  for (int i = 0; i < 200001; i++)
    gyaku[i] = jou(kai[i], MOD - 2);

  for (int i = 1; i < n * m; i++) {
    ll tmp = abs(i % m - 0 % m) + abs(i / m - 0 / m); // コストを求める
    // cerr<<tmp<<" ";
    tmp *= kai[n * m - 2];
    tmp %= MOD;
    tmp *= gyaku[k - 2];
    tmp %= MOD;
    tmp *= gyaku[n * m - k];
    tmp %= MOD;
    // cerr<<tmp<<endl;
    // 個数はこの四角形が入る個数分
    tmp *= (ll)(m - i % m);
    tmp %= MOD;
    tmp *= (ll)(n - i / m);
    tmp %= MOD;
    if (i % m > 0 && i / m > 0)
      tmp *= 2;
    tmp %= MOD;
    if (tmp < 0)
      tmp += MOD;
    ans += tmp;
    ans %= MOD;
  }
  if (ans < 0)
    ans += MOD;
  cout << ans << endl;
  return 0;
}
Problem p03039 (C++)

Buggy submission (status: Time Limit Exceeded):

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#include <vector>
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
using namespace std;

typedef long long ll;

const ll MOD = 1000000007;
const int N = 200000;
ll fac[N * 2 + 10];
ll invfac[N * 2 + 10];

ll mypow(ll x, ll n) {
  if (n == 0)
    return 1;
  if (n % 2 == 0)
    return mypow(x * x % MOD, n / 2);
  return mypow(x * x % MOD, n / 2) * x % MOD;
}

inline ll cmb(ll n, ll k) {
  if (k < 0 || n < k)
    return 0;
  return fac[n] * invfac[k] % MOD * invfac[n - k] % MOD;
}

int main(void) {
  fac[0] = 1;
  REP(i, N * 2) { fac[i + 1] = fac[i] * (i + 1) % MOD; }
  invfac[N * 2] = mypow(fac[N * 2], MOD - 2);
  for (int i = N * 2; i > 0; --i) {
    invfac[i - 1] = invfac[i] * i % MOD;
  }
  ll n, m, k;
  cin >> n >> m >> k;
  ll res = 0;
  for (ll d = 1; d <= n + m - 2; ++d) {
    ll num = 0;
    REP(i, d + 1) {
      num = (num + max(0LL, (n - i) * (m - d + i))) % MOD;
      if (i != 0 && i != d) {
        num = (num + max(0LL, (n - i) * (m - d + i))) % MOD;
      }
    }
    res = (res + num * d) % MOD;
  }
  cout << res * cmb(n * m - 2, k - 2) % MOD << endl;
  return 0;
}


Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#include <vector>
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
using namespace std;

typedef long long ll;

const ll MOD = 1000000007;
const int N = 200000;
ll fac[N * 2 + 10];
ll invfac[N * 2 + 10];

ll mypow(ll x, ll n) {
  if (n == 0)
    return 1;
  if (n % 2 == 0)
    return mypow(x * x % MOD, n / 2);
  return mypow(x * x % MOD, n / 2) * x % MOD;
}

inline ll cmb(ll n, ll k) {
  if (k < 0 || n < k)
    return 0;
  return fac[n] * invfac[k] % MOD * invfac[n - k] % MOD;
}

int main(void) {
  fac[0] = 1;
  REP(i, N * 2) { fac[i + 1] = fac[i] * (i + 1) % MOD; }
  invfac[N * 2] = mypow(fac[N * 2], MOD - 2);
  for (int i = N * 2; i > 0; --i) {
    invfac[i - 1] = invfac[i] * i % MOD;
  }
  ll n, m, k;
  cin >> n >> m >> k;
  ll res = 0;
  for (ll d = 1; d <= n - 1; ++d) {
    res = (res + d * m % MOD * m % MOD * (n - d) % MOD) % MOD;
  }
  for (ll d = 1; d <= m - 1; ++d) {
    res = (res + d * n % MOD * n % MOD * (m - d) % MOD) % MOD;
  }
  cout << res * cmb(n * m - 2, k - 2) % MOD << endl;
  return 0;
}
Problem p03039 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define rep(i, s, n) for (int i = s; i < (int)(n); i++)
typedef long long int ll;
using namespace std;
// 分割数
ll n;
const int MAX_N = 100010;
const int M = 1000000007;

ll f[MAX_N], rf[MAX_N];
ll inv(ll x) {
  ll res = 1;
  ll k = M - 2;
  ll y = x;
  while (k) {
    if (k & 1)
      res = (res * y) % M;
    y = (y * y) % M;
    k /= 2;
  }
  return res;
}
void init() {
  f[0] = 1;
  rep(i, 1, MAX_N) f[i] = (f[i - 1] * i) % M;
  rep(i, 0, MAX_N) rf[i] = inv(f[i]);
}
//---------------------------------------------------------------------------------------------------
ll C(int n, int k) {
  ll a = f[n];      // = n!
  ll b = rf[n - k]; // = (n-k)!
  ll c = rf[k];     // = k!

  ll bc = (b * c) % M;

  return (a * bc) % M;
}

int main() {
  init();
  ll k, m;
  cin >> n >> m >> k;
  ll x = 0;
  ll y = 0;
  REP(i, m) {
    ll t = (i * (m - i)) % M;
    t = (t * ((n * n) % M)) % M;
    x += t;
    x %= M;
  }
  x *= C(n * m - 2, k - 2);
  x %= M;
  REP(i, n) {
    ll t = (i * (n - i)) % M;
    t = (t * ((m * m) % M)) % M;
    y += t;
    y %= M;
  }
  y *= C(n * m - 2, k - 2);
  cout << (x + y) % M << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define rep(i, s, n) for (int i = s; i < (int)(n); i++)
typedef long long int ll;
using namespace std;
// 分割数
ll n;
const int MAX_N = 200010;
const int M = 1000000007;

ll f[MAX_N], rf[MAX_N];
ll inv(ll x) {
  ll res = 1;
  ll k = M - 2;
  ll y = x;
  while (k) {
    if (k & 1)
      res = (res * y) % M;
    y = (y * y) % M;
    k /= 2;
  }
  return res;
}
void init() {
  f[0] = 1;
  rep(i, 1, MAX_N) f[i] = (f[i - 1] * i) % M;
  rep(i, 0, MAX_N) rf[i] = inv(f[i]);
}
//---------------------------------------------------------------------------------------------------
ll C(int n, int k) {
  ll a = f[n];      // = n!
  ll b = rf[n - k]; // = (n-k)!
  ll c = rf[k];     // = k!

  ll bc = (b * c) % M;

  return (a * bc) % M;
}

int main() {
  init();
  ll k, m;
  cin >> n >> m >> k;
  ll x = 0;
  ll y = 0;
  REP(i, m) {
    ll t = (i * (m - i)) % M;
    t = (t * ((n * n) % M)) % M;
    x += t;
    x %= M;
  }
  x *= C(n * m - 2, k - 2);
  x %= M;
  REP(i, n) {
    ll t = (i * (n - i)) % M;
    t = (t * ((m * m) % M)) % M;
    y += t;
    y %= M;
  }
  y *= C(n * m - 2, k - 2);
  cout << (x + y) % M << endl;
}
Problem p03039 (Python)

Buggy submission (status: Time Limit Exceeded):
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10**9)
INF = 1 << 60
MOD = 1000000007


def main():
    N, M, K = map(int, readline().split())

    COM_MAX = N * M

    fac, finv, inv = [0] * (COM_MAX + 1), [0] * (COM_MAX + 1), [0] * (COM_MAX + 1)
    fac[0] = fac[1] = finv[0] = finv[1] = inv[1] = 1

    for i in range(2, COM_MAX + 1):
        fac[i] = fac[i - 1] * i % MOD
        inv[i] = MOD - inv[MOD % i] * (MOD // i) % MOD
        finv[i] = finv[i - 1] * inv[i] % MOD

    def com(n, r):
        if n < 0 or r < 0 or n < r:
            return 0
        return fac[n] * (finv[r] * finv[n - r] % MOD) % MOD

    def solve(n, m, k):
        ans = 0
        for j in range(1, k):
            tmp = 0
            for i in range(1, n):
                tmp = (tmp + com(m * i, j) * com(m * (n - i), k - j)) % MOD
            ans = (ans + tmp * j * (k - j)) % MOD

        return ans

    ans = (solve(N, M, K) + solve(M, N, K)) % MOD
    print(ans)
    return


if __name__ == "__main__":
    main()


Fixed submission:
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10**9)
INF = 1 << 60
MOD = 1000000007


def main():
    N, M, K = map(int, readline().split())

    COM_MAX = N * M

    fac, finv, inv = [0] * (COM_MAX + 1), [0] * (COM_MAX + 1), [0] * (COM_MAX + 1)
    fac[0] = fac[1] = finv[0] = finv[1] = inv[1] = 1

    for i in range(2, COM_MAX + 1):
        fac[i] = fac[i - 1] * i % MOD
        inv[i] = MOD - inv[MOD % i] * (MOD // i) % MOD
        finv[i] = finv[i - 1] * inv[i] % MOD

    def com(n, r):
        if n < 0 or r < 0 or n < r:
            return 0
        return fac[n] * (finv[r] * finv[n - r] % MOD) % MOD

    def solve(n, m, k):
        ans = 0
        for d in range(1, n):
            ans = (ans + com(n * m - 2, k - 2) * (m**2) * (n - d) * d) % MOD

        return ans

    ans = (solve(N, M, K) + solve(M, N, K)) % MOD
    print(ans)
    return


if __name__ == "__main__":
    main()
Problem p03039 (Python)

Buggy submission (status: Time Limit Exceeded):
def comb(n, k, mod=10**9 + 7):
    from functools import reduce
    from operator import mul

    k = min(k, n - k)
    x = reduce(mul, range(n - k + 1, n + 1), 1) % mod
    y = reduce(mul, range(1, k + 1), 1) % mod
    return x * pow(y, mod - 2, mod) % mod


def solve(string):
    n, m, k = map(int, string.split())
    mod = 10**9 + 7
    base = n * m * (n + m) * (n * m - 1) // 6 % mod
    return str(base * comb(n * m - 2, k - 2) % mod)


if __name__ == "__main__":
    print(solve(input()))


Fixed submission:
def comb(n, k, mod=10**9 + 7):
    from functools import reduce
    from operator import mul

    k = min(k, n - k)

    def calc(x, y):
        return x * y % mod

    x = reduce(calc, range(n - k + 1, n + 1), 1) % mod
    y = reduce(calc, range(1, k + 1), 1) % mod
    return x * pow(y, mod - 2, mod) % mod


def solve(string):
    n, m, k = map(int, string.split())
    mod = 10**9 + 7
    base = n * m * (n + m) * (n * m - 1) // 6 % mod
    return str(base * comb(n * m - 2, k - 2) % mod)


if __name__ == "__main__":
    print(solve(input()))
Problem p03039 (Python)

Buggy submission (status: Runtime Error):
import sys

readline = sys.stdin.readline
MOD = 10**9 + 7
INF = float("INF")
sys.setrecursionlimit(10**5)


def comb_mod(n, r):
    res = 1
    r = min(n - r, r)

    for i in range(r):
        res *= n - i
        res %= MOD
        res *= pow((r - i), -1, MOD)

    return res


def main():
    n, m, k = list(map(int, readline().split()))

    com = comb_mod(n * m - 2, k - 2)
    x = 0
    y = 0

    for i in range(n):
        right = n - 1 - i
        x += (right + 1) * right // 2
        x %= MOD

    for i in range(m):
        down = m - 1 - i
        y += (down + 1) * down // 2
        y %= MOD

    ans = 0
    ans += m**2 * x * com
    ans += n**2 * y * com
    ans %= MOD

    print(ans)


if __name__ == "__main__":
    main()


Fixed submission:
import sys

readline = sys.stdin.readline
MOD = 10**9 + 7
INF = float("INF")
sys.setrecursionlimit(10**5)


def comb_mod(n, r):
    res = 1
    r = min(n - r, r)

    for i in range(r):
        res *= n - i
        res %= MOD
        res *= pow((r - i), MOD - 2, MOD)

    return res


def main():
    n, m, k = list(map(int, readline().split()))

    com = comb_mod(n * m - 2, k - 2)
    x = 0
    y = 0

    for i in range(n):
        right = n - 1 - i
        x += (right + 1) * right // 2
        x %= MOD

    for i in range(m):
        down = m - 1 - i
        y += (down + 1) * down // 2
        y %= MOD

    ans = 0
    ans += m**2 * x * com
    ans += n**2 * y * com
    ans %= MOD

    print(ans)


if __name__ == "__main__":
    main()
Problem p03039 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> Pl;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define repr(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)

ll const INF = 1000000000000000000;
int const I_MAX = 2147483647;
ll const MOD = 1e9 + 7;
int const POW_MAX = 1e5;

// nCr
ll po[POW_MAX + 2];
ll mod_pow(ll x, ll n) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * x % MOD;
    x = x * x % MOD;
    n >>= 1;
  }
  return res;
}
ll mod_inv(ll x) { return mod_pow(x, MOD - 2) % MOD; }
ll comb(ll n, ll r) {
  if (!po[n]) {
    po[0] = 1;
    FOR(i, 1, n + 1) po[i] = po[i - 1] * i % MOD;
  }
  return (((po[n] * mod_inv(po[r])) % MOD) * mod_inv(po[n - r])) % MOD;
}
// greatest common divisor
ll gcd(ll a, ll b) {
  while (a % b != 0) {
    ll tmp = a;
    a = b;
    b = tmp % b;
  }
  return b;
}

int N, M;
ll K;

int main() {
  scanf("%d %d %lld", &N, &M, &K);

  ll comb_v = comb(N * M - 2, K - 2);

  ll sum = 0;
  FOR(i, 1, N) {
    ll m = (N - i) * M * M % MOD;
    sum += i * m * comb_v % MOD;
    sum %= MOD;
  }
  FOR(i, 1, M) {
    ll m = (M - i) * N * N % MOD;
    sum += i * m * comb_v % MOD;
    sum %= MOD;
  }
  printf("%lld\n", sum);
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> Pl;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define repr(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)

ll const INF = 1000000000000000000;
int const I_MAX = 2147483647;
ll const MOD = 1e9 + 7;
int const POW_MAX = 1e5;

// nCr
map<ll, ll> po;
ll mod_pow(ll x, ll n) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * x % MOD;
    x = x * x % MOD;
    n >>= 1;
  }
  return res;
}
ll mod_inv(ll x) { return mod_pow(x, MOD - 2) % MOD; }
ll comb(ll n, ll r) {
  if (!po[n]) {
    po[0] = 1;
    FOR(i, 1, n + 1) po[i] = po[i - 1] * i % MOD;
  }
  return (((po[n] * mod_inv(po[r])) % MOD) * mod_inv(po[n - r])) % MOD;
}
// greatest common divisor
ll gcd(ll a, ll b) {
  while (a % b != 0) {
    ll tmp = a;
    a = b;
    b = tmp % b;
  }
  return b;
}

int N, M;
ll K;

int main() {
  scanf("%d %d %lld", &N, &M, &K);

  ll comb_v = comb(N * M - 2, K - 2);

  ll sum = 0;
  FOR(i, 1, N) {
    ll m = (N - i) * M * M % MOD;
    sum += i * m * comb_v % MOD;
    sum %= MOD;
  }
  FOR(i, 1, M) {
    ll m = (M - i) * N * N % MOD;
    sum += i * m * comb_v % MOD;
    sum %= MOD;
  }
  printf("%lld\n", sum);
}