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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
typedef long long ll;
const int INF = 1e9;
const int MOD = 1e9 + 7;
using namespace std;

// ループマクロ
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; 0 <= i; i--)
#define ROP(i, n) for (int i = 1; i <= n; i++)

// 入出力
#define SCOUT(x) cout << (x) << " "
#define VECCIN(x)                                                              \
  for (auto &ele : (x))                                                        \
  cin >> ele
#define VECOUT(x)                                                              \
  for (auto &ele : (x))                                                        \
    cout << ele << " ";                                                        \
  cout << endl;

// その他
#define ALL(obj) (obj).begin(), (obj).end()
#define V vector<int>
#define VLL vector<long long>
#define S set<int>
#define PQ priority_queue<int>
#define PQG priority_queue<int, V, greater<int>>
ll func(int a) {
  if (a == 1)
    return 1;
  return a + func(a - 1);
}
int main(void) {

  ios::sync_with_stdio(false);
  cin.tie(0);
  int N, K;
  cin >> N >> K;
  V A(N);
  VECCIN(A);
  ll ans = 0;

  ll plus1 = func(K), plus2 = func(K - 1);
  REP(i, N) {
    REP(j, N) {
      if (i == j)
        continue;

      if (A[i] > A[j])
        ans += (i < j) ? plus1 : plus2;
      ans %= MOD;
    }
  }

  cout << ans % MOD << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
typedef long long ll;
const int INF = 1e9;
const int MOD = 1e9 + 7;
using namespace std;

// ループマクロ
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; 0 <= i; i--)
#define ROP(i, n) for (int i = 1; i <= n; i++)

// 入出力
#define SCOUT(x) cout << (x) << " "
#define VECCIN(x)                                                              \
  for (auto &ele : (x))                                                        \
  cin >> ele
#define VECOUT(x)                                                              \
  for (auto &ele : (x))                                                        \
    cout << ele << " ";                                                        \
  cout << endl;

// その他
#define ALL(obj) (obj).begin(), (obj).end()
#define V vector<int>
#define VLL vector<long long>
#define S set<int>
#define PQ priority_queue<int>
#define PQG priority_queue<int, V, greater<int>>
ll func(int a) {
  if (a == 1)
    return 1;
  return a + func(a - 1);
}
int main(void) {

  ios::sync_with_stdio(false);
  cin.tie(0);
  int N, K;
  cin >> N >> K;
  V A(N);
  VECCIN(A);
  ll ans = 0;

  ll plus1 = func(K), plus2 = plus1 - K;
  REP(i, N) {
    REP(j, N) {
      if (i == j)
        continue;

      if (A[i] > A[j])
        ans += (i < j) ? plus1 : plus2;
      ans %= MOD;
    }
  }

  cout << ans % MOD << endl;

  return 0;
}
Problem p02928 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <set>
#include <vector>
using namespace std;
typedef long long ll;

const ll mod = 1e9 + 7;

ll modpow(ll a, ll b, ll p = 1e9 + 7) {
  if (b == 0)
    return 1;

  if (b % 2 == 0) {
    ll d = modpow(a, b / 2, p);
    return (d * d) % p;
  } else {
    return (a % p * modpow(a, b - 1, p)) % p;
  }
}

int main() {
  int n;
  ll k;
  cin >> n >> k;
  k %= mod;
  if (k == 0)
    k = mod;
  vector<ll> v(n);
  for (int i = 0; i < n; i++)
    cin >> v[i];
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    ll cnt = 0;
    for (int j = i + 1; j < n; j++) {
      if (v[i] > v[j])
        cnt++;
    }
    ll tmp = 0;
    for (int j = 0; j < n; j++) {
      if (v[i] > v[j])
        tmp++;
    }
    ll a = 2 * cnt + (k - 1) * tmp;
    a %= mod;
    a = a * modpow(2, -1) % mod * k % mod;
    ans += a;
    ans %= mod;
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <iostream>
#include <set>
#include <vector>
using namespace std;
typedef long long ll;

const ll mod = 1e9 + 7;

ll modpow(ll a, ll b, ll p = 1e9 + 7) {
  if (b == 0)
    return 1;

  if (b % 2 == 0) {
    ll d = modpow(a, b / 2, p);
    return (d * d) % p;
  } else {
    return (a % p * modpow(a, b - 1, p)) % p;
  }
}

int main() {
  int n;
  ll k;
  cin >> n >> k;
  k %= mod;
  if (k == 0)
    k = mod;
  vector<ll> v(n);
  for (int i = 0; i < n; i++)
    cin >> v[i];
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    ll cnt = 0;
    for (int j = i + 1; j < n; j++) {
      if (v[i] > v[j])
        cnt++;
    }
    ll tmp = 0;
    for (int j = 0; j < n; j++) {
      if (v[i] > v[j])
        tmp++;
    }
    ll a = 2 * cnt + (k - 1) * tmp;
    a %= mod;
    a = a * modpow(2, mod - 2) % mod * k % mod;
    ans += a;
    ans %= mod;
  }
  cout << ans << endl;
  return 0;
}
Problem p02928 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll NUM = pow(10, 9) + 7;

int main() {
  ll N, K;
  cin >> N >> K;
  vector<int> A(N);
  for (int i(0); i < N; ++i) {
    cin >> A[i];
  }
  vector<int> left(N, 0), right(N, 0);
  for (int i(0); i < N; ++i) {
    if (i >= 1) {
      for (int j(0); j < i; ++j) {
        if (A[j] < A[i])
          left[i]++;
      }
    }
    if (i <= N - 2) {
      for (int j(i + 1); j < N; ++j) {
        if (A[j] < A[i])
          right[i]++;
      }
    }
  }
  ll ans(0);
  ll sum1(0), sum2(0);
  for (int i(1); i <= K; ++i) {
    sum2 += i;
    sum2 %= NUM;
  }
  sum2 %= NUM;
  sum1 = sum2 - K;
  sum1 %= NUM;
  for (int i(0); i < N; ++i) {
    ans += ((sum1 * left[i]) % NUM + (sum2 * right[i]) % NUM) % NUM;
    ans %= NUM;
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll NUM = pow(10, 9) + 7;

int main() {
  ll N, K;
  cin >> N >> K;
  vector<int> A(N);
  for (int i(0); i < N; ++i) {
    cin >> A[i];
  }
  vector<int> left(N, 0), right(N, 0);
  for (int i(0); i < N; ++i) {
    if (i >= 1) {
      for (int j(0); j < i; ++j) {
        if (A[j] < A[i])
          left[i]++;
      }
    }
    if (i <= N - 2) {
      for (int j(i + 1); j < N; ++j) {
        if (A[j] < A[i])
          right[i]++;
      }
    }
  }
  ll ans(0);
  ll sum1, sum2;
  sum1 = (K * (K - 1) / 2) % NUM;
  sum2 = (sum1 + K) % NUM;
  for (int i(0); i < N; ++i) {
    ans += ((sum1 * left[i]) % NUM + (sum2 * right[i]) % NUM) % NUM;
    ans %= NUM;
  }
  cout << ans << endl;
  return 0;
}
Problem p02928 (C++)

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

using namespace std;

const long long mod = 1e9 + 7;
long long n, k;
long long c[N];

long long a[N], b[N];
long long num[N];
long long ans;

long long calc(int x) {
  long long res = 0;
  for (int i = n; i > x; --i)
    if (a[i] < a[x])
      ++res;
  return res;
}

int main() {
  freopen("in", "r", stdin);
  freopen("out", "w", stdout);
  ios::sync_with_stdio(false);

  cin >> n >> k;
  for (int i = 1; i <= n; ++i)
    cin >> a[i], b[i] = a[i];

  sort(b + 1, b + n + 1);
  for (int i = 1; i <= n; ++i)
    num[i] = lower_bound(b + 1, b + n + 1, a[i]) - b - 1;

  for (int i = n; i; --i) {
    (ans += calc(i) * k % mod) %= mod;
    (ans += k * (k - 1) / 2 % mod * num[i] % mod) %= mod;
  }

  cout << ans << endl;
  return 0;
}

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

using namespace std;

const long long mod = 1e9 + 7;
long long n, k;
long long c[N];

long long a[N], b[N];
long long num[N];
long long ans;

long long calc(int x) {
  long long res = 0;
  for (int i = n; i > x; --i)
    if (a[i] < a[x])
      ++res;
  return res;
}

int main() {
  ios::sync_with_stdio(false);

  cin >> n >> k;
  for (int i = 1; i <= n; ++i)
    cin >> a[i], b[i] = a[i];

  sort(b + 1, b + n + 1);
  for (int i = 1; i <= n; ++i)
    num[i] = lower_bound(b + 1, b + n + 1, a[i]) - b - 1;

  for (int i = n; i; --i) {
    (ans += calc(i) * k % mod) %= mod;
    (ans += k * (k - 1) / 2 % mod * num[i] % mod) %= mod;
  }

  cout << ans << endl;
  return 0;
}
Problem p02928 (Python)

Buggy submission (status: Runtime Error):
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
cnt = 0
cnt2 = 0
for i, ai in enumerate(a):
    for j in range(i, n):
        if ai > a[j]:
            cnt += 1
    if i > 0:
        for j in range(i):
            if ai > a[j]:
                cnt2 += 1

if k % 2 == 0:
    mul = (1 + k) * (k // 2)
else:
    mul = (1 + k) * (k // 2) + (k // 2 + 1)
if k > 1:
    k -= 1
    if k % 2 == 0:
        mul2 = (1 + k) * (k // 2)
    else:
        mul2 = (1 + k) * (k // 2) + (k // 2 + 1)
print(((cnt * mul) + (cnt2 * mul2)) % 1000000007)


Fixed submission:
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
cnt = 0
cnt2 = 0
for i, ai in enumerate(a):
    for j in range(i, n):
        if ai > a[j]:
            cnt += 1
    if i > 0:
        for j in range(i):
            if ai > a[j]:
                cnt2 += 1

if k % 2 == 0:
    mul = (1 + k) * (k // 2)
else:
    mul = (1 + k) * (k // 2) + (k // 2 + 1)
mul2 = 0
if k > 1:
    k -= 1
    if k % 2 == 0:
        mul2 = (1 + k) * (k // 2)
    else:
        mul2 = (1 + k) * (k // 2) + (k // 2 + 1)
print(((cnt * mul) + (cnt2 * mul2)) % 1000000007)
Problem p02928 (C++)

Buggy submission (status: Runtime Error):
/*------------------------------------
........Bismillahir Rahmanir Rahim....
..........created by Abdul Aziz.......
------------------------------------*/
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <unordered_map>
#include <vector>
#define mod 998244353
#define int long long
#define ld long double
#define pb push_back
#define vi vector<int>
#define dbg(x) cerr << #x << " = " << x << '\n'
#define sz(x) (int)x.size()
#define all(a) (a.begin(), a.end())
#define ff first
#define ss second
#define pii pair<int, int>
#define lcm(a, b) (a * b) / __gcd(a, b)
using namespace std;

int fun(int n) { return ((n * (n - 1)) / 2) % 1000000007; }

inline void solve() {
  map<int, int> oc;
  int n, k, ans = 0;
  cin >> n >> k;
  vi a(n), b(n), help(n, 0);
  for (int &i : a)
    cin >> i;
  for (int i = 0; i < n; i++) {
    int b = 0, f = 0;
    for (int j = i + 1; j < n; j++)
      if (a[i] > a[j])
        b++;
    for (int j = 0; j < i; j++)
      if (a[i] > a[j])
        f++;
    ans = (ans + ((f + b) * fun(k)) + (b * k)) % 1000000007;
  }
  cout << ans << endl;
  return;
}

signed main() {
  int n = 1;
  cin >> n;
  while (n--)
    solve();
  return 0;
}


Fixed submission:
/*------------------------------------
........Bismillahir Rahmanir Rahim....
..........created by Abdul Aziz.......
------------------------------------*/
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <unordered_map>
#include <vector>
#define mod 998244353
#define int long long
#define ld long double
#define pb push_back
#define vi vector<int>
#define dbg(x) cerr << #x << " = " << x << '\n'
#define sz(x) (int)x.size()
#define all(a) (a.begin(), a.end())
#define ff first
#define ss second
#define pii pair<int, int>
#define lcm(a, b) (a * b) / __gcd(a, b)
using namespace std;

int fun(int n) { return ((n * (n - 1)) / 2) % 1000000007; }

inline void solve() {
  map<int, int> oc;
  int n, k, ans = 0;
  cin >> n >> k;
  vi a(n), b(n), help(n, 0);
  for (int &i : a)
    cin >> i;
  for (int i = 0; i < n; i++) {
    int b = 0, f = 0;
    for (int j = i + 1; j < n; j++)
      if (a[i] > a[j])
        b++;
    for (int j = 0; j < i; j++)
      if (a[i] > a[j])
        f++;
    ans = (ans + ((f + b) * fun(k)) + (b * k)) % 1000000007;
  }
  cout << ans << endl;
  return;
}

signed main() {
  int n = 1; // cin>>n;
  while (n--)
    solve();
  return 0;
}
Problem p02928 (C++)

Buggy submission (status: Runtime Error):
// atcoder 20190824 B
#include <bits/stdc++.h>
#define N 2005

using namespace std;

const long long mod = 1e9 + 7;
long long n, k;
long long c[N];

long long a[N], b[N];
long long num[N];
long long ans;

long long calc(int x) {
  long long res = 0;
  for (int i = n; i > x; --i)
    if (a[i] < a[x])
      ++res;
  return res;
}

int main() {
  freopen("in", "r", stdin);
  freopen("out", "w", stdout);
  ios::sync_with_stdio(false);

  cin >> n >> k;
  for (int i = 1; i <= n; ++i)
    cin >> a[i], b[i] = a[i];

  sort(b + 1, b + n + 1);
  for (int i = 1; i <= n; ++i)
    num[i] = lower_bound(b + 1, b + n + 1, a[i]) - b - 1;

  for (int i = n; i; --i) {
    (ans += calc(i) * k % mod) %= mod;
    (ans += k * (k - 1) / 2 % mod * num[i] % mod) %= mod;
  }

  cout << ans << endl;
  return 0;
}


Fixed submission:
// atcoder 20190824 B
#include <bits/stdc++.h>
#define N 2005

using namespace std;

const long long mod = 1e9 + 7;
long long n, k;
long long c[N];

long long a[N], b[N];
long long num[N];
long long ans;

long long calc(int x) {
  long long res = 0;
  for (int i = n; i > x; --i)
    if (a[i] < a[x])
      ++res;
  return res;
}

int main() {

  cin >> n >> k;
  for (int i = 1; i <= n; ++i)
    cin >> a[i], b[i] = a[i];

  sort(b + 1, b + n + 1);
  for (int i = 1; i <= n; ++i)
    num[i] = lower_bound(b + 1, b + n + 1, a[i]) - b - 1;

  for (int i = n; i; --i) {
    (ans += calc(i) * k % mod) %= mod;
    (ans += k * (k - 1) / 2 % mod * num[i] % mod) %= mod;
  }

  cout << ans << endl;
  return 0;
}
Problem p02928 (C++)

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

int read() {
  int x = 0;
  bool f = 0;
  char c = getchar();
  while (c < '0' || c > '9')
    f |= (c == '-'), c = getchar();
  while (c >= '0' && c <= '9')
    x = x * 10 + (c ^ 48), c = getchar();
  return f ? -x : x;
}

#define MAXN 1000
#define MOD 1000000007
int N, K;
int A[MAXN + 5];

#define MAXA 2000
#define lowbit(x) (x & -x)
int Tree[MAXA + 5];
void Update(int i, int d) {
  while (i <= MAXA)
    Tree[i] += d, i += lowbit(i);
}
int GetSum(int i) {
  int ret = 0;
  while (i > 0)
    ret += Tree[i], i -= lowbit(i);
  return ret;
}

int main() {
  N = read(), K = read();
  for (int i = 1; i <= N; i++)
    A[i] = read();
  int Sum = 0;
  for (int i = 1; i <= N; i++)
    Sum += i - 1 - GetSum(A[i]), Update(A[i], 1);
  Sum = (long long)Sum * K % MOD;
  for (int i = 1; i <= N; i++) {
    long long tmp = GetSum(A[i] - 1);
    Sum = (Sum + (tmp * ((long long)K * (K - 1) / 2 % MOD) % MOD)) % MOD;
  }
  printf("%d", Sum);
}


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

int read() {
  int x = 0;
  bool f = 0;
  char c = getchar();
  while (c < '0' || c > '9')
    f |= (c == '-'), c = getchar();
  while (c >= '0' && c <= '9')
    x = x * 10 + (c ^ 48), c = getchar();
  return f ? -x : x;
}

#define MAXN 20000
#define MOD 1000000007
int N, K;
int A[MAXN + 5];

#define MAXA 2000
#define lowbit(x) (x & -x)
int Tree[MAXA + 5];
void Update(int i, int d) {
  while (i <= MAXA)
    Tree[i] += d, i += lowbit(i);
}
int GetSum(int i) {
  int ret = 0;
  while (i > 0)
    ret += Tree[i], i -= lowbit(i);
  return ret;
}

int main() {
  N = read(), K = read();
  for (int i = 1; i <= N; i++)
    A[i] = read();
  int Sum = 0;
  for (int i = 1; i <= N; i++)
    Sum += i - 1 - GetSum(A[i]), Update(A[i], 1);
  Sum = (long long)Sum * K % MOD;
  for (int i = 1; i <= N; i++) {
    long long tmp = GetSum(A[i] - 1);
    Sum = (Sum + (tmp * ((long long)K * (K - 1) / 2 % MOD) % MOD)) % MOD;
  }
  printf("%d", Sum);
}
Problem p02928 (C++)

Buggy submission (status: Time Limit Exceeded):
#define _USE_MATH_DEFINES
#include <algorithm>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <stdio.h>
#include <vector>

using namespace std;
typedef long long ll;

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

int main() {
  ll mod = 1000000007;
  ll N, K;
  cin >> N >> K;
  ll A[N];
  rep(i, N) cin >> A[i];
  ll S = 0;
  rep(i, N) {
    for (ll j = i + 1; j < N; j++) {
      if (A[i] > A[j])
        S++;
    }
  }
  ll out = (S * K) % mod;
  ll T = 0;
  rep(i, N) {
    rep(j, N) {
      if (A[i] > A[j])
        T++;
    }
  }
  rep(i, K) { out = (out + i * T) % mod; }
  cout << out << endl;
  return 0;
}


Fixed submission:
#define _USE_MATH_DEFINES
#include <algorithm>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <stdio.h>
#include <vector>

using namespace std;
typedef long long ll;

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

int main() {
  ll mod = 1000000007;
  ll N, K;
  cin >> N >> K;
  ll A[N];
  rep(i, N) cin >> A[i];
  ll S = 0;
  rep(i, N) {
    for (ll j = i + 1; j < N; j++) {
      if (A[i] > A[j])
        S++;
    }
  }
  ll out = (S * K) % mod;
  ll T = 0;
  rep(i, N) {
    rep(j, N) {
      if (A[i] > A[j])
        T++;
    }
  }
  out = (out + ((K * (K - 1)) / 2) % mod * T) % mod;
  cout << out << endl;
  return 0;
}
Problem p02928 (C++)

Buggy submission (status: Runtime Error):
#define _CRT_SECURE_NO_WARNINGS
using namespace std;
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

int arr[100000];
long long MOD = 1000000007;

int main() {
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  long long N, K;
  cin >> N >> K;
  for (int i = 0; i < N; i++) {
    cin >> arr[i];
  }
  long long ans = 0;
  for (int i = 0; i < N; i++) {
    long long less_inv = 0, gre_inv = 0;
    for (int j = 0; j < N; j++) {
      if (j < i && arr[i] > arr[j])
        less_inv++;
      if (j > i && arr[i] > arr[j])
        gre_inv++;
    }
    ans += (less_inv * ((K * (K - 1) / 2) % MOD)) % MOD;
    ans %= MOD;
    ans += (gre_inv * ((K * (K + 1) / 2) % MOD)) % MOD;
    ans %= MOD;
  }
  cout << ans;
}

Fixed submission:
#define _CRT_SECURE_NO_WARNINGS
using namespace std;
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

int arr[100000];
long long MOD = 1000000007;

int main() {
  long long N, K;
  cin >> N >> K;
  for (int i = 0; i < N; i++) {
    cin >> arr[i];
  }
  long long ans = 0;
  for (int i = 0; i < N; i++) {
    long long less_inv = 0, gre_inv = 0;
    for (int j = 0; j < N; j++) {
      if (j < i && arr[i] > arr[j])
        less_inv++;
      if (j > i && arr[i] > arr[j])
        gre_inv++;
    }
    ans += (less_inv * ((K * (K - 1) / 2) % MOD)) % MOD;
    ans %= MOD;
    ans += (gre_inv * ((K * (K + 1) / 2) % MOD)) % MOD;
    ans %= MOD;
  }
  cout << ans;
}
Problem p02928 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long ll;
const int MAXN = 2000;
const ll mod = 1e9 + 7;
int a[MAXN + 5], cnt1[MAXN + 5], cnt2[MAXN + 5];
ll n, k, ans;
int main() {
  scanf("%lld%lld", &n, &k);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) {
      if (a[j] < a[i] && j < i)
        cnt1[i]++;
      if (a[j] < a[i] && j > i)
        cnt2[i]++;
    }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= k; j++) {
      ans = (ans + cnt2[i] * (k - j + 1) % mod) % mod + cnt1[i] * (k - j) % mod;
      ans %= mod;
    }
  }
  printf("%lld\n", ans % mod);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long ll;
const int MAXN = 2000;
const ll mod = 1e9 + 7;
int a[MAXN + 5], cnt1[MAXN + 5], cnt2[MAXN + 5];
ll n, k, ans;
int main() {
  scanf("%lld%lld", &n, &k);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) {
      if (a[j] < a[i] && j < i)
        cnt1[i]++;
      if (a[j] < a[i] && j > i)
        cnt2[i]++;
    }
  for (int i = 1; i <= n; i++) {
    ans = (ans + cnt2[i] * (k * (k + 1) / 2 % mod)) % mod +
          cnt1[i] * ((k * (k - 1) / 2) % mod) % mod;
    ans %= mod;
  }
  printf("%lld\n", ans % mod);
  return 0;
}
Problem p02928 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#define ll long long
using namespace std;
#define mod 1000000007
int n, m, ans, a[1005], a1[1005], a2[1005];
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  for (int i = 1; i <= n; i++) {
    for (int j = i + 1; j <= n; j++)
      a1[i] += (a[j] < a[i]);
    for (int j = i - 1; j >= 1; j--)
      a2[i] += (a[j] < a[i]);
    ans = int((1ll * ans + 1ll * (m) * (m + 1) / 2 % mod * a1[i] % mod +
               1ll * (m) * (m - 1) / 2 % mod * a2[i] % mod) %
              mod);
  }
  printf("%d", ans);
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#define ll long long
using namespace std;
#define mod 1000000007
int n, m, ans, a[2005], a1[2005], a2[2005];
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  for (int i = 1; i <= n; i++) {
    for (int j = i + 1; j <= n; j++)
      a1[i] += (a[j] < a[i]);
    for (int j = i - 1; j >= 1; j--)
      a2[i] += (a[j] < a[i]);
    ans = int((1ll * ans + 1ll * (m) * (m + 1) / 2 % mod * a1[i] % mod +
               1ll * (m) * (m - 1) / 2 % mod * a2[i] % mod) %
              mod);
  }
  printf("%d", ans);
}
Problem p02928 (C++)

Buggy submission (status: Runtime Error):
// atcoder 20190824 B
#include <bits/stdc++.h>
#define N 2005

using namespace std;

const long long mod = 1e9 + 7;
long long n, k;
long long c[N];

long long a[N], b[N];
long long num[N];
long long ans;

long long calc(int x) {
  long long res = 0;
  for (int i = n; i > x; --i)
    if (a[i] < a[x])
      ++res;
  return res;
}

int main() {
  freopen("in", "r", stdin);
  freopen("out", "w", stdout);
  ios::sync_with_stdio(false);

  cin >> n >> k;
  for (int i = 1; i <= n; ++i)
    cin >> a[i], b[i] = a[i];

  sort(b + 1, b + n + 1);
  for (int i = 1; i <= n; ++i)
    num[i] = lower_bound(b + 1, b + n + 1, a[i]) - b - 1;

  for (int i = n; i; --i) {
    (ans += calc(i) * k % mod) %= mod;
    (ans += k * (k - 1) / 2 % mod * num[i] % mod) %= mod;
  }

  cout << ans << endl;
  return 0;
}

Fixed submission:
// atcoder 20190824 B
#include <bits/stdc++.h>
#define N 2005

using namespace std;

const long long mod = 1e9 + 7;
long long n, k;
long long c[N];

long long a[N], b[N];
long long num[N];
long long ans;

long long calc(int x) {
  long long res = 0;
  for (int i = n; i > x; --i)
    if (a[i] < a[x])
      ++res;
  return res;
}

int main() {
  ios::sync_with_stdio(false);

  cin >> n >> k;
  for (int i = 1; i <= n; ++i)
    cin >> a[i], b[i] = a[i];

  sort(b + 1, b + n + 1);
  for (int i = 1; i <= n; ++i)
    num[i] = lower_bound(b + 1, b + n + 1, a[i]) - b - 1;

  for (int i = n; i; --i) {
    (ans += calc(i) * k % mod) %= mod;
    (ans += k * (k - 1) / 2 % mod * num[i] % mod) %= mod;
  }

  cout << ans << endl;
  return 0;
}
Problem p02928 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, k) for (i = 0; i < k; i++)
#define P pair<ll, ll>
ll mod = 1000000007;
P p[200005];

ll i, b, c, k, j, n, m, z = 0, mi = 10;
ll a[2005] = {};
vector<ll> v[30];
string s;
char ca[10000];
priority_queue<ll> q;
int main() {
  cin >> n >> k;
  rep(i, n) cin >> a[i];

  rep(i, n) rep(j, n) {
    if (a[i] > a[j]) {
      if (i < j)
        b++;
      if (i > j)
        c++;
    }
  }
  m = b + c;
  z = z + (b * k) % mod;
  ll bb = 0;
  rep(i, k) bb = (bb + i) % mod;
  z = (z + m * bb) % mod;
  cout << z;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, k) for (i = 0; i < k; i++)
#define P pair<ll, ll>
ll mod = 1000000007;
P p[200005];

ll i, b, c, k, j, n, m, z = 0, mi = 10;
ll a[2005] = {};
vector<ll> v[30];
string s;
char ca[10000];
priority_queue<ll> q;
int main() {
  cin >> n >> k;
  rep(i, n) cin >> a[i];

  rep(i, n) rep(j, n) {
    if (a[i] > a[j]) {
      if (i < j)
        b++;
      if (i > j)
        c++;
    }
  }
  m = b + c;
  z = z + (b * k) % mod;
  ll bb = 0;
  bb = ((k - 1) * k / 2) % mod;
  z = (z + m * bb) % mod;
  cout << z;

  return 0;
}
Problem p02928 (C++)

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

template <typename T> class BIT {
  // bit[x]
  // manage range of bit[x] is (x & -x)
  //
public:
  static const int n = 10000;
  T bit[n] = {};
  void add(int a, T w) {
    for (int x = a; x <= n; x += x & -x) {
      bit[x] += w;
    }
  }
  T sum(int a) {
    T ret = 0;
    for (int x = a; x > 0; x -= x & -x) {
      ret += bit[x];
    }
    return ret;
  }
};

long long int wa(long long int a) { return (a * (a + 1)) / 2; }

int main() {
  long long int n, k;
  cin >> n >> k;
  int A[2001], B[2001];
  for (int i = 0; i < n; ++i) {
    cin >> B[i];
    A[i] = 2000 - B[i];
  }
  long long int tentou[2001];
  long long int migi[2001];
  long long int ti = wa(k) % 1000000007;
  long long int to = wa(k - 1) % 1000000007;
  BIT<long long int> BT;
  BIT<long long int> saisho;
  for (int i = 0; i < n; ++i) {
    tentou[i] = BT.sum(A[i] - 1);
    BT.add(A[i], 1);
  }
  for (int i = n - 1; i >= 0; --i) {
    migi[i] = saisho.sum(A[i] - 1);
    saisho.add(A[i], 1);
  }
  long long int ans = 0;
  for (int i = 0; i < n; ++i) {
    ans += ((ti * tentou[i]) % 1000000007 + (to * migi[i]) % 1000000007) %
           1000000007;
  }
  cout << ans % 1000000007 << endl;
}

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

template <typename T> class BIT {
  // bit[x]
  // manage range of bit[x] is (x & -x)
  //
public:
  static const int n = 10000;
  T bit[n] = {};
  void add(int a, T w) {
    for (int x = a; x <= n; x += x & -x) {
      bit[x] += w;
    }
  }
  T sum(int a) {
    T ret = 0;
    for (int x = a; x > 0; x -= x & -x) {
      ret += bit[x];
    }
    return ret;
  }
};

long long int wa(long long int a) { return (a * (a + 1)) / 2; }

int main() {
  long long int n, k;
  cin >> n >> k;
  int A[2001], B[2001];
  for (int i = 0; i < n; ++i) {
    cin >> B[i];
    A[i] = 2000 - B[i] + 1;
  }
  long long int tentou[2001];
  long long int migi[2001];
  long long int ti = wa(k) % 1000000007;
  long long int to = wa(k - 1) % 1000000007;
  BIT<long long int> BT;
  BIT<long long int> saisho;
  for (int i = 0; i < n; ++i) {
    tentou[i] = BT.sum(A[i] - 1);
    BT.add(A[i], 1);
  }
  for (int i = n - 1; i >= 0; --i) {
    migi[i] = saisho.sum(A[i] - 1);
    saisho.add(A[i], 1);
  }
  long long int ans = 0;
  for (int i = 0; i < n; ++i) {
    ans += ((ti * tentou[i]) % 1000000007 + (to * migi[i]) % 1000000007) %
           1000000007;
  }
  cout << ans % 1000000007 << endl;
}
Problem p02928 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define until(x) while (!(x))
#define unless(x) if (!(x))
#define all(x) (x).begin(), (x).end()
#define F first
#define S second
#define pc __builtin_popcount

const int mod = 1e9 + 7;
int ar[2005];
int before[2005];
int after[2005];
int n, k;

int mult(int n, int m) {
  if (n > m) {
    swap(n, m);
  }
  if (m == 1) {
    return n;
  }
  int k = mult(n, m / 2);
  int ans = k + k;
  ans %= mod;
  if (m & 1) {
    ans += n;
  }
  ans %= mod;
  return ans;
}

int32_t main() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> ar[i];
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j < i; j++) {
      before[i] += (ar[i] > ar[j]);
    }
    for (int j = i + 1; j <= n; j++) {
      after[i] += (ar[i] > ar[j]);
    }
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    ans += after[i] * k;
    ans %= mod;
    ans += mult(((k) * (k - 1) / 2), (before[i] + after[i]));
    ans %= mod;
    // cout<<ans<<"\n";
  }
  /*for(int i=1;i<=n;i++){
          cout<<before[i]<<" "<<after[i]<<"\n";
  }*/
  cout << ans;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define until(x) while (!(x))
#define unless(x) if (!(x))
#define all(x) (x).begin(), (x).end()
#define F first
#define S second
#define pc __builtin_popcount

const int mod = 1e9 + 7;
int ar[2005];
int before[2005];
int after[2005];
int n, k;

int mult(int n, int m) {
  if (n > m) {
    swap(n, m);
  }
  if (n == 0 || m == 0) {
    return 0;
  }
  if (m == 1) {
    return n;
  }
  int k = mult(n, m / 2);
  int ans = k + k;
  ans %= mod;
  if (m & 1) {
    ans += n;
  }
  ans %= mod;
  return ans;
}

int32_t main() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> ar[i];
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j < i; j++) {
      before[i] += (ar[i] > ar[j]);
    }
    for (int j = i + 1; j <= n; j++) {
      after[i] += (ar[i] > ar[j]);
    }
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    ans += after[i] * k;
    ans %= mod;
    ans += mult(((k) * (k - 1) / 2), (before[i] + after[i]));
    ans %= mod;
    // cout<<ans<<"\n";
  }
  /*for(int i=1;i<=n;i++){
          cout<<before[i]<<" "<<after[i]<<"\n";
  }*/
  cout << ans;
}
Problem p02928 (C++)

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

using ll_t = long long;
const int MAXN = 1000;
const int P = (int)1e9 + 7;

int a[MAXN];

ll_t solve(int n, int k) {
  int x = 0, y = 0;
  for (int i = 0; i < n; ++i) {
    for (int j = i + 1; j < n; ++j) {
      if (a[i] > a[j])
        ++x;
      else if (a[i] < a[j])
        ++y;
    }
  }
  ll_t res = (ll_t)k * (k + 1) / 2 % P * x + (ll_t)k * (k - 1) / 2 % P * y;
  return res % P;
}

int main() {
  int n, k;
  std::cin >> n >> k;
  for (int i = 0; i < n; ++i) {
    scanf("%d", a + i);
  }
  std::cout << solve(n, k) << std::endl;
}


Fixed submission:
#include <iostream>

using ll_t = long long;
const int MAXN = 2000;
const int P = (int)1e9 + 7;

int a[MAXN];

ll_t solve(int n, int k) {
  int x = 0, y = 0;
  for (int i = 0; i < n; ++i) {
    for (int j = i + 1; j < n; ++j) {
      if (a[i] > a[j])
        ++x;
      else if (a[i] < a[j])
        ++y;
    }
  }
  ll_t res = (ll_t)k * (k + 1) / 2 % P * x + (ll_t)k * (k - 1) / 2 % P * y;
  return res % P;
}

int main() {
  int n, k;
  std::cin >> n >> k;
  for (int i = 0; i < n; ++i) {
    scanf("%d", a + i);
  }
  std::cout << solve(n, k) << std::endl;
}
Problem p02929 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <string>
using namespace std;
#pragma warning(disable : 4996)

long long mod = 1000000007;
string S;
int N, A[100009];

int main() {
  cin >> N >> S;
  if (S[0] == 'W' || S[S.size() - 1] == 'W') {
    cout << "0" << endl;
    return 0;
  }
  for (int i = 0; i < S.size(); i++) {
    if (i == 0)
      A[i] = 1;
    else if (S[i - 1] != S[i]) {
      if (A[i - 1] == 1)
        A[i] = 1;
      else
        A[i] = 0;
    } else {
      if (A[i - 1] == 1)
        A[i] = 0;
      else
        A[i] = 1;
    }
  }
  long long dep = 0, ans = 1;
  for (int i = 0; i < S.size(); i++) {
    if (A[i] == 1)
      dep++;
    else {
      ans *= dep;
      ans %= mod;
      dep--;
    }
    if (dep < 0) {
      cout << "0" << endl;
      return 0;
    }
  }
  if (dep != 0) {
    cout << "0" << endl;
    return 0;
  }
  for (long long i = 1; i <= N; i++) {
    ans *= i;
    ans %= mod;
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <iostream>
#include <string>
using namespace std;
#pragma warning(disable : 4996)

long long mod = 1000000007;
string S;
int N, A[300009];

int main() {
  cin >> N >> S;
  if (S[0] == 'W' || S[S.size() - 1] == 'W') {
    cout << "0" << endl;
    return 0;
  }
  for (int i = 0; i < S.size(); i++) {
    if (i == 0)
      A[i] = 1;
    else if (S[i - 1] != S[i]) {
      if (A[i - 1] == 1)
        A[i] = 1;
      else
        A[i] = 0;
    } else {
      if (A[i - 1] == 1)
        A[i] = 0;
      else
        A[i] = 1;
    }
  }
  long long dep = 0, ans = 1;
  for (int i = 0; i < S.size(); i++) {
    if (A[i] == 1)
      dep++;
    else {
      ans *= dep;
      ans %= mod;
      dep--;
    }
    if (dep < 0) {
      cout << "0" << endl;
      return 0;
    }
  }
  if (dep != 0) {
    cout << "0" << endl;
    return 0;
  }
  for (long long i = 1; i <= N; i++) {
    ans *= i;
    ans %= mod;
  }
  cout << ans << endl;
  return 0;
}
Problem p02929 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define SORT(c) sort((c).begin(), (c).end())

typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> V;
typedef map<int, int> M;

constexpr ll INF = 1e18;
constexpr ll MOD = 1e9 + 7;
constexpr double PI = 3.14159265358979323846;
constexpr int di[] = {0, 0, 1, -1};
constexpr int dj[] = {1, -1, 0, 0};

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

  int n;
  string s;

  bool d[112345];

  cin >> n >> s;

  int cnt = 0;

  d[0] = false;
  for (int i = 1; i < s.size(); i++) {
    if (s[i] == s[i - 1])
      d[i] = !d[i - 1];
    else
      d[i] = d[i - 1];
    cnt += d[i];
  }

  if (cnt != n || s[0] == 'W' || s[s.size() - 1] == 'W') {
    cout << 0 << endl;
    return 0;
  }

  ll res = 1;

  int ln = 1;
  for (int i = 1; i < s.size(); i++) {
    if (d[i])
      (res *= ln) %= MOD, ln--;
    else
      ln++;
  }

  for (ll i = 1; i <= n; i++)
    (res *= i) %= MOD;

  cout << res << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define SORT(c) sort((c).begin(), (c).end())

typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> V;
typedef map<int, int> M;

constexpr ll INF = 1e18;
constexpr ll MOD = 1e9 + 7;
constexpr double PI = 3.14159265358979323846;
constexpr int di[] = {0, 0, 1, -1};
constexpr int dj[] = {1, -1, 0, 0};

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

  int n;
  string s;

  bool d[212345];

  cin >> n >> s;

  int cnt = 0;

  d[0] = false;
  for (int i = 1; i < s.size(); i++) {
    if (s[i] == s[i - 1])
      d[i] = !d[i - 1];
    else
      d[i] = d[i - 1];
    cnt += d[i];
  }

  if (cnt != n || s[0] == 'W' || s[s.size() - 1] == 'W') {
    cout << 0 << endl;
    return 0;
  }

  ll res = 1;

  int ln = 1;
  for (int i = 1; i < s.size(); i++) {
    if (d[i])
      (res *= ln) %= MOD, ln--;
    else
      ln++;
  }

  for (ll i = 1; i <= n; i++)
    (res *= i) %= MOD;

  cout << res << endl;

  return 0;
}
Problem p02929 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
ll n, dp[100008];
string s;
ll MOD = 1e9 + 7;

int main() {
  cin >> n >> s;

  ll ans = 1;
  dp[0] = 0;
  for (ll i = 1; i <= 2 * n; i++) {
    if (dp[i - 1] == 0) {
      if (s[i - 1] == 'W') {
        cout << 0 << endl;
        return 0;
      } else {
        dp[i] = 1;
      }
    } else {
      if ((s[i - 1] == 'W' && dp[i - 1] % 2 == 1) ||
          (s[i - 1] == 'B' && dp[i - 1] % 2 == 0)) {
        dp[i] = dp[i - 1] + 1;
      } else {
        ans *= dp[i - 1];
        ans %= MOD;
        dp[i] = dp[i - 1] - 1;
      }
    }
  }

  if (dp[2 * n] > 0) {
    cout << 0 << endl;
  } else {
    for (ll i = 1; i <= n; i++) {
      ans *= i;
      ans %= MOD;
    }
    cout << ans << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
ll n, dp[200008];
string s;
ll MOD = 1e9 + 7;

int main() {
  cin >> n >> s;

  ll ans = 1;
  dp[0] = 0;
  for (ll i = 1; i <= 2 * n; i++) {
    if (dp[i - 1] == 0) {
      if (s[i - 1] == 'W') {
        cout << 0 << endl;
        return 0;
      } else {
        dp[i] = 1;
      }
    } else {
      if ((s[i - 1] == 'W' && dp[i - 1] % 2 == 1) ||
          (s[i - 1] == 'B' && dp[i - 1] % 2 == 0)) {
        dp[i] = dp[i - 1] + 1;
      } else {
        ans *= dp[i - 1];
        ans %= MOD;
        dp[i] = dp[i - 1] - 1;
      }
    }
  }

  if (dp[2 * n] > 0) {
    cout << 0 << endl;
  } else {
    for (ll i = 1; i <= n; i++) {
      ans *= i;
      ans %= MOD;
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p02929 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < ((int)(n)); i++)
#define reg(i, a, b) for (int i = ((int)(a)); i <= ((int)(b)); i++)
#define irep(i, n) for (int i = ((int)(n)) - 1; i >= 0; i--)
#define ireg(i, a, b) for (int i = ((int)(b)); i >= ((int)(a)); i--)
typedef long long int lli;
typedef pair<int, int> mp;
#define fir first
#define sec second
#define IINF INT_MAX
#define LINF LLONG_MAX
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#define pque(type) priority_queue<type, vector<type>, greater<type>>
#define memst(a, b) memset(a, b, sizeof(a))
#define iter(v, ite) for (auto ite = (v).begin(); ite != (v).end(); ite++)
#define mimunum(v, x)                                                          \
  distance((v).begin(), lower_bound((v).begin(), (v).end(), x))

#define MOD ((lli)(1000000007))

char s[100005];
int main(void) {
  int n;
  scanf("%d%s", &n, s);
  lli ans = 1;
  int en = 0;
  rep(i, n * 2) {
    if ((en % 2 == 0) == (s[i] == 'W')) {
      if (en <= 0) {
        puts("0");
        return 0;
      } else {
        ans = (ans * en) % MOD;
        en--;
      }
    } else
      en++;
    // printf("%d %d\n",i,en);
  }
  if (en > 0) {
    puts("0");
    return 0;
  }
  // printf("%lld %d\n",ans);
  reg(i, 1, n) { ans = (ans * i) % MOD; }
  printf("%lld\n", ans);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < ((int)(n)); i++)
#define reg(i, a, b) for (int i = ((int)(a)); i <= ((int)(b)); i++)
#define irep(i, n) for (int i = ((int)(n)) - 1; i >= 0; i--)
#define ireg(i, a, b) for (int i = ((int)(b)); i >= ((int)(a)); i--)
typedef long long int lli;
typedef pair<int, int> mp;
#define fir first
#define sec second
#define IINF INT_MAX
#define LINF LLONG_MAX
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#define pque(type) priority_queue<type, vector<type>, greater<type>>
#define memst(a, b) memset(a, b, sizeof(a))
#define iter(v, ite) for (auto ite = (v).begin(); ite != (v).end(); ite++)
#define mimunum(v, x)                                                          \
  distance((v).begin(), lower_bound((v).begin(), (v).end(), x))

#define MOD ((lli)(1000000007))

char s[200005];
int main(void) {
  int n;
  scanf("%d%s", &n, s);
  lli ans = 1;
  int en = 0;
  rep(i, n * 2) {
    if ((en % 2 == 0) == (s[i] == 'W')) {
      if (en <= 0) {
        puts("0");
        return 0;
      } else {
        ans = (ans * en) % MOD;
        en--;
      }
    } else
      en++;
    // printf("%d %d\n",i,en);
  }
  if (en > 0) {
    puts("0");
    return 0;
  }
  // printf("%lld %d\n",ans);
  reg(i, 1, n) { ans = (ans * i) % MOD; }
  printf("%lld\n", ans);
  return 0;
}
Problem p02929 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define MODD 1000000007
using namespace std;
long long fac(int n) {
  long long ans = 1;
  for (int i = 2; i <= n; i++) {
    ans = (ans * i) % MODD;
  }
  return ans;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  string s;
  cin >> n >> s;
  int d[100005]; // 0 represents L, 1 represents R
  int l[100005] = {0}, r[100005] = {0};
  int len = s.size();
  d[0] = 0;
  for (int i = 1; i < len; i++) {
    if (s[i] == s[i - 1])
      d[i] = !d[i - 1];
    else
      d[i] = d[i - 1];
  }
  int cntL = 0, cntR = 0;
  for (int i = 0; i < len; i++) {
    if (d[i] == 0)
      cntL++;
    else
      cntR++;
  }
  long long ans = 1;
  if (cntL != cntR || s[0] == 'W' || s[len - 1] == 'W') {
    ans = 0;
  } else {
    for (int i = 1; i < len; i++) {
      if (d[i - 1] == 0) {
        l[i] = l[i - 1] + 1;
        r[i] = r[i - 1];
      } else {
        l[i] = l[i - 1];
        r[i] = r[i - 1] + 1;
      }
    }
    for (int i = 0; i < len; i++) {
      if (d[i] == 1) {
        ans = (ans * (l[i] - r[i])) % MODD;
      }
    }
    ans = (ans * fac(n)) % MODD;
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define MODD 1000000007
using namespace std;
long long fac(int n) {
  long long ans = 1;
  for (int i = 2; i <= n; i++) {
    ans = (ans * i) % MODD;
  }
  return ans;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  string s;
  cin >> n >> s;
  int d[200005];                        // 0 represents L, 1 represents R
  int l[200005] = {0}, r[200005] = {0}; // size should be 2N
  int len = s.size();
  d[0] = 0;
  for (int i = 1; i < len; i++) {
    if (s[i] == s[i - 1])
      d[i] = !d[i - 1];
    else
      d[i] = d[i - 1];
  }
  int cntL = 0, cntR = 0;
  for (int i = 0; i < len; i++) {
    if (d[i] == 0)
      cntL++;
    else
      cntR++;
  }
  long long ans = 1;
  if (cntL != cntR || s[0] == 'W' || s[len - 1] == 'W') {
    ans = 0;
  } else {
    for (int i = 1; i < len; i++) {
      if (d[i - 1] == 0) {
        l[i] = l[i - 1] + 1;
        r[i] = r[i - 1];
      } else {
        l[i] = l[i - 1];
        r[i] = r[i - 1] + 1;
      }
    }
    for (int i = 0; i < len; i++) {
      if (d[i] == 1) {
        ans = (ans * (l[i] - r[i])) % MODD;
      }
    }
    ans = (ans * fac(n)) % MODD;
  }
  cout << ans << endl;
  return 0;
}
Problem p02929 (C++)

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

typedef long long int ll;
typedef pair<ll, ll> pll;

#define FOR(i, n, m) for (ll(i) = (m); (i) < (n); ++(i))
#define REP(i, n) FOR(i, n, 0)
#define OF64 std::setprecision(10)

const ll MOD = 1000000007;
const ll INF = (ll)1e15;

bool L[100005];

int main() {
  ll N;
  string S;
  cin >> N >> S;
  memset(L, 0, sizeof(L));
  ll l = 0;
  REP(i, 2 * N) {
    if (l % 2 == 0 && S[i] == 'B') {
      L[i] = true;
      l++;
    } else if (l % 2 == 1 && S[i] == 'W') {
      L[i] = true;
      l++;
    } else
      l--;
  }
  if (l != 0) {
    cout << 0 << endl;
    return 0;
  }

  ll ans = 1;
  ll r = 0;
  REP(i, 2 * N) {
    if (!L[2 * N - 1 - i])
      r++;
    else
      ans = (ans * r--) % MOD;
  }
  REP(i, N) { ans = (ans * (i + 1)) % MOD; }
  cout << ans << endl;
  return 0;
}

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

typedef long long int ll;
typedef pair<ll, ll> pll;

#define FOR(i, n, m) for (ll(i) = (m); (i) < (n); ++(i))
#define REP(i, n) FOR(i, n, 0)
#define OF64 std::setprecision(10)

const ll MOD = 1000000007;
const ll INF = (ll)1e15;

bool L[200005];

int main() {
  ll N;
  string S;
  cin >> N >> S;
  memset(L, 0, sizeof(L));
  ll l = 0;
  REP(i, 2 * N) {
    if (l % 2 == 0 && S[i] == 'B') {
      L[i] = true;
      l++;
    } else if (l % 2 == 1 && S[i] == 'W') {
      L[i] = true;
      l++;
    } else
      l--;
  }
  if (l != 0) {
    cout << 0 << endl;
    return 0;
  }

  ll ans = 1;
  ll r = 0;
  REP(i, 2 * N) {
    if (!L[2 * N - 1 - i])
      r++;
    else
      ans = (ans * r--) % MOD;
  }
  REP(i, N) { ans = (ans * (i + 1)) % MOD; }
  cout << ans << endl;
  return 0;
}
Problem p02929 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
typedef long long LL;
int N;
string S;
int A[100001];
LL MOD = 1e9 + 7;
int main() {
  cin >> N >> S;
  if (S[0] == 'W') {
    cout << 0 << endl;
    return 0;
  }
  A[0] = 0;
  int c = 1;
  for (int i = 1; i < 2 * N; i++) {
    int a = 0;
    if (S[i] == 'B') {
      a = 1;
    }
    if (a == c) {
      A[i] = 1;
    } else {
      A[i] = 0;
    }
    c = 1 - c;
    // cout<<A[i]<<endl;
  }
  LL temp = 0;
  LL ans = 1;
  REP(i, 2 * N) {
    if (A[i] == 0)
      temp++;
    if (A[i] == 1) {
      if (temp == 0) {
        cout << 0 << endl;
        return 0;
      }
      ans *= temp;
      ans %= MOD;
      temp--;
    }
  }
  if (temp != 0) {
    cout << 0 << endl;
    return 0;
  }
  REP(i, N) {
    LL x = i + 1;
    ans *= x;
    ans %= MOD;
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
typedef long long LL;
int N;
string S;
int A[200001];
LL MOD = 1e9 + 7;
int main() {
  cin >> N >> S;
  if (S[0] == 'W') {
    cout << 0 << endl;
    return 0;
  }
  A[0] = 0;
  int c = 1;
  for (int i = 1; i < 2 * N; i++) {
    int a = 0;
    if (S[i] == 'B') {
      a = 1;
    }
    if (a == c) {
      A[i] = 1;
    } else {
      A[i] = 0;
    }
    c = 1 - c;
    // cout<<A[i]<<endl;
  }
  LL temp = 0;
  LL ans = 1;
  REP(i, 2 * N) {
    if (A[i] == 0)
      temp++;
    if (A[i] == 1) {
      if (temp == 0) {
        cout << 0 << endl;
        return 0;
      }
      ans *= temp;
      ans %= MOD;
      temp--;
    }
  }
  if (temp != 0) {
    cout << 0 << endl;
    return 0;
  }
  REP(i, N) {
    LL x = i + 1;
    ans *= x;
    ans %= MOD;
  }
  cout << ans << endl;
  return 0;
}