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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int N = 100 * 1000 + 10, mod = 1e9 + 7;
int r[N], c[N];
long long ans = 1, sr, sc, cnt;
int main() {
  int n, m;
  cin >> n >> m;
  memset(r, -1, sizeof(r));
  memset(c, -1, sizeof(c));
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    x--;
    if (r[x] != -1) {
      cout << 0;
      return 0;
    }
    r[x] = i;
  }
  for (int i = 0; i < m; i++) {
    int x;
    cin >> x;
    x--;
    if (c[x] != -1) {
      cout << 0;
      return 0;
    }
    c[x] = i;
  }
  for (int i = n * m - 1; i >= 0; i--) {
    if (r[i] >= 0 && c[i] >= 0) {
      cnt++, sr++, sc++;
      continue;
    }
    if (r[i] == -1 && c[i] == -1) {
      //			cout << "rc : i = " << i<< " : " << ans << " : :
      //";
      if (sr * sc - cnt <= 0) {
        cout << 0;
        return 0;
      }
      ans = (ans * (sr * sc - cnt)) % mod;
      //			cout << ans << endl;
      cnt++;
      continue;
    }
    if (r[i] == -1) {
      if (!sc) {
        cout << 0;
        return 0;
      }
      //			cout << "r : i = " << i << " : " << ans << " : :
      //";
      ans = ans * sc % mod;
      //			cout << ans << endl;
      cnt++, sr++;
      continue;
    }
    if (c[i] == -1) {
      if (!sr) {
        cout << 0;
        return 0;
      }
      //			cout << "c : i = " << i << " : " << ans << " : :
      //";
      ans = ans * sr % mod;
      //			cout << ans << endl;
      cnt++, sc++;
      continue;
    }
  }
  cout << ans;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 1000 * 1000 + 10, mod = 1e9 + 7;
int r[N], c[N];
long long ans = 1, sr, sc, cnt;
int main() {
  int n, m;
  cin >> n >> m;
  memset(r, -1, sizeof(r));
  memset(c, -1, sizeof(c));
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    x--;
    if (r[x] != -1) {
      cout << 0;
      return 0;
    }
    r[x] = i;
  }
  for (int i = 0; i < m; i++) {
    int x;
    cin >> x;
    x--;
    if (c[x] != -1) {
      cout << 0;
      return 0;
    }
    c[x] = i;
  }
  for (int i = n * m - 1; i >= 0; i--) {
    if (r[i] >= 0 && c[i] >= 0) {
      cnt++, sr++, sc++;
      continue;
    }
    if (r[i] == -1 && c[i] == -1) {
      //			cout << "rc : i = " << i<< " : " << ans << " : :
      //";
      if (sr * sc - cnt <= 0) {
        cout << 0;
        return 0;
      }
      ans = (ans * (sr * sc - cnt)) % mod;
      //			cout << ans << endl;
      cnt++;
      continue;
    }
    if (r[i] == -1) {
      if (!sc) {
        cout << 0;
        return 0;
      }
      //			cout << "r : i = " << i << " : " << ans << " : :
      //";
      ans = ans * sc % mod;
      //			cout << ans << endl;
      cnt++, sr++;
      continue;
    }
    if (c[i] == -1) {
      if (!sr) {
        cout << 0;
        return 0;
      }
      //			cout << "c : i = " << i << " : " << ans << " : :
      //";
      ans = ans * sr % mod;
      //			cout << ans << endl;
      cnt++, sc++;
      continue;
    }
  }
  cout << ans;
  return 0;
}
Problem p03152 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// #include <math.h>
#include <algorithm>
#include <vector>

typedef long long ll;
#define MIN(a, b) ((a) > (b) ? (b) : (a))
#define MAX(a, b) ((a) < (b) ? (b) : (a))

using namespace std;

#define MOD 1000000007

int main(int argc, char *argv[]) {
  long n, m;
  scanf("%ld %ld", &n, &m);

  vector<long> a(n);
  vector<long> b(n);
  vector<ll> d(n);

  long i;
  for (i = 0; i < n; i++) {
    scanf("%ld", &(a[i]));
  }
  for (i = 0; i < m; i++) {
    scanf("%ld", &(b[i]));
  }

  sort(a.begin(), a.end());
  for (i = 0; i < n - 1; i++) {
    if (a[i] == a[i + 1]) {
      printf("0\n");
      return 0;
    }
  }

  sort(b.begin(), b.end());
  for (i = 0; i < m - 1; i++) {
    if (b[i] == b[i + 1]) {
      printf("0\n");
      return 0;
    }
  }

  ll ans = 1;
  for (i = n * m; i >= 1; i--) {
    auto ita = lower_bound(a.begin(), a.end(), i);
    auto itb = lower_bound(b.begin(), b.end(), i);
    if ((*ita) == i && (*itb) == i) {
      ;
    } else if ((*ita) == i) {
      ans = ans * (b.end() - itb) % MOD;
    } else if ((*itb) == i) {
      ans = ans * (a.end() - ita) % MOD;
    } else {
      ans = ans * ((a.end() - ita) * (b.end() - itb) - (n * m - i)) % MOD;
    }
  }

  printf("%lld\n", ans);

  return 0;
}


Fixed submission:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// #include <math.h>
#include <algorithm>
#include <vector>

typedef long long ll;
#define MIN(a, b) ((a) > (b) ? (b) : (a))
#define MAX(a, b) ((a) < (b) ? (b) : (a))

using namespace std;

#define MOD 1000000007

int main(int argc, char *argv[]) {
  long n, m;
  scanf("%ld %ld", &n, &m);

  vector<long> a(n);
  vector<long> b(m);

  long i;
  for (i = 0; i < n; i++) {
    scanf("%ld", &(a[i]));
  }
  for (i = 0; i < m; i++) {
    scanf("%ld", &(b[i]));
  }

  sort(a.begin(), a.end());
  for (i = 0; i < n - 1; i++) {
    if (a[i] == a[i + 1]) {
      printf("0\n");
      return 0;
    }
  }

  sort(b.begin(), b.end());
  for (i = 0; i < m - 1; i++) {
    if (b[i] == b[i + 1]) {
      printf("0\n");
      return 0;
    }
  }

  ll ans = 1;
  for (i = n * m; i >= 1; i--) {
    auto ita = lower_bound(a.begin(), a.end(), i);
    auto itb = lower_bound(b.begin(), b.end(), i);
    if ((*ita) == i && (*itb) == i) {
      ;
    } else if ((*ita) == i) {
      ans = ans * (b.end() - itb) % MOD;
    } else if ((*itb) == i) {
      ans = ans * (a.end() - ita) % MOD;
    } else {
      ans = ans * ((a.end() - ita) * (b.end() - itb) - (n * m - i)) % MOD;
    }
  }

  printf("%lld\n", ans);

  return 0;
}
Problem p03152 (C++)

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

using namespace std;

int main() {
  const Int MOD = 1e9 + 7;
  int N, M;
  cin >> N >> M;
  vector<int> a(N), b(M);
  vector<Int> cnt_a(100010, 0), cnt_b(100010, 0);
  bool ok = true;
  for (int i = 0; i < N; ++i) {
    cin >> a[i];
    ++cnt_a[a[i]];
    if (cnt_a[a[i]] > 1) {
      ok = false;
    }
  }
  for (int i = 0; i < M; ++i) {
    cin >> b[i];
    ++cnt_b[b[i]];
    if (cnt_b[b[i]] > 1) {
      ok = false;
    }
  }
  if (!ok) {
    cout << 0 << endl;
    return 0;
  }

  Int ans = 1;
  for (int i = N * M; i > 0; --i) {
    if (cnt_a[i] == 0 && cnt_b[i] == 0) {
      Int tmp = cnt_a[i + 1] * cnt_b[i + 1] % MOD;
      tmp = (tmp + MOD - (N * M - i)) % MOD;
      (ans *= tmp) %= MOD;
    } else if (cnt_a[i] != 1 || cnt_b[i] != 1) {
      if (cnt_a[i] > 0) {
        (ans *= cnt_b[i + 1]) %= MOD;
      } else {
        (ans *= cnt_a[i + 1]) %= MOD;
      }
    }

    cnt_a[i] += cnt_a[i + 1];
    cnt_b[i] += cnt_b[i + 1];
  }
  cout << ans % MOD << endl;

  return 0;
}


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

using namespace std;

int main() {
  const Int MOD = 1e9 + 7;
  int N, M;
  cin >> N >> M;
  vector<int> a(N), b(M);
  vector<Int> cnt_a(1000010, 0), cnt_b(1000010, 0);
  bool ok = true;
  for (int i = 0; i < N; ++i) {
    cin >> a[i];
    ++cnt_a[a[i]];
    if (cnt_a[a[i]] > 1) {
      ok = false;
    }
  }
  for (int i = 0; i < M; ++i) {
    cin >> b[i];
    ++cnt_b[b[i]];
    if (cnt_b[b[i]] > 1) {
      ok = false;
    }
  }
  if (!ok) {
    cout << 0 << endl;
    return 0;
  }

  Int ans = 1;
  for (int i = N * M; i > 0; --i) {
    if (cnt_a[i] == 0 && cnt_b[i] == 0) {
      Int tmp = cnt_a[i + 1] * cnt_b[i + 1] % MOD;
      tmp = (tmp + MOD - (N * M - i)) % MOD;
      (ans *= tmp) %= MOD;
    } else if (cnt_a[i] != 1 || cnt_b[i] != 1) {
      if (cnt_a[i] > 0) {
        (ans *= cnt_b[i + 1]) %= MOD;
      } else {
        (ans *= cnt_a[i + 1]) %= MOD;
      }
    }

    cnt_a[i] += cnt_a[i + 1];
    cnt_b[i] += cnt_b[i + 1];
  }
  cout << ans % MOD << endl;

  return 0;
}
Problem p03152 (C++)

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

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> A(N), B(N);
  for (int i = 0; i < N; i++)
    cin >> A[i];
  for (int i = 0; i < M; i++)
    cin >> B[i];
  sort(A.begin(), A.end());
  sort(B.begin(), B.end());

  for (int i = 1; i < N; i++)
    if (A[i] == A[i - 1]) {
      puts("0");
      return 0;
    }
  for (int i = 1; i < M; i++)
    if (B[i] == B[i - 1]) {
      puts("0");
      return 0;
    }

  int mod = 1e9 + 7;
  ll ans = 1;
  for (int i = N * M; i >= 1; i--) {
    if (binary_search(A.begin(), A.end(), i) &&
        binary_search(B.begin(), B.end(), i))
      continue;
    else if (binary_search(A.begin(), A.end(), i)) {
      ans = (ans * (B.end() - lower_bound(B.begin(), B.end(), i))) % mod;
    } else if (binary_search(B.begin(), B.end(), i)) {
      ans = (ans * (A.end() - lower_bound(A.begin(), A.end(), i))) % mod;
    } else {
      int h = A.end() - lower_bound(A.begin(), A.end(), i);
      int w = B.end() - lower_bound(B.begin(), B.end(), i);
      ans = (ans * (h * w - (N * M - i))) % mod;
    }
    if (ans == 0)
      break;
  }

  cout << ans << endl;
}


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

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> A(N), B(M);
  for (int i = 0; i < N; i++)
    cin >> A[i];
  for (int i = 0; i < M; i++)
    cin >> B[i];
  sort(A.begin(), A.end());
  sort(B.begin(), B.end());

  for (int i = 1; i < N; i++)
    if (A[i] == A[i - 1]) {
      puts("0");
      return 0;
    }
  for (int i = 1; i < M; i++)
    if (B[i] == B[i - 1]) {
      puts("0");
      return 0;
    }

  int mod = 1e9 + 7;
  ll ans = 1;
  for (int i = N * M; i >= 1; i--) {
    if (binary_search(A.begin(), A.end(), i) &&
        binary_search(B.begin(), B.end(), i))
      continue;
    else if (binary_search(A.begin(), A.end(), i)) {
      ans = (ans * (B.end() - lower_bound(B.begin(), B.end(), i))) % mod;
    } else if (binary_search(B.begin(), B.end(), i)) {
      ans = (ans * (A.end() - lower_bound(A.begin(), A.end(), i))) % mod;
    } else {
      int h = A.end() - lower_bound(A.begin(), A.end(), i);
      int w = B.end() - lower_bound(B.begin(), B.end(), i);
      ans = (ans * (h * w - (N * M - i))) % mod;
    }
    if (ans == 0)
      break;
  }

  cout << ans << endl;
}
Problem p03152 (C++)

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

const int mod = 1000000007;
int n, m, sa[1010], sb[1010];
long long ans = 1;
bool a[1000010], b[1000010];

int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    int x;
    scanf("%d", &x);
    if (a[x]) {
      puts("0");
      return 0;
    }
    a[x] = 1;
  }
  for (int i = 1; i <= m; i++) {
    int x;
    scanf("%d", &x);
    if (b[x]) {
      puts("0");
      return 0;
    }
    b[x] = 1;
  }
  for (int i = n * m; i > 0; i--) {
    sa[i] = sa[i + 1] + a[i];
    sb[i] = sb[i + 1] + b[i];
  }
  for (int i = 1; i <= n * m; i++) {
    if (a[i] && b[i])
      continue;
    else if (a[i]) {
      ans = ans * sb[i] % mod;
    } else if (b[i]) {
      ans = ans * sa[i] % mod;
    } else {
      ans = ans * ((sa[i] * sb[i] - n * m + i) % mod) % mod;
    }
  }
  printf("%lld\n", ans);
}

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

const int mod = 1000000007;
int n, m, sa[1000010], sb[1000010];
long long ans = 1;
bool a[1000010], b[1000010];

int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    int x;
    scanf("%d", &x);
    if (a[x]) {
      puts("0");
      return 0;
    }
    a[x] = 1;
  }
  for (int i = 1; i <= m; i++) {
    int x;
    scanf("%d", &x);
    if (b[x]) {
      puts("0");
      return 0;
    }
    b[x] = 1;
  }
  for (int i = n * m; i > 0; i--) {
    sa[i] = sa[i + 1] + a[i];
    sb[i] = sb[i + 1] + b[i];
  }
  for (int i = 1; i <= n * m; i++) {
    if (a[i] && b[i])
      continue;
    else if (a[i]) {
      ans = ans * sb[i] % mod;
    } else if (b[i]) {
      ans = ans * sa[i] % mod;
    } else {
      ans = ans * ((sa[i] * sb[i] - n * m + i) % mod) % mod;
    }
  }
  printf("%lld\n", ans);
}
Problem p03152 (Python)

Buggy submission (status: Runtime Error):
import bisect

N, M = map(int, input().split())
A = [int(x) for x in input().split()]
B = [int(x) for x in input().split()]
A.sort()
B.sort()
if len(set(A)) != N or len(set(B)) != M:
    print(0)
    quit()
ans = 1
mod = 10**9 + 7
for i in reversed(range(1, N * M + 1)):
    ai = bisect.bisect_left(A, i)
    bi = bisect.bisect_left(B, i)
    a_flag = A[ai] == i
    b_flag = B[bi] == i
    if a_flag and b_flag:
        pass
    elif a_flag:
        cand = M - bi
        ans = (ans * cand) % mod
    elif b_flag:
        cand = N - ai
        ans = (ans * cand) % mod
    else:
        cand = (N - ai) * (M - bi) - (N * M - i)
        if cand <= 0:
            print(0)
            quit()
        ans = (ans * cand) % mod
print(ans)


Fixed submission:
import bisect

N, M = map(int, input().split())
A = [int(x) for x in input().split()]
B = [int(x) for x in input().split()]
A.sort()
B.sort()
if len(set(A)) != N or len(set(B)) != M:
    print(0)
    quit()
if A[-1] != N * M or B[-1] != N * M:
    print(0)
    quit()
ans = 1
mod = 10**9 + 7
for i in reversed(range(1, N * M + 1)):
    ai = bisect.bisect_left(A, i)
    bi = bisect.bisect_left(B, i)
    a_flag = A[ai] == i
    b_flag = B[bi] == i
    if a_flag and b_flag:
        pass
    elif a_flag:
        cand = M - bi
        ans = (ans * cand) % mod
    elif b_flag:
        cand = N - ai
        ans = (ans * cand) % mod
    else:
        cand = (N - ai) * (M - bi) - (N * M - i)
        if cand <= 0:
            print(0)
            quit()
        ans = (ans * cand) % mod
print(ans)
Problem p03152 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 1e3 + 10;
const LL MOD = 1e9 + 7;
int n, m, px[N], py[N];

int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    int x;
    scanf("%d", &x);
    if (px[x]) {
      puts("0");
      return 0;
    }
    px[x] = 1;
  }
  for (int j = 1; j <= m; j++) {
    int y;
    scanf("%d", &y);
    if (py[y]) {
      puts("0");
      return 0;
    }
    py[y] = 1;
  }

  LL ans = 1, usedN = 0, usedM = 0;
  for (LL i = n * m; i; i--) {
    if (px[i] && py[i]) {
      usedN++;
      usedM++;
      continue;
    }
    if (px[i]) {
      usedN++;
      ans = (ans * usedM) % MOD;
    }
    if (py[i]) {
      usedM++;
      ans = (ans * usedN) % MOD;
    }
    if (!px[i] && !py[i])
      ans = (ans * (usedN * usedM - (n * m - i))) % MOD;
  }
  cout << ans;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 1e3 + 10;
const LL MOD = 1e9 + 7;
int n, m, px[N * N], py[N * N];

int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    int x;
    scanf("%d", &x);
    if (px[x]) {
      puts("0");
      return 0;
    }
    px[x] = 1;
  }
  for (int j = 1; j <= m; j++) {
    int y;
    scanf("%d", &y);
    if (py[y]) {
      puts("0");
      return 0;
    }
    py[y] = 1;
  }

  LL ans = 1, usedN = 0, usedM = 0;
  for (LL i = n * m; i; i--) {
    if (px[i] && py[i]) {
      usedN++;
      usedM++;
      continue;
    }
    if (px[i]) {
      usedN++;
      ans = (ans * usedM) % MOD;
    }
    if (py[i]) {
      usedM++;
      ans = (ans * usedN) % MOD;
    }
    if (!px[i] && !py[i])
      ans = (ans * (usedN * usedM - (n * m - i))) % MOD;
  }
  cout << ans;
  return 0;
}
Problem p03152 (Python)

Buggy submission (status: Runtime Error):
from bisect import bisect_left

MOD = 10**9 + 7

N, M = map(int, input().split())

a = list(map(int, input().split()))
a = sorted(a)
la = len(a)
sa = set(a)
lsa = len(sa)
if la != lsa:
    print(0)
    exit()

b = list(map(int, input().split()))
b = sorted(b)
lb = len(b)
sb = set(b)
lsb = len(sb)
if lb != lsb:
    print(0)
    exit()

ans = 1
ai = 0
bi = 0
k = N * M
d = 0
while k > 0:
    ai = bisect_left(a, k)
    bi = bisect_left(b, k)

    if a[ai] == k:
        ta = 1
        if b[bi] == k:
            tb = 1
            t = ta * tb
        else:
            tb = lb - bi
            t = ta * tb
    else:
        ta = la - ai
        if b[bi] == k:
            tb = 1
            t = ta * tb
        else:
            tb = lb - bi
            t = ta * tb - d

    # print(ta, tb)
    ans *= t

    ans %= MOD
    # print(ans)
    k -= 1
    d += 1

print(ans)


Fixed submission:
from bisect import bisect_left

MOD = 10**9 + 7

N, M = map(int, input().split())

a = list(map(int, input().split()))
a = sorted(a)
la = len(a)
sa = set(a)
lsa = len(sa)
if la != lsa:
    print(0)
    exit()

b = list(map(int, input().split()))
b = sorted(b)
lb = len(b)
sb = set(b)
lsb = len(sb)
if lb != lsb:
    print(0)
    exit()

ans = 1
ai = 0
bi = 0
k = N * M
d = 0
while k > 0:
    ai = bisect_left(a, k)
    bi = bisect_left(b, k)

    if ai == la or bi == lb:
        print(0)
        exit()

    if a[ai] == k:
        ta = 1
        if b[bi] == k:
            tb = 1
            t = ta * tb
        else:
            tb = lb - bi
            t = ta * tb
    else:
        ta = la - ai
        if b[bi] == k:
            tb = 1
            t = ta * tb
        else:
            tb = lb - bi
            t = ta * tb - d

    # print(ta, tb)
    ans *= t

    ans %= MOD
    # print(ans)
    k -= 1
    d += 1

print(ans)
Problem p03152 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int M = 1e3 + 4;
const int MD = 1e9 + 7;
int a[M], b[M], c[M], d[M], cnt = 0, cnt1 = 0;
ll ans = 1;
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    int x;
    cin >> x;
    a[i] = x;
    c[x]++;
    if (c[x] > 1) {
      cout << "0";
      return 0;
    }
  }
  for (int i = 1; i <= m; i++) {
    int x;
    cin >> x;
    b[i] = x;
    d[x]++;
    if (d[x] > 1) {
      cout << "0";
      return 0;
    }
  }
  for (int i = n * m; i >= 1; i--) {
    if (c[i] && d[i])
      continue;
    else if (c[i]) {
      cnt = 0;
      for (int j = 1; j <= m; j++) {
        if (b[j] > i)
          cnt++;
      }
      ans = (ans * cnt) % MD;
    } else if (d[i]) {
      cnt = 0;
      for (int j = 1; j <= n; j++) {
        if (a[j] > i)
          cnt++;
      }
      ans = (ans * cnt) % MD;
    } else {
      cnt = cnt1 = 0;
      for (int j = 1; j <= n; j++) {
        if (a[j] > i)
          cnt++;
      }
      for (int j = 1; j <= m; j++) {
        if (b[j] > i)
          cnt1++;
      }
      ll tp = cnt * cnt1 - (n * m - i);
      ans = (ans * tp) % MD;
    }
    // cout<<ans<<" ";
  }
  cout << ans;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int M = 1e3 + 4;
const int MD = 1e9 + 7;
int a[M], b[M], c[M * M], d[M * M], cnt = 0, cnt1 = 0;
ll ans = 1;
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    int x;
    cin >> x;
    a[i] = x;
    c[x]++;
    if (c[x] > 1) {
      cout << "0";
      return 0;
    }
  }
  for (int i = 1; i <= m; i++) {
    int x;
    cin >> x;
    b[i] = x;
    d[x]++;
    if (d[x] > 1) {
      cout << "0";
      return 0;
    }
  }
  for (int i = n * m; i >= 1; i--) {
    if (c[i] && d[i])
      continue;
    else if (c[i]) {
      cnt = 0;
      for (int j = 1; j <= m; j++) {
        if (b[j] > i)
          cnt++;
      }
      ans = (ans * cnt) % MD;
    } else if (d[i]) {
      cnt = 0;
      for (int j = 1; j <= n; j++) {
        if (a[j] > i)
          cnt++;
      }
      ans = (ans * cnt) % MD;
    } else {
      cnt = cnt1 = 0;
      for (int j = 1; j <= n; j++) {
        if (a[j] > i)
          cnt++;
      }
      for (int j = 1; j <= m; j++) {
        if (b[j] > i)
          cnt1++;
      }
      ll tp = cnt * cnt1 - (n * m - i);
      ans = (ans * tp) % MD;
    }
    // cout<<ans<<" ";
  }
  cout << ans;
  return 0;
}
Problem p03152 (Python)

Buggy submission (status: Runtime Error):
from collections import Counter


N, M = map(int, input().split())

A = list(map(int, input().split()))
B = list(map(int, input().split()))

A.sort()
B.sort()
A_cnt = Counter(A)
B_cnt = Counter(B)
if len(A) > 0:
    if max(A_cnt.values()) > 1:
        print(0)
        exit()
if len(B) > 0:
    if max(B_cnt.values()) > 1:
        print(0)
        exit()
if A[N - 1] != N * M and B[M - 1] != N * M:
    print(0)
    exit()

a = 0
b = 0
i = 1
C = [0] * (N * M)
while i <= N * M:
    if i == A[a] and i == B[b]:
        C[i - 1] = 1
        a += 1
        b += 1
    elif i == A[a] and i != B[b]:
        C[i - 1] = M - b
        a += 1
    elif i != A[a] and i == B[b]:
        C[i - 1] = N - a
        b += 1
    else:
        C[i - 1] = (N - a) * (M - b) - (N * M - i)
    i += 1

ans = 1
mod = 10**9 + 7
for c in C:
    ans = (ans * c) % mod

print(ans)


Fixed submission:
from collections import Counter


N, M = map(int, input().split())

A = list(map(int, input().split()))
B = list(map(int, input().split()))

A.sort()
B.sort()
A_cnt = Counter(A)
B_cnt = Counter(B)
if len(A) > 0:
    if max(A_cnt.values()) > 1:
        print(0)
        exit()
if len(B) > 0:
    if max(B_cnt.values()) > 1:
        print(0)
        exit()
if A[N - 1] != N * M or B[M - 1] != N * M:
    print(0)
    exit()

a = 0
b = 0
i = 1
C = [0] * (N * M)
while i <= N * M:
    if i == A[a] and i == B[b]:
        C[i - 1] = 1
        a += 1
        b += 1
    elif i == A[a] and i != B[b]:
        C[i - 1] = M - b
        a += 1
    elif i != A[a] and i == B[b]:
        C[i - 1] = N - a
        b += 1
    else:
        C[i - 1] = (N - a) * (M - b) - (N * M - i)
    i += 1

ans = 1
mod = 10**9 + 7
for c in C:
    ans = (ans * c) % mod

print(ans)
Problem p03153 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
#define F first
#define S second
#define RE register
#define random(x) (rand() % x)
#define LOG(a, b) (log(a) / log(b))
#define N 100005
#define mod 998244353
#define INF 0x3f3f3f3f
#define LINF 0x3f3f3f3f3f3f3f3fll
int n;
ll d, a[N], ans = 0, dpl[N], dpr[N];
int main() {
  scanf("%d%lld", &n, &d);
  for (int i = 1; i <= n; ++i) {
    scanf("%lld", a + i);
    ans += a[i];
  }
  ans += d * (n - 1);
  dpl[1] = a[1];
  for (int i = 2; i <= n; ++i)
    dpl[i] = min(dpl[i - 1] + d, a[i]);
  dpr[n] = a[n];
  for (int i = n - 1; i; --i)
    dpr[i] = min(dpr[i + 1] + d, a[i]);
  for (int i = 2; i < n; ++i)
    ans += min(dpl[i], dpr[i]);
  printf("%lld\n", ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
#define F first
#define S second
#define RE register
#define random(x) (rand() % x)
#define LOG(a, b) (log(a) / log(b))
#define N 200005
#define mod 998244353
#define INF 0x3f3f3f3f
#define LINF 0x3f3f3f3f3f3f3f3fll
int n;
ll d, a[N], ans = 0, dpl[N], dpr[N];
int main() {
  scanf("%d%lld", &n, &d);
  for (int i = 1; i <= n; ++i) {
    scanf("%lld", a + i);
    ans += a[i];
  }
  ans += d * (n - 1);
  dpl[1] = a[1];
  for (int i = 2; i <= n; ++i)
    dpl[i] = min(dpl[i - 1] + d, a[i]);
  dpr[n] = a[n];
  for (int i = n - 1; i; --i)
    dpr[i] = min(dpr[i + 1] + d, a[i]);
  for (int i = 2; i < n; ++i)
    ans += min(dpl[i], dpr[i]);
  printf("%lld\n", ans);
  return 0;
}
Problem p03153 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
typedef long long ll;
int n, tot, d, a[N], f[N];
struct node {
  int u, v;
  node(int u = 0, int v = 0, ll w = 0) : u(u), v(v), w(w) {}
  ll w;
  bool operator<(const node &o) const { return w < o.w; }
} e[N * 10];
void solve(int l, int r) {
  if (l == r)
    return;
  int m = l + r >> 1;
  ll p1 = l, p2 = m + 1, s = 1e18;
  for (ll i = l; i <= m; i++)
    if (-i * d + a[i] < s)
      s = -i * d + a[i], p1 = i;
  s = 1e18;
  for (ll i = m + 1; i <= r; i++)
    if (i * d + a[i] < s)
      s = i * d + a[i], p2 = i;
  for (ll i = l; i <= m; i++) {
    tot++;
    e[tot] = node(i, p2, -i * d + a[i] + p2 * d + a[p2]);
  }
  for (ll i = m + 1; i <= r; i++) {
    tot++;
    e[tot] = node(p1, i, -p1 * d + a[p1] + i * d + a[i]);
  }
  solve(l, m);
  solve(m + 1, r);
}
int getf(int x) { return f[x] == x ? x : f[x] = getf(f[x]); }
int main() {
  scanf("%d%d", &n, &d);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  solve(1, n);
  sort(e + 1, e + 1 + tot);
  for (int i = 1; i <= n; i++)
    f[i] = i;
  ll ans = 0;
  for (int i = 1; i <= tot; i++) {
    int fu = getf(e[i].u), fv = getf(e[i].v);
    if (fu == fv)
      continue;
    ans += e[i].w;
    f[fu] = fv;
  }
  printf("%lld\n", ans);
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
typedef long long ll;
int n, tot, d, a[N], f[N];
struct node {
  int u, v;
  node(int u = 0, int v = 0, ll w = 0) : u(u), v(v), w(w) {}
  ll w;
  bool operator<(const node &o) const { return w < o.w; }
} e[N * 20];
void solve(int l, int r) {
  if (l == r)
    return;
  int m = l + r >> 1;
  ll p1 = l, p2 = m + 1, s = 1e18;
  for (ll i = l; i <= m; i++)
    if (-i * d + a[i] < s)
      s = -i * d + a[i], p1 = i;
  s = 1e18;
  for (ll i = m + 1; i <= r; i++)
    if (i * d + a[i] < s)
      s = i * d + a[i], p2 = i;
  for (ll i = l; i <= m; i++) {
    tot++;
    e[tot] = node(i, p2, -i * d + a[i] + p2 * d + a[p2]);
  }
  for (ll i = m + 1; i <= r; i++) {
    tot++;
    e[tot] = node(p1, i, -p1 * d + a[p1] + i * d + a[i]);
  }
  solve(l, m);
  solve(m + 1, r);
}
int getf(int x) { return f[x] == x ? x : f[x] = getf(f[x]); }
int main() {
  scanf("%d%d", &n, &d);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  solve(1, n);
  sort(e + 1, e + 1 + tot);
  for (int i = 1; i <= n; i++)
    f[i] = i;
  ll ans = 0;
  for (int i = 1; i <= tot; i++) {
    int fu = getf(e[i].u), fv = getf(e[i].v);
    if (fu == fv)
      continue;
    ans += e[i].w;
    f[fu] = fv;
  }
  printf("%lld\n", ans);
}
Problem p03153 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MAX_N = 100005;
LL A[MAX_N], dp1[MAX_N], dp2[MAX_N];
LL ans, D;
int n;
int main() {
  cin >> n >> D;
  ans += (n - 1) * D;
  for (int i = 0; i < n; i++) {
    cin >> A[i];
    ans += A[i];
  }
  dp1[0] = A[0];
  dp2[n - 1] = A[n - 1];
  for (int i = 1; i < n; i++)
    dp1[i] = min(dp1[i - 1] + D, A[i]);
  for (int i = n - 2; i >= 0; i--)
    dp2[i] = min(dp2[i + 1] + D, A[i]);
  for (int i = 1; i < n - 1; i++)
    ans += min(dp1[i], dp2[i]);
  if (n == 1)
    ans = 0;
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MAX_N = 200005;
LL A[MAX_N], dp1[MAX_N], dp2[MAX_N];
LL ans, D;
int n;
int main() {
  cin >> n >> D;
  ans += (n - 1) * D;
  for (int i = 0; i < n; i++) {
    cin >> A[i];
    ans += A[i];
  }
  dp1[0] = A[0];
  dp2[n - 1] = A[n - 1];
  for (int i = 1; i < n; i++)
    dp1[i] = min(dp1[i - 1] + D, A[i]);
  for (int i = n - 2; i >= 0; i--)
    dp2[i] = min(dp2[i + 1] + D, A[i]);
  for (int i = 1; i < n - 1; i++)
    ans += min(dp1[i], dp2[i]);
  if (n == 1)
    ans = 0;
  cout << ans << endl;
  return 0;
}
Problem p03153 (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++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define Would
#define you
#define please

// getintとかいうのそんなに速いのかなテスト

int getint() {
  char ch;
  while (!isdigit(ch = getchar()))
    ;
  int x = ch - 48;
  while (isdigit(ch = getchar()))
    x = x * 10 + ch - 48;
  return x;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll N, D;
  cin >> N >> D;
  ll kotae = (N - 1) * D;
  int A[200001];
  ll C[200001];
  C[0] = 1e18;
  rep1(i, N) {
    A[i] = getint();
    kotae += A[i];
    C[i] = min(C[i - 1] + D, (ll)A[i]);
  }
  ll mae = 1e18;
  for (int i = N - 1; i > 1; i--) {
    mae = min(mae + D, (ll)A[i + 1]);
    ll kari = min(mae, C[i - 1]) + D;
    kotae += min((ll)A[i], kari);
  }
  co(kotae);

  Would you please 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++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define Would
#define you
#define please

// getintとかいうのそんなに速いのかなテスト

int getint() {
  char ch;
  while (!isdigit(ch = getchar()))
    ;
  int x = ch - 48;
  while (isdigit(ch = getchar()))
    x = x * 10 + ch - 48;
  return x;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N = getint();
  ll D = getint();
  ll kotae = (N - 1) * D;
  int A[200001];
  ll C[200001];
  C[0] = 1e18;
  rep1(i, N) {
    A[i] = getint();
    kotae += A[i];
    C[i] = min(C[i - 1] + D, (ll)A[i]);
  }
  ll mae = 1e18;
  for (int i = N - 1; i > 1; i--) {
    mae = min(mae + D, (ll)A[i + 1]);
    ll kari = min(mae, C[i - 1]) + D;
    kotae += min((ll)A[i], kari);
  }
  co(kotae);

  Would you please return 0;
}
Problem p03153 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
using namespace std;
struct node {
  int u, v;
  ll w;
  bool operator<(const node &o) const { return w < o.w; }
} e[6000005];
int n, cot = 0, a[200005], fail[200005];
ll d;
void add(int l, int r) {
  if (l >= r)
    return;
  int m = l + r >> 1;
  ll mi = 1e18, pos;
  for (int i = l; i <= m; i++) {
    ll f = a[i] - d * i;
    if (f < mi)
      mi = f, pos = i;
  }
  for (int i = m + 1; i <= r; i++)
    e[cot].u = pos, e[cot].v = i, e[cot++].w = a[i] + a[pos] + d * (i - pos);
  mi = 1e18;
  for (int i = m + 1; i <= r; i++) {
    ll f = a[i] + d * i;
    if (f < mi)
      mi = f, pos = i;
  }
  for (int i = 1; i <= m; i++)
    e[cot].u = i, e[cot].v = pos, e[cot++].w = a[i] + a[pos] + d * (pos - i);
  add(l, m);
  add(m + 1, r);
}
int get_fa(int x) { return fail[x] == x ? x : fail[x] = get_fa(fail[x]); }
ll kruskal() {
  sort(e, e + cot);
  for (int i = 1; i <= 200000; i++)
    fail[i] = i;
  ll ans = 0;
  for (int i = 0; i < cot; i++) {
    int fu = get_fa(e[i].u), fv = get_fa(e[i].v);
    if (fu == fv)
      continue;
    fail[fu] = fv;
    ans += e[i].w;
  }
  return ans;
}
int main() {
  scanf("%d%lld", &n, &d);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  add(1, n);
  printf("%lld\n", kruskal());
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
using namespace std;
struct node {
  int u, v;
  ll w;
  bool operator<(const node &o) const { return w < o.w; }
} e[6000005];
int n, cot = 0, a[200005], fail[200005];
ll d;
void add(int l, int r) {
  if (l >= r)
    return;
  int m = l + r >> 1;
  ll mi = 1e18, pos;
  for (int i = l; i <= m; i++) {
    ll f = a[i] - d * i;
    if (f < mi)
      mi = f, pos = i;
  }
  for (int i = m + 1; i <= r; i++)
    e[cot].u = pos, e[cot].v = i, e[cot++].w = a[i] + a[pos] + d * (i - pos);
  mi = 1e18;
  for (int i = m + 1; i <= r; i++) {
    ll f = a[i] + d * i;
    if (f < mi)
      mi = f, pos = i;
  }
  for (int i = l; i <= m; i++)
    e[cot].u = i, e[cot].v = pos, e[cot++].w = a[i] + a[pos] + d * (pos - i);
  add(l, m);
  add(m + 1, r);
}
int get_fa(int x) { return fail[x] == x ? x : fail[x] = get_fa(fail[x]); }
ll kruskal() {
  sort(e, e + cot);
  for (int i = 1; i <= 200000; i++)
    fail[i] = i;
  ll ans = 0;
  for (int i = 0; i < cot; i++) {
    int fu = get_fa(e[i].u), fv = get_fa(e[i].v);
    if (fu == fv)
      continue;
    fail[fu] = fv;
    ans += e[i].w;
  }
  return ans;
}
int main() {
  scanf("%d%lld", &n, &d);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  add(1, n);
  printf("%lld\n", kruskal());
}
Problem p03154 (C++)

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

using namespace std;

const int MOD = 1000000007;

int main() {
  const int maxSize = 2000001;
  vector<long long> inv(maxSize);
  vector<long long> fact(maxSize);
  vector<long long> factInv(maxSize);
  for (int i = 0; i < 2; i++)
    inv[i] = fact[i] = factInv[i] = 1;
  for (int i = 2; i < maxSize; i++) {
    inv[i] = inv[MOD % i] * (MOD - MOD / i) % MOD;
    fact[i] = fact[i - 1] * i % MOD;
    factInv[i] = factInv[i - 1] * inv[i] % MOD;
  }
  auto comb = [&](int n, int r) {
    if (n < r || r < 0)
      return 0LL;
    return fact[n] * factInv[n - r] % MOD * factInv[r] % MOD;
  };
  auto perm = [&](int n, int r) {
    if (n < r || r < 0)
      return 0LL;
    return fact[n] * factInv[n - r] % MOD;
  };
  int H, W, K;
  while (cin >> H >> W >> K) {
    long long res = K;
    for (int i = 0; i < K; i++) {
      long long add = 0;
      add += inv[H + W] * (W + H) % MOD;
      add +=
          2 * inv[H + W] * i % MOD * inv[H + W - 1] % MOD * W % MOD * H % MOD;
      add = (add * (K - i)) % MOD;
      res = (res + add) % MOD;
    }
    res = res * perm(H + W, K) % MOD;
    cout << res << endl;
  }
}

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

using namespace std;

const int MOD = 1000000007;

int main() {
  const int maxSize = 20000001;
  vector<long long> inv(maxSize);
  vector<long long> fact(maxSize);
  vector<long long> factInv(maxSize);
  for (int i = 0; i < 2; i++)
    inv[i] = fact[i] = factInv[i] = 1;
  for (int i = 2; i < maxSize; i++) {
    inv[i] = inv[MOD % i] * (MOD - MOD / i) % MOD;
    fact[i] = fact[i - 1] * i % MOD;
    factInv[i] = factInv[i - 1] * inv[i] % MOD;
  }
  auto comb = [&](int n, int r) {
    if (n < r || r < 0)
      return 0LL;
    return fact[n] * factInv[n - r] % MOD * factInv[r] % MOD;
  };
  auto perm = [&](int n, int r) {
    if (n < r || r < 0)
      return 0LL;
    return fact[n] * factInv[n - r] % MOD;
  };
  int H, W, K;
  while (cin >> H >> W >> K) {
    long long res = K;
    for (int i = 0; i < K; i++) {
      long long add = 0;
      add += inv[H + W] * (W + H) % MOD;
      add +=
          2 * inv[H + W] * i % MOD * inv[H + W - 1] % MOD * W % MOD * H % MOD;
      add = (add * (K - i)) % MOD;
      res = (res + add) % MOD;
    }
    res = res * perm(H + W, K) % MOD;
    cout << res << endl;
  }
}
Problem p03154 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define mp make_pair
#define si(x) int(x.size())
const int mod = 1000000007, MAX = 2000005, INF = 1 << 30;

ll inv[MAX], fac[MAX], finv[MAX];

void make() {

  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;

  for (int i = 2; i < MAX; i++) {
    inv[i] = mod - inv[mod % i] * (mod / i) % mod;
    fac[i] = fac[i - 1] * (ll)i % mod;
    finv[i] = finv[i - 1] * inv[i] % mod;
  }
}

ll comb(ll a, ll b) {
  if (a < b)
    return 0;
  return fac[a] * (finv[b] * finv[a - b] % mod) % mod;
}

ll perm(ll a, ll b) {
  if (a < b)
    return 0;
  return fac[a] * finv[a - b] % mod;
}

int main() {

  std::ifstream in("text.txt");
  std::cin.rdbuf(in.rdbuf());
  cin.tie(0);
  ios::sync_with_stdio(false);

  make();

  ll H, W, K;
  cin >> H >> W >> K;

  ll ans = 0;

  ans += perm(H + W, K) * K % mod;

  for (int t = 1; t <= K; t++) {
    ans += (H + W) * perm(H + W - 1, K - 1) % mod * (K + 1 - t) % mod;
    ans %= mod;
  }

  for (int t = 2; t <= K; t++) {
    ans += (H * W * 2) % mod * (t - 1) % mod * perm(H + W - 2, K - 2) % mod *
           (K + 1 - t) % mod;
    ans %= mod;
  }

  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define mp make_pair
#define si(x) int(x.size())
const int mod = 1000000007, MAX = 20000005, INF = 1 << 30;

ll inv[MAX], fac[MAX], finv[MAX];

void make() {

  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;

  for (int i = 2; i < MAX; i++) {
    inv[i] = mod - inv[mod % i] * (mod / i) % mod;
    fac[i] = fac[i - 1] * (ll)i % mod;
    finv[i] = finv[i - 1] * inv[i] % mod;
  }
}

ll comb(ll a, ll b) {
  if (a < b)
    return 0;
  return fac[a] * (finv[b] * finv[a - b] % mod) % mod;
}

ll perm(ll a, ll b) {
  if (a < b)
    return 0;
  return fac[a] * finv[a - b] % mod;
}

int main() {

  std::ifstream in("text.txt");
  std::cin.rdbuf(in.rdbuf());
  cin.tie(0);
  ios::sync_with_stdio(false);

  make();

  ll H, W, K;
  cin >> H >> W >> K;

  ll ans = 0;

  ans += perm(H + W, K) * K % mod;

  for (int t = 1; t <= K; t++) {
    ans += (H + W) * perm(H + W - 1, K - 1) % mod * (K + 1 - t) % mod;
    ans %= mod;
  }

  for (int t = 2; t <= K; t++) {
    ans += (H * W * 2) % mod * (t - 1) % mod * perm(H + W - 2, K - 2) % mod *
           (K + 1 - t) % mod;
    ans %= mod;
  }

  cout << ans << endl;
}
Problem p03154 (C++)

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

using namespace std;

#define N 2200000
#define LL long long

const LL mod = 1000000007;
LL n, m, k, p, ans, fac[N], ifac[N];

LL inv(LL x) { return x == 1 ? 1 : (mod - mod / x) * inv(mod % x) % mod; }

int main() {
  scanf("%lld%lld%lld", &n, &m, &k);
  p = n + m;
  fac[0] = 1;
  for (LL i = 1; i <= p; ++i)
    fac[i] = fac[i - 1] * i % mod;
  ifac[p] = inv(fac[p]);
  for (LL i = p - 1; ~i; --i)
    ifac[i] = ifac[i + 1] * (i + 1) % mod;
  ans = k * fac[p] % mod * ifac[p - k] % mod;
  for (LL i = 1; i <= k; ++i) {
    ans = (ans + fac[p] * ifac[p - k] % mod * (k - i + 1)) % mod;
    ans = (ans + 2 * n * m % mod * (i - 1) % mod * (k - i + 1) % mod *
                     fac[p - 2] % mod * ifac[p - k]) %
          mod;
  }
  printf("%lld\n", ans);

  return 0;
}


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

using namespace std;

#define N 22000000
#define LL long long

const LL mod = 1000000007;
LL n, m, k, p, ans, fac[N], ifac[N];

LL inv(LL x) { return x == 1 ? 1 : (mod - mod / x) * inv(mod % x) % mod; }

int main() {
  scanf("%lld%lld%lld", &n, &m, &k);
  p = n + m;
  fac[0] = 1;
  for (LL i = 1; i <= p; ++i)
    fac[i] = fac[i - 1] * i % mod;
  ifac[p] = inv(fac[p]);
  for (LL i = p - 1; ~i; --i)
    ifac[i] = ifac[i + 1] * (i + 1) % mod;
  ans = k * fac[p] % mod * ifac[p - k] % mod;
  for (LL i = 1; i <= k; ++i) {
    ans = (ans + fac[p] * ifac[p - k] % mod * (k - i + 1)) % mod;
    ans = (ans + 2 * n * m % mod * (i - 1) % mod * (k - i + 1) % mod *
                     fac[p - 2] % mod * ifac[p - k]) %
          mod;
  }
  printf("%lld\n", ans);

  return 0;
}
Problem p03154 (C++)

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

using namespace std;
typedef long long ll;
const int M = 1000000007;
int f[maxn], nf[maxn], n, m, K;
long long ans;

int C(int x, int y) {
  if (y < 0 || y > x)
    return 0;
  return 1ll * f[x] * nf[y] % M * nf[x - y] % M;
}

int main() {
  f[0] = 1;
  for (int i = 1; i < maxn; i++)
    f[i] = 1ll * f[i - 1] * i % M;
  nf[0] = nf[1] = 1;
  for (int i = 2; i < maxn; i++)
    nf[i] = M - 1ll * (M / i) * nf[M % i] % M;
  for (int i = 2; i < maxn; i++)
    nf[i] = 1ll * nf[i - 1] * nf[i] % M;
  cin >> n >> m >> K;
  for (int k = 1; k <= K; k++) {
    long long res = (1ll * n * m % M * C(n + m - 2, k - 2) +
                     1ll * (n + m) * C(n + m - 1, k - 1) + C(n + m, k)) %
                    M;
    (ans += res * f[n + m - k] % M * f[k] % M * nf[n + m - K]) %= M;
  }
  cout << ans << endl;
}


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

using namespace std;
typedef long long ll;
const int M = 1000000007;
int f[maxn], nf[maxn], n, m, K;
long long ans;

int C(int x, int y) {
  if (y < 0 || y > x)
    return 0;
  return 1ll * f[x] * nf[y] % M * nf[x - y] % M;
}

int main() {
  f[0] = 1;
  for (int i = 1; i < maxn; i++)
    f[i] = 1ll * f[i - 1] * i % M;
  nf[0] = nf[1] = 1;
  for (int i = 2; i < maxn; i++)
    nf[i] = M - 1ll * (M / i) * nf[M % i] % M;
  for (int i = 2; i < maxn; i++)
    nf[i] = 1ll * nf[i - 1] * nf[i] % M;
  cin >> n >> m >> K;
  for (int k = 1; k <= K; k++) {
    long long res = (1ll * n * m % M * C(n + m - 2, k - 2) +
                     1ll * (n + m) * C(n + m - 1, k - 1) + C(n + m, k)) %
                    M;
    (ans += res * f[n + m - k] % M * f[k] % M * nf[n + m - K]) %= M;
  }
  cout << ans << endl;
}
Problem p03155 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update

using namespace std;
using namespace __gnu_pbds;
typedef tree<pair<int, int>,                  // change type
             null_type, less<pair<int, int>>, // change type
             rb_tree_tag, tree_order_statistics_node_update>
    ordered_set;

typedef long long ll;
#define rep(i, start, end) for (int i = start; i < end; ++i)
#define per(i, start, end) for (int i = (int)start - 1; i >= end; --i)
#define sz(x) (int)(x).size()
#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define clr(d, v) memset(d, v, sizeof(d))
#define pii pair<int, int>
const double PI = 3.14159265358979323846;
const double eps = (1e-8);

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
//    freopen("facebook.txt", "w", stdout);
#endif
  int n, w, h;
  cin >> n >> w >> h;
  cout << (n - w + 1) * (n - h + 1) << '\n';
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update

using namespace std;
using namespace __gnu_pbds;
typedef tree<pair<int, int>,                  // change type
             null_type, less<pair<int, int>>, // change type
             rb_tree_tag, tree_order_statistics_node_update>
    ordered_set;

typedef long long ll;
#define rep(i, start, end) for (int i = start; i < end; ++i)
#define per(i, start, end) for (int i = (int)start - 1; i >= end; --i)
#define sz(x) (int)(x).size()
#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define clr(d, v) memset(d, v, sizeof(d))
#define pii pair<int, int>
const double PI = 3.14159265358979323846;
const double eps = (1e-8);

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  // #ifndef ONLINE_JUDGE
  //     freopen("input.txt", "r", stdin);
  ////    freopen("facebook.txt", "w", stdout);
  // #endif
  int n, w, h;
  cin >> n >> w >> h;
  cout << (n - w + 1) * (n - h + 1) << '\n';
  return 0;
}
Problem p03155 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define f(i, x, n) for (int i = x; i < (int)(n); ++i)
#define speed                                                                  \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);
int const N = 1e5 + 1;

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
#endif
  speed int n, h, w;
  cin >> n >> h >> w;
  int ans = (n - h + 1) * (n - w + 1);
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define f(i, x, n) for (int i = x; i < (int)(n); ++i)
#define speed                                                                  \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);
int const N = 1e5 + 1;

int main() {
  speed int n, h, w;
  cin >> n >> h >> w;
  int ans = (n - h + 1) * (n - w + 1);
  cout << ans << endl;
}
Problem p03155 (Python)

Buggy submission (status: Runtime Error):
N, W, H = map(int, input().split())

print((N - W + 1) * (N - H + 1))


Fixed submission:
N = int(input())
W = int(input())
H = int(input())

print((N - W + 1) * (N - H + 1))
Problem p03156 (C++)

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

using namespace std;

int main() {
  int N, p[21], A, B, i, x = 0, y = 0, z = 0, min;
  cin >> N >> A >> B;
  for (i = 1; i <= N; i++) {
    cin >> p[i];
    if (p[i] <= A)
      x++;
    if (p[i] >= A + 1 && p[i] <= B)
      y++;
    if (p[i] >= B + 1)
      z++;
  }

  if (x <= y && x <= z)
    min = x;
  if (y <= x && y <= z)
    min = y;
  if (z <= x && z <= y)
    min = z;
  cout << min;
  return 0;
}


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

using namespace std;

int main() {
  int N, p[101], A, B, i, x = 0, y = 0, z = 0, min;
  cin >> N >> A >> B;
  for (i = 1; i <= N; i++) {
    cin >> p[i];
    if (p[i] <= A)
      x++;
    if (p[i] >= A + 1 && p[i] <= B)
      y++;
    if (p[i] >= B + 1)
      z++;
  }

  if (x <= y && x <= z)
    min = x;
  if (y <= x && y <= z)
    min = y;
  if (z <= x && z <= y)
    min = z;
  cout << min;
  return 0;
}
Problem p03156 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
#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--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define REVERSE(v, n) reverse(v, v + n);
#define VREVERSE(v) reverse(v.begin(), v.end());
#define ll long long
#define pb(a) push_back(a)
#define INF 999999999

using namespace std;

int dy[4] = {0, 0, 1, -1};
int dx[4] = {1, -1, 0, 0};
int dxx[8] = {0, 0, 1, 1, 1, -1, -1, -1};
int dyy[8] = {1, -1, 0, 1, -1, 0, 1, -1};

ll gcd(ll x, ll y) {
  ll m = max(x, y), n = min(x, y);
  if (m % n == 0)
    return n;
  else
    return gcd(m % n, n);
}
ll X[301][4];
int main() {
  int N, A, B;
  int P[20];
  cin >> N >> A >> B;
  REP(i, N) cin >> P[i];
  int a = 0, b = 0, c = 0;
  REP(i, N) {
    if (P[i] <= A)
      a++;
    else if (A < P[i] && P[i] <= B)
      b++;
    else
      c++;
  }
  int ans = min(min(a, b), c);
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
#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--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define REVERSE(v, n) reverse(v, v + n);
#define VREVERSE(v) reverse(v.begin(), v.end());
#define ll long long
#define pb(a) push_back(a)
#define INF 999999999

using namespace std;

int dy[4] = {0, 0, 1, -1};
int dx[4] = {1, -1, 0, 0};
int dxx[8] = {0, 0, 1, 1, 1, -1, -1, -1};
int dyy[8] = {1, -1, 0, 1, -1, 0, 1, -1};

ll gcd(ll x, ll y) {
  ll m = max(x, y), n = min(x, y);
  if (m % n == 0)
    return n;
  else
    return gcd(m % n, n);
}
ll X[301][4];
int main() {
  int N, A, B;
  int P[101];
  cin >> N >> A >> B;
  REP(i, N) cin >> P[i];
  int a = 0, b = 0, c = 0;
  REP(i, N) {
    if (P[i] <= A)
      a++;
    else if (A < P[i] && P[i] <= B)
      b++;
    else
      c++;
  }
  int ans = min(min(a, b), c);
  cout << ans << endl;
}