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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long int
#define MP make_pair
#define PB push_back
#define F first
#define S second
using namespace std;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef vector<PII> VP;
typedef pair<PII, int> PPI;
typedef pair<int, PII> PIP;

const int INF = 3e18;
const int MOD = 1000000007;
int n, a[100010];
int na, nb, nc;

int soujou[100010];
int soujou_r[100010];

bool check[100010];
int d, i;

int cb(int n, int k) {
  return soujou[n] * soujou_r[k] % MOD * soujou_r[n - k] % MOD;
}

int gyakugen(int k) {
  int ans = 1;
  int m = MOD - 2;
  while (m) {
    if (m & 1) {
      ans *= k;
      ans %= MOD;
    }
    k *= k;
    k %= MOD;
    m >>= 1;
  }
  return ans;
}

signed main() {
  cin >> n;

  for (i = 0; i < n + 1; i++) {
    cin >> a[i];
    if (check[a[i]]) {
      d = a[i];
    }
    check[a[i]] = true;
  }

  for (i = 0; i < n + 1; i++) {
    if (a[i] == d) {
      na = i;
      break;
    }
  }
  for (i = i + 1; i < n + 1; i++) {
    if (a[i] == d) {
      nb = i - na - 1;
      break;
    }
  }
  nc = n - i;

  soujou[0] = soujou_r[0] = 1;
  for (i = 1; i <= n + 1; i++) {
    soujou[i] = soujou[i - 1] * i % MOD;
    soujou_r[i] = soujou_r[i - 1] * gyakugen(i) % MOD;
  }

  for (i = 1; i <= n + 1; i++) {
    cout << (MOD + cb(n + 1, i) - cb(na + nc, i - 1)) % MOD << endl;
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define int long long int
#define MP make_pair
#define PB push_back
#define F first
#define S second
using namespace std;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef vector<PII> VP;
typedef pair<PII, int> PPI;
typedef pair<int, PII> PIP;

const int INF = 3e18;
const int MOD = 1000000007;
int n, a[100010];
int na, nb, nc;

int soujou[100010];
int soujou_r[100010];

bool check[100010];
int d, i;

int cb(int n, int k) {
  return soujou[n] * soujou_r[k] % MOD * soujou_r[n - k] % MOD;
}

int gyakugen(int k) {
  int ans = 1;
  int m = MOD - 2;
  while (m) {
    if (m & 1) {
      ans *= k;
      ans %= MOD;
    }
    k *= k;
    k %= MOD;
    m >>= 1;
  }
  return ans;
}

signed main() {
  cin >> n;

  for (i = 0; i < n + 1; i++) {
    cin >> a[i];
    if (check[a[i]]) {
      d = a[i];
    }
    check[a[i]] = true;
  }

  for (i = 0; i < n + 1; i++) {
    if (a[i] == d) {
      na = i;
      break;
    }
  }
  for (i = i + 1; i < n + 1; i++) {
    if (a[i] == d) {
      nb = i - na - 1;
      break;
    }
  }
  nc = n - i;

  soujou[0] = soujou_r[0] = 1;
  for (i = 1; i <= n + 1; i++) {
    soujou[i] = soujou[i - 1] * i % MOD;
    soujou_r[i] = soujou_r[i - 1] * gyakugen(i) % MOD;
  }

  for (i = 1; i <= n + 1; i++) {
    int ans = MOD + cb(n + 1, i);
    if (na + nc >= i - 1) {
      ans -= cb(na + nc, i - 1);
    }
    ans %= MOD;
    cout << ans << endl;
  }

  return 0;
}
Problem p03676 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
const int mod = 1e9 + 7;

int n, pos[N], f[N], invF[N];

int poww(int x, int e) {
  int ret = 1;
  while (e) {
    if (e & 1) {
      ret = 1ll * ret * x % mod;
    }
    x = 1ll * x * x % mod;
    e >>= 1;
  }
  return ret;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  f[0] = invF[0] = 1;
  for (int i = 1; i <= n + 1; i++) {
    f[i] = 1ll * f[i - 1] * i % mod;
    invF[i] = poww(f[i], mod - 2);
  }
  int x;
  for (int i = 1; i <= n + 1; i++) {
    cin >> x;
    if (pos[x]) {
      for (int k = 1; k <= n + 1; k++) {
        cout << (1ll * f[n + 1] * invF[k] % mod * invF[n + 1 - k] % mod -
                 1ll * f[pos[x] - 1 + n + 1 - i] * invF[k - 1] % mod *
                     invF[pos[x] + n + 1 - i - k] % mod +
                 mod) %
                    mod
             << "\n";
      }
      return 0;
    }
    pos[x] = i;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
const int mod = 1e9 + 7;

int n, pos[N], f[N], invF[N];

int poww(int x, int e) {
  int ret = 1;
  while (e) {
    if (e & 1) {
      ret = 1ll * ret * x % mod;
    }
    x = 1ll * x * x % mod;
    e >>= 1;
  }
  return ret;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  f[0] = invF[0] = 1;
  for (int i = 1; i <= n + 1; i++) {
    f[i] = 1ll * f[i - 1] * i % mod;
    invF[i] = poww(f[i], mod - 2);
  }
  int x;
  for (int i = 1; i <= n + 1; i++) {
    cin >> x;
    if (pos[x]) {
      for (int k = 1; k <= n + 1; k++) {
        cout << (1ll * f[n + 1] * invF[k] % mod * invF[n + 1 - k] % mod -
                 (pos[x] - 1 + n + 1 - i < (k - 1)
                      ? 0
                      : 1ll * f[pos[x] - 1 + n + 1 - i] * invF[k - 1] % mod *
                            invF[pos[x] + n + 1 - i - k] % mod) +
                 mod) %
                    mod
             << "\n";
      }
      return 0;
    }
    pos[x] = i;
  }
}
Problem p03676 (C++)

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

using namespace std;

// #define FILE_IO

const int mod = 1e9 + 7;

int fct[200005], ifct[200005];

int N, pos1, pos2;
int pos[100005];
int sol[100005];

int power(int x, int y) {
  if (y <= 0)
    return 1;
  int ans = power((1LL * x * x) % mod, y >> 1);
  if (y & 1)
    ans = (1LL * ans * x) % mod;
  return ans;
}

int cmb(int N, int K) {
  if (K > N)
    return 0;
  int ans = fct[N];
  ans = (1LL * ans * ifct[N - K]) % mod;
  ans = (1LL * ans * ifct[K]) % mod;
  return ans;
}

int main() {
#ifdef FILE_IO
  freopen("1.in", "r", stdin);
  freopen("1.out", "w", stdout);
#endif

  int M = 200000;
  fct[0] = 1;
  for (int i = 1; i <= M; i++)
    fct[i] = (1LL * fct[i - 1] * i) % mod;
  ifct[M] = power(fct[M], mod - 2);
  for (int i = M - 1; i >= 0; i--)
    ifct[i] = (1LL * ifct[i + 1] * (i + 1)) % mod;

  scanf("%d", &N);
  for (int i = 1; i <= N + 1; i++) {
    int x;
    scanf("%d", &x);
    if (pos[x]) {
      pos1 = pos[x];
      pos2 = i;
    }
    pos[x] = i;
  }

  for (int i = 1; i <= N + 1; i++)
    sol[i] = cmb(N + 1, i);

  for (int i = 0; i < pos1; i++)
    for (int j = 0; j <= N + 1 - pos2; j++)
      sol[i + j + 1] =
          (sol[i + j + 1] -
           ((1LL * cmb(pos1 - 1, i) * cmb(N + 1 - pos2, j)) % mod) + mod) %
          mod;

  for (int i = 1; i <= N + 1; i++)
    printf("%d\n", sol[i]);

  return 0;
}


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

using namespace std;

// #define FILE_IO

const int mod = 1e9 + 7;

int fct[200005], ifct[200005];

int N, pos1, pos2;
int pos[100005];
int sol[100005];

int power(int x, int y) {
  if (y <= 0)
    return 1;
  int ans = power((1LL * x * x) % mod, y >> 1);
  if (y & 1)
    ans = (1LL * ans * x) % mod;
  return ans;
}

int cmb(int N, int K) {
  if (K > N)
    return 0;
  int ans = fct[N];
  ans = (1LL * ans * ifct[N - K]) % mod;
  ans = (1LL * ans * ifct[K]) % mod;
  return ans;
}

int main() {
#ifdef FILE_IO
  freopen("1.in", "r", stdin);
  freopen("1.out", "w", stdout);
#endif

  int M = 200000;
  fct[0] = 1;
  for (int i = 1; i <= M; i++)
    fct[i] = (1LL * fct[i - 1] * i) % mod;
  ifct[M] = power(fct[M], mod - 2);
  for (int i = M - 1; i >= 0; i--)
    ifct[i] = (1LL * ifct[i + 1] * (i + 1)) % mod;

  scanf("%d", &N);
  for (int i = 1; i <= N + 1; i++) {
    int x;
    scanf("%d", &x);
    if (pos[x]) {
      pos1 = pos[x];
      pos2 = i;
    }
    pos[x] = i;
  }

  for (int i = 1; i <= N + 1; i++)
    sol[i] = cmb(N + 1, i);

  int L = pos1 - 1 + (N + 1) - pos2;
  for (int i = 1; i <= N + 1; i++)
    sol[i] = (sol[i] - cmb(L, i - 1) + mod) % mod;

  for (int i = 1; i <= N + 1; i++)
    printf("%d\n", sol[i]);

  return 0;
}
Problem p03676 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
using namespace std;

const int MAXN = 100005, P = 1e9 + 7;
int n, a[MAXN], ans[MAXN], pos_le, pos_ri, fac[MAXN], fac_inv[MAXN],
    last_pos[MAXN];

inline int read() {
  char c;
  int x;
  while (c = getchar(), c < '0' || '9' < c)
    ;
  x = c - '0';
  while (c = getchar(), '0' <= c && c <= '9')
    x = x * 10 + c - '0';
  return x;
}

inline void pre_tab() {
  fac[0] = 1;
  for (int i = 1; i <= n; ++i)
    fac[i] = (long long)fac[i - 1] * i % P;
  fac_inv[n] = 1;
  for (int i = P - 2, tmp = fac[n]; i;
       i >>= 1, tmp = (long long)tmp * tmp % P) {
    if (i & 1)
      fac_inv[n] = (long long)fac_inv[n] * tmp % P;
  }
  for (int i = n - 1; i >= 0; --i)
    fac_inv[i] = (long long)fac_inv[i + 1] * (i + 1) % P;
}

inline int C(int m, int n) {
  return (long long)fac[m] * fac_inv[n] % P * fac_inv[m - n] % P;
}

int main() {
  n = read();
  ++n;
  pre_tab();
  for (int i = 1; i <= n; ++i) {
    a[i] = read();
    if (last_pos[a[i]]) {
      pos_le = last_pos[a[i]];
      pos_ri = i;
    }
    last_pos[a[i]] = i;
  }
  ans[1] = n - 1;
  for (int i = 2; i <= n; ++i) {
    ans[i] = C(n, i);
    for (int j = max(i - 1 - n + pos_ri, 0); j <= pos_le - 1; ++j)
      ans[i] =
          (ans[i] - (long long)C(pos_le - 1, j) * C(n - pos_ri, i - j - 1) % P +
           P) %
          P;
  }
  for (int i = 1; i <= n; ++i)
    printf("%d\n", ans[i]);
  return 0;
}

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

const int MAXN = 100005, P = 1e9 + 7;
int n, a[MAXN], ans[MAXN], pos_le, pos_ri, fac[MAXN], fac_inv[MAXN],
    last_pos[MAXN];

inline int read() {
  char c;
  int x;
  while (c = getchar(), c < '0' || '9' < c)
    ;
  x = c - '0';
  while (c = getchar(), '0' <= c && c <= '9')
    x = x * 10 + c - '0';
  return x;
}

inline void pre_tab() {
  fac[0] = 1;
  for (int i = 1; i <= n; ++i)
    fac[i] = (long long)fac[i - 1] * i % P;
  fac_inv[n] = 1;
  for (int i = P - 2, tmp = fac[n]; i;
       i >>= 1, tmp = (long long)tmp * tmp % P) {
    if (i & 1)
      fac_inv[n] = (long long)fac_inv[n] * tmp % P;
  }
  for (int i = n - 1; i >= 0; --i)
    fac_inv[i] = (long long)fac_inv[i + 1] * (i + 1) % P;
}

inline int C(int m, int n) {
  return (long long)fac[m] * fac_inv[n] % P * fac_inv[m - n] % P;
}

int main() {
  n = read();
  ++n;
  pre_tab();
  for (int i = 1; i <= n; ++i) {
    a[i] = read();
    if (last_pos[a[i]]) {
      pos_le = last_pos[a[i]];
      pos_ri = i;
    }
    last_pos[a[i]] = i;
  }
  ans[1] = n - 1;
  for (int i = 2; i <= n; ++i) {
    ans[i] = C(n, i);
    if (n - pos_ri + pos_le >= i - 1)
      ans[i] = (ans[i] - C(n - pos_ri + pos_le - 1, i - 1) + P) % P;
    // for(int j=max(i-1-n+pos_ri,0);j<=pos_le-1;++j)
    // ans[i]=(ans[i]-(long long)C(pos_le-1,j)*C(n-pos_ri,i-j-1)%P+P)%P;
  }
  for (int i = 1; i <= n; ++i)
    printf("%d\n", ans[i]);
  return 0;
}
Problem p03676 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
//---------------------------------------------------------------------------------------------------
ll MOD = 1000000007;
ll f[101010], rf[101010];
ll inv(ll x) {
  ll res = 1;
  ll k = MOD - 2;
  ll y = x;
  while (k) {
    if (k & 1)
      res = (res * y) % MOD;
    y = (y * y) % MOD;
    k /= 2;
  }
  return res;
}
void init() {
  f[0] = 1;
  for (int i = 1; i < 101010; i++)
    f[i] = (f[i - 1] * i) % MOD;
  for (int i = 0; i < 101010; i++)
    rf[i] = inv(f[i]);
}
ll C(int n, int k) {
  ll a = f[n];      // = n!
  ll b = rf[n - k]; // = (n-k)!
  ll c = rf[k];     // = k!

  ll bc = (b * c) % MOD;

  return (a * bc) % MOD;
}
//---------------------------------------------------------------------------------------------------

int main() {
  init();
  ll n;
  cin >> n;
  vector<ll> a(n + 1);
  int left = 0;
  int right = 0;
  for (int i = 0; i < n + 1; i++) {
    ll b;
    cin >> b;
    if (a[b] == 0) {
      a[b] = i + 1;
    } else {
      left = a[b];
      right = i + 1;
    }
  }
  int all = left + n - right;
  cout << n << endl;
  for (int k = 2; k <= n + 1; k++) {
    ll sum = C(n + 1, k);
    ll dec = C(all, k - 1);
    cout << (sum - dec + MOD) % MOD << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
//---------------------------------------------------------------------------------------------------
ll MOD = 1000000007;
ll f[101010], rf[101010];
ll inv(ll x) {
  ll res = 1;
  ll k = MOD - 2;
  ll y = x;
  while (k) {
    if (k & 1)
      res = (res * y) % MOD;
    y = (y * y) % MOD;
    k /= 2;
  }
  return res;
}
void init() {
  f[0] = 1;
  for (int i = 1; i < 101010; i++)
    f[i] = (f[i - 1] * i) % MOD;
  for (int i = 0; i < 101010; i++)
    rf[i] = inv(f[i]);
}
ll C(int n, int k) {
  ll a = f[n];      // = n!
  ll b = rf[n - k]; // = (n-k)!
  ll c = rf[k];     // = k!

  ll bc = (b * c) % MOD;

  return (a * bc) % MOD;
}
//---------------------------------------------------------------------------------------------------

int main() {
  init();
  ll n;
  cin >> n;
  vector<ll> a(n + 1);
  int left = 0;
  int right = 0;
  for (int i = 0; i < n + 1; i++) {
    ll b;
    cin >> b;
    if (a[b] == 0) {
      a[b] = i + 1;
    } else {
      left = a[b];
      right = i + 1;
    }
  }
  int all = left + n - right;
  cout << n << endl;
  for (int k = 2; k <= n + 1; k++) {
    ll sum = C(n + 1, k);
    ll dec = 0;
    if (all >= k - 1) {
      dec = C(all, k - 1);
    }
    cout << (sum - dec + MOD) % MOD << endl;
  }
}
Problem p03676 (C++)

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

using namespace std;
typedef long long ll;
const int MAXN = 200100;
const ll MOD = 1e9 + 7;

int N;
int arr[MAXN];
ll fac[MAXN];
ll finv[MAXN];

ll cpow(ll b, int e) {
  if (e == 0)
    return 1;
  ll v = cpow(b, e / 2);
  v = (v * v) % MOD;
  if (e % 2)
    v = (v * b) % MOD;
  return v;
}

ll ch(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0)
    return 0;
  if (k < 0)
    return 0;
  ll v = (fac[n] * finv[k]) % MOD;
  v = (v * finv[n - k]) % MOD;
  return v;
}

int main() {
  ios_base::sync_with_stdio(0);

  fac[0] = 1;
  finv[0] = 1;
  for (int i = 1; i < MAXN; i++) {
    fac[i] = (fac[i - 1] * i) % MOD;
    finv[i] = cpow(fac[i], MOD - 2);
  }

  cin >> N;
  ll ctot = 0;
  for (int i = 0; i <= N; i++) {
    cin >> arr[i];
    ctot += arr[i];
  }

  ctot -= (N * (N + 1)) / 2;
  int llo = 0, lhi = N;
  while (arr[llo] != ctot)
    llo++;
  while (arr[lhi] != ctot)
    lhi--;

  int nd = lhi - llo - 1;
  for (int i = 1; i <= N; i++) {
    ll cc =
        ch(N, i) + ch(N - 1, i - 1) - ch(N - nd - 1, i - 1) + ch(N - 1, i - 2);
    cc = (cc + MOD) % MOD;
    cout << cc << "\n";
  }
  cout << "1\n";
}

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

using namespace std;
typedef long long ll;
const int MAXN = 200100;
const ll MOD = 1e9 + 7;

int N;
int arr[MAXN];
ll fac[MAXN];
ll finv[MAXN];

ll cpow(ll b, int e) {
  if (e == 0)
    return 1;
  ll v = cpow(b, e / 2);
  v = (v * v) % MOD;
  if (e % 2)
    v = (v * b) % MOD;
  return v;
}

ll ch(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0)
    return 0;
  if (k < 0)
    return 0;
  ll v = (fac[n] * finv[k]) % MOD;
  v = (v * finv[n - k]) % MOD;
  return v;
}

int main() {
  ios_base::sync_with_stdio(0);

  fac[0] = 1;
  finv[0] = 1;
  for (int i = 1; i < MAXN; i++) {
    fac[i] = (fac[i - 1] * i) % MOD;
    finv[i] = cpow(fac[i], MOD - 2);
  }

  cin >> N;
  ll ctot = 0;
  for (int i = 0; i <= N; i++) {
    cin >> arr[i];
    ctot += arr[i];
  }

  ctot -= (N * ((ll)N + 1)) / 2;
  int llo = 0, lhi = N;
  while (arr[llo] != ctot)
    llo++;
  while (arr[lhi] != ctot)
    lhi--;

  int nd = lhi - llo - 1;
  for (int i = 1; i <= N; i++) {
    ll cc =
        ch(N, i) + ch(N - 1, i - 1) - ch(N - nd - 1, i - 1) + ch(N - 1, i - 2);
    cc = (cc + MOD) % MOD;
    cout << cc << "\n";
  }
  cout << "1\n";
}
Problem p03676 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
typedef long long ll;
int a[100005];
int p[100005];

ll MOD = 1000000007;
ll fr[200005];
ll inv(ll d) {
  // d^(MOD-1) == 1
  ll m = MOD - 2;
  ll ret = 1;
  while (m > 0) {
    if (m & 1) {
      ret = (ret * d) % MOD;
    }
    d = (d * d) % MOD;
    m >>= 1;
  }
  return ret;
}
ll comb(ll a, ll b) {
  // nCb = (a+b)! / a!b!
  ll ret = fr[a + b];
  ret = (ret * inv(fr[a])) % MOD;
  ret = (ret * inv(fr[b])) % MOD;
  return ret;
}
int main(void) {
  ios::sync_with_stdio(false);
  int N, d;
  cin >> N;

  fr[0] = 1;
  for (ll i = 1; i <= 100005; i++) {
    fr[i] = (fr[i - 1] * i) % MOD;
  }

  for (int i = 1; i <= N + 1; i++) {
    cin >> a[i];
    if (p[a[i]] == 0) {
      p[a[i]] = i;
    } else {
      d = i - p[a[i]];
      d = N - d;
    }
  }

  for (int i = 1; i <= N + 1; i++) {
    // cout << C[c][i] - D[i-1] << endl;
    if (i - 1 >= 0) {
      cout << (comb(N + 1 - i, i) - comb(d - (i - 1), i - 1) + MOD) % MOD
           << endl;
    } else {
      cout << comb(N + 1 - i, i) << endl;
    }
  }
}

Fixed submission:
#include <iostream>
using namespace std;
typedef long long ll;
int a[100005];
int p[100005];

ll MOD = 1000000007;
ll fr[200005];
ll inv(ll d) {
  // d^(MOD-1) == 1
  ll m = MOD - 2;
  ll ret = 1;
  while (m > 0) {
    if (m & 1) {
      ret = (ret * d) % MOD;
    }
    d = (d * d) % MOD;
    m >>= 1;
  }
  return ret;
}
ll comb(ll a, ll b) {
  // nCb = (a+b)! / a!b!
  ll ret = fr[a + b];
  ret = (ret * inv(fr[a])) % MOD;
  ret = (ret * inv(fr[b])) % MOD;
  return ret;
}
int main(void) {
  ios::sync_with_stdio(false);
  int N, d;
  cin >> N;

  fr[0] = 1;
  for (ll i = 1; i <= 100005; i++) {
    fr[i] = (fr[i - 1] * i) % MOD;
  }

  for (int i = 1; i <= N + 1; i++) {
    cin >> a[i];
    if (p[a[i]] == 0) {
      p[a[i]] = i;
    } else {
      d = i - p[a[i]];
      d = N - d;
    }
  }

  for (int i = 1; i <= N + 1; i++) {
    // cout << C[c][i] - D[i-1] << endl;
    if (d - (i - 1) >= 0) {
      cout << (comb(N + 1 - i, i) - comb(d - (i - 1), i - 1) + MOD) % MOD
           << endl;
    } else {
      cout << comb(N + 1 - i, i) << endl;
    }
  }
}
Problem p03676 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef long long LL;
const LL MOD = 1000000007LL;
bool b[100001] = {false};
int num[100001];
LL fact[100001];
LL inv[100001];
LL inv_fact[100001];
LL mod_pow(LL x, LL a) {
  if (a == 0)
    return 1;
  if (a % 2 == 0) {
    LL ret = mod_pow(x, a / 2);
    return (ret * ret) % MOD;
  } else {
    return (x * mod_pow(x, a - 1)) % MOD;
  }
}
LL comb(LL n, LL k) {
  if (n < k)
    return 0;
  LL ans = (fact[n] * inv_fact[k]) % MOD;
  ans = (ans * inv_fact[n - k]) % MOD;
  return ans;
}
int main() {
  int n, dup[2];
  cin >> n;
  for (int i = 0; i <= n; i++) {
    int a;
    cin >> a;
    num[i] = a;
    if (!b[a - 1])
      b[a - 1] = true;
    else {
      dup[1] = i;
      break;
    }
  }
  for (int i = 0; i < dup[1]; i++) {
    if (num[i] == num[dup[1]])
      dup[0] = i;
  }
  fact[0] = 1;
  inv_fact[0] = 1;
  for (int i = 1; i < n + 1; i++) {
    inv[i] = mod_pow(i, MOD - 2);
    fact[i] = (fact[i - 1] * i) % MOD;
    inv_fact[i] = (inv_fact[i - 1] * inv[i]) % MOD;
  }
  int cen = dup[1] - dup[0] - 1;
  cout << n << endl;
  for (int i = 2; i <= n + 1; i++) {
    LL sum = 0;
    sum += comb(n - 1, i) % MOD;
    sum += comb(n - 1, i - 2) % MOD;
    sum += comb(n - cen - 1, i - 1) % MOD;
    for (int j = max(1, i - n + cen - 1); j < min(i, cen + 1); j++) {
      sum += 2 * comb(cen, j) * comb(n - cen - 1, i - j - 1) % MOD;
    }
    cout << sum % MOD << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef long long LL;
const LL MOD = 1000000007LL;
bool b[100001] = {false};
int num[100001];
LL fact[100001];
LL inv[100001];
LL inv_fact[100001];
LL mod_pow(LL x, LL a) {
  if (a == 0)
    return 1;
  if (a % 2 == 0) {
    LL ret = mod_pow(x, a / 2);
    return (ret * ret) % MOD;
  } else {
    return (x * mod_pow(x, a - 1)) % MOD;
  }
}
LL comb(LL n, LL k) {
  if (n < k)
    return 0;
  LL ans = (fact[n] * inv_fact[k]) % MOD;
  ans = (ans * inv_fact[n - k]) % MOD;
  return ans;
}
int main() {
  int n, dup[2];
  cin >> n;
  for (int i = 0; i <= n; i++) {
    int a;
    cin >> a;
    num[i] = a;
    if (!b[a - 1])
      b[a - 1] = true;
    else {
      dup[1] = i;
      break;
    }
  }
  for (int i = 0; i < dup[1]; i++) {
    if (num[i] == num[dup[1]])
      dup[0] = i;
  }
  fact[0] = 1;
  inv_fact[0] = 1;
  for (int i = 1; i < n + 1; i++) {
    inv[i] = mod_pow(i, MOD - 2);
    fact[i] = (fact[i - 1] * i) % MOD;
    inv_fact[i] = (inv_fact[i - 1] * inv[i]) % MOD;
  }
  int cen = dup[1] - dup[0] - 1;
  cout << n << endl;
  for (int i = 2; i <= n + 1; i++) {
    LL sum = 0;
    sum = (sum + comb(n - 1, i)) % MOD;
    sum = (sum + comb(n - 1, i - 2)) % MOD;
    sum = (sum + 2 * comb(n - 1, i - 1)) % MOD;
    sum = (sum - comb(n - cen - 1, i - 1) + MOD) % MOD;
    cout << sum % MOD << endl;
  }
}
Problem p03676 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define mod 1000000007
#define imfor(i, n) for (int i = 0; i < n; i++)
using namespace std;
using ll = long long;

ll inv[1001000];
ll fac[10001000];
ll invfac[1001000];

ll getcon(ll a, ll b) { return fac[a] * invfac[b] % mod * invfac[a - b] % mod; }

int main() {
  ll n;
  cin >> n;
  inv[1] = 1;
  fac[0] = 1;
  invfac[0] = 1;
  for (ll i = 1; i < n + 10; i++) {
    if (i > 1) {
      inv[i] = (mod - (mod / i) * inv[mod % i] % mod) % mod;
    }
    fac[i] = (fac[i - 1] * i) % mod;
    invfac[i] = invfac[i - 1] * inv[i] % mod;
  }
  ll a[100100];
  fill(a, a + n + 1, -1);
  ll lef, rig;
  imfor(i, n + 1) {
    ll tmp;
    cin >> tmp;
    if (a[tmp] != -1) {
      lef = a[tmp];
      rig = n - i;
    } else {
      a[tmp] = i;
    }
  }
  for (ll i = 1; i <= n + 1; i++) {
    cout << (getcon(n + 1, i) - getcon(lef + rig, i - 1)) % mod << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define mod 1000000007
#define imfor(i, n) for (int i = 0; i < n; i++)
using namespace std;
using ll = long long;

ll inv[1001000];
ll fac[10001000];
ll invfac[1001000];

ll getcon(ll a, ll b) { return fac[a] * invfac[b] % mod * invfac[a - b] % mod; }

int main() {
  ll n;
  cin >> n;
  inv[1] = 1;
  fac[0] = 1;
  invfac[0] = 1;
  for (ll i = 1; i < n + 10; i++) {
    if (i > 1) {
      inv[i] = (mod - (mod / i) * inv[mod % i] % mod) % mod;
    }
    fac[i] = (fac[i - 1] * i) % mod;
    invfac[i] = invfac[i - 1] * inv[i] % mod;
  }
  ll a[100100];
  fill(a, a + n + 1, -1);
  ll lef, rig;
  imfor(i, n + 1) {
    ll tmp;
    cin >> tmp;
    if (a[tmp] != -1) {
      lef = a[tmp];
      rig = n - i;
    } else {
      a[tmp] = i;
    }
  }
  for (ll i = 1; i <= n + 1; i++) {
    ll ans = getcon(n + 1, i);
    if (i - 1 <= lef + rig) {
      ans -= getcon(lef + rig, i - 1);
    }
    ans %= mod;
    if (ans < 0) {
      ans += mod;
    }
    cout << ans << endl;
  }
}
Problem p03676 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i, n) FOR(i, 0, n)
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define PB push_back
#define LB lower_bound
#define UB upper_bound
#define PQ priority_queue
#define UM unordered_map
#define US unordered_set
#define ALL(a) (a).begin(), (a).end()
typedef vector<ll> vi;
typedef vector<vector<ll>> vvi;
const ll INF = (1ll << 60);
typedef pair<ll, ll> pii;
const ll MOD = 1000000007;

pii extendedEuclid(ll a, ll b) {
  bool swapped = false;
  if (a < b) {
    swap(a, b);
    swapped = true;
  }
  pii x = {1, 0}, y = {0, 1};
  while (1) {
    ll xv = a * x.first + b * x.second;
    ll yv = a * y.first + b * y.second;
    if (yv == 0) {
      if (swapped)
        return {x.second, x.first};
      else
        return x;
    }
    ll d = xv / yv;
    swap(x, y);
    y.first -= d * x.first;
    y.second -= d * x.second;
  }
}

ll inverse(ll a, ll mod) {
  return (mod + extendedEuclid(a, mod).first % mod) % mod;
}

int main() {
  ll n;
  cin >> n;
  vi a(n + 1);
  REP(i, n + 1) {
    cin >> a[i];
    a[i] -= 1;
  }
  vvi v(n);
  REP(i, n + 1) v[a[i]].PB(i);
  ll y;
  for (vi &i : v)
    if (i.size() > 1)
      y = i[1] - i[0];
  y = (n - y);

  vi c1(n + 2), c2(y + 1);
  c1[0] = 1;
  c2[0] = 1;
  FOR(i, 1, n + 2)
  c1[i] = (c1[i - 1] * ((n + 2 - i) * inverse(i, MOD) % MOD)) % MOD;
  FOR(i, 1, y + 1)
  c2[i] = (c2[i - 1] * ((y + 1 - i) * inverse(i, MOD) % MOD)) % MOD;
  FOR(k, 1, n + 2) { cout << (c1[k] - c2[k - 1] + MOD) % MOD << endl; }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i, n) FOR(i, 0, n)
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define PB push_back
#define LB lower_bound
#define UB upper_bound
#define PQ priority_queue
#define UM unordered_map
#define US unordered_set
#define ALL(a) (a).begin(), (a).end()
typedef vector<ll> vi;
typedef vector<vector<ll>> vvi;
const ll INF = (1ll << 60);
typedef pair<ll, ll> pii;
const ll MOD = 1000000007;

pii extendedEuclid(ll a, ll b) {
  bool swapped = false;
  if (a < b) {
    swap(a, b);
    swapped = true;
  }
  pii x = {1, 0}, y = {0, 1};
  while (1) {
    ll xv = a * x.first + b * x.second;
    ll yv = a * y.first + b * y.second;
    if (yv == 0) {
      if (swapped)
        return {x.second, x.first};
      else
        return x;
    }
    ll d = xv / yv;
    swap(x, y);
    y.first -= d * x.first;
    y.second -= d * x.second;
  }
}

ll inverse(ll a, ll mod) {
  return (mod + extendedEuclid(a, mod).first % mod) % mod;
}

int main() {
  ll n;
  cin >> n;
  vi a(n + 1);
  REP(i, n + 1) {
    cin >> a[i];
    a[i] -= 1;
  }
  vvi v(n);
  REP(i, n + 1) v[a[i]].PB(i);
  ll y;
  for (vi &i : v)
    if (i.size() > 1)
      y = i[1] - i[0];
  y = (n - y);

  vi c1(n + 2), c2(n + 2);
  c1[0] = 1;
  c2[0] = 1;
  FOR(i, 1, n + 2)
  c1[i] = (c1[i - 1] * ((n + 2 - i) * inverse(i, MOD) % MOD)) % MOD;
  FOR(i, 1, y + 1)
  c2[i] = (c2[i - 1] * ((y + 1 - i) * inverse(i, MOD) % MOD)) % MOD;
  FOR(k, 1, n + 2) { cout << (c1[k] - c2[k - 1] + MOD) % MOD << endl; }
}
Problem p03676 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;

typedef long long ll;

vector<ll> fact(100005), fact_inv(100005);
vector<int> appear(100005, 0);
int l, r;
ll mod = 1e9 + 7;

ll func(ll n, ll p) {
  if (p == 1)
    return n;
  ll res = func(n, p / 2);
  res *= res;
  res %= mod;
  if (p % 2 == 1)
    return (res * n) % mod;
  return res;
}

int main() {
  int n;
  cin >> n;
  ll f = 1;
  fact[0] = fact_inv[0] = 1;
  for (int i = 1; i <= n + 1; ++i) {
    f *= i;
    f %= mod;
    fact.at(i) = f;
    fact_inv.at(i) = func(f, mod - 2);
  }

  for (int i = 1; i <= n; ++i) {
    int a;
    cin >> a;
    if (appear.at(a) != 0) {
      l = appear.at(a) - 1;
      r = n + 1 - i;
    }
    appear.at(a) = i;
  }

  cout << n << endl;
  for (int k = 2; k <= n; ++k) {
    ll ans =
        ((fact.at(n + 1) * fact_inv.at(k)) % mod * fact_inv.at(n + 1 - k)) %
        mod;
    for (int t = 0; t <= k - 1; ++t) {
      if (l < t)
        break;
      if (r < k - 1 - t)
        continue;
      ll d = ((fact.at(l) * fact_inv.at(t)) % mod * fact_inv.at(l - t)) % mod;
      d *= ((fact.at(r) * fact_inv.at(k - 1 - t)) % mod *
            fact_inv.at(r - k + 1 + t)) %
           mod;
      d %= mod;
      ans = (ans + mod - d) % mod;
    }
    cout << ans << endl;
  }
  cout << 1 << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;

typedef long long ll;

vector<ll> fact(100005), fact_inv(100005);
vector<int> appear(100005, 0);
int l, r;
ll mod = 1e9 + 7;

ll func(ll n, ll p) {
  if (p == 1)
    return n;
  ll res = func(n, p / 2);
  res *= res;
  res %= mod;
  if (p % 2 == 1)
    return (res * n) % mod;
  return res;
}

int main() {
  int n;
  cin >> n;
  ll f = 1;
  fact[0] = fact_inv[0] = 1;
  for (int i = 1; i <= n + 1; ++i) {
    f *= i;
    f %= mod;
    fact.at(i) = f;
    fact_inv.at(i) = func(f, mod - 2);
  }

  for (int i = 1; i <= n; ++i) {
    int a;
    cin >> a;
    if (appear.at(a) != 0) {
      l = appear.at(a) - 1;
      r = n + 1 - i;
    }
    appear.at(a) = i;
  }

  cout << n << endl;
  for (int k = 2; k <= n; ++k) {
    ll ans =
        ((fact.at(n + 1) * fact_inv.at(k)) % mod * fact_inv.at(n + 1 - k)) %
        mod;
    if (l + r >= k - 1)
      ans = (ans + mod -
             ((fact[l + r] * fact_inv[k - 1]) % mod * fact_inv[l + r - k + 1]) %
                 mod) %
            mod;
    cout << ans << endl;
  }
  cout << 1 << endl;

  return 0;
}
Problem p03676 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3f
#define EPS (1e-10)
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int a[400000];
ll fact[400000];

ll ppow(ll a) {
  ll b = MOD - 2;
  ll res = 1;
  while (b) {
    if (b & 1)
      res = (res * a) % MOD;
    a = (a * a) % MOD;
    b >>= 1;
  }
  return res;
}
ll C(ll a, ll b) {
  return (fact[a] * ppow((fact[b] * fact[a - b]) % MOD)) % MOD;
}
bool used[400000];
signed main() {
  int n;
  scanf("%d", &n);
  int b = 0;
  rep(i, n + 1) {
    scanf("%d", &a[i]);
    if (used[a[i]])
      b = a[i];
    used[a[i]] = 1;
  }
  fact[0] = 1;
  for (int i = 1; i <= n + 1; i++)
    fact[i] = (fact[i - 1] * i) % MOD;
  int s = -1, g;
  rep(i, n + 1) {
    if (a[i] == b) {
      if (s == -1)
        s = i;
      else
        g = i;
    }
  }
  for (int i = 1; i <= n + 1; i++) {
    cout << (C(n + 1, i) + MOD - C(s + n - g, i - 1)) % MOD << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3f
#define EPS (1e-10)
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int a[400000];
ll fact[400000];

ll ppow(ll a) {
  ll b = MOD - 2;
  ll res = 1;
  while (b) {
    if (b & 1)
      res = (res * a) % MOD;
    a = (a * a) % MOD;
    b >>= 1;
  }
  return res;
}
ll C(ll a, ll b) {
  return (fact[a] * ppow((fact[b] * fact[a - b]) % MOD)) % MOD;
}
bool used[400000];
signed main() {
  int n;
  scanf("%d", &n);
  int b = 0;
  rep(i, n + 1) {
    scanf("%d", &a[i]);
    if (used[a[i]])
      b = a[i];
    used[a[i]] = 1;
  }
  fact[0] = 1;
  for (int i = 1; i <= n + 1; i++)
    fact[i] = (fact[i - 1] * i) % MOD;
  int s = -1, g;
  rep(i, n + 1) {
    if (a[i] == b) {
      if (s == -1)
        s = i;
      else
        g = i;
    }
  }
  for (int i = 1; i <= n + 1; i++) {
    printf("%lld\n", (C(n + 1, i) + MOD -
                      (s + n - g >= i - 1 ? C(s + n - g, i - 1) : 0)) %
                         MOD);
  }
}
Problem p03676 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>
using namespace std;
typedef long long LL;
static const int MOD = 1000000007;
static const int MAXN = 100000 + 10;

LL factors[MAXN];

LL power(LL base, int n) {
  LL cur = base;
  LL ret = 1;
  while (n) {
    if (n & 1) {
      ret *= cur;
      ret %= MOD;
    }
    cur *= cur;
    cur %= MOD;
    n = n >> 1;
  }
  return ret;
}

LL combine(int n, int k) {
  LL ret = 1;
  ret *= factors[n];
  ret *= power(factors[k], MOD - 2);
  ret %= MOD;
  ret *= power(factors[n - k], MOD - 2);
  ret %= MOD;
  return ret;
}

LL compute(int n, int k, int d) {
  if (k == 1)
    return n;
  if (k == n + 1)
    return 1;
  LL ret = combine(n + 1, k);
  if (n - d >= k)
    ret -= combine(n - 1 - d, k - 1);
  if (ret < 0)
    ret += MOD;
  return ret;
}

int main() {
  //    freopen("test.txt", "r", stdin);

  int n;
  scanf("%d", &n);

  factors[0] = 1;
  for (int i = 1; i <= n + 1; i++) {
    factors[i] = factors[i - 1] * i;
    factors[i] %= MOD;
  }

  vector<LL> nums(n + 1, 0);
  LL cnt = 0;
  for (int i = 0; i <= n; i++) {
    scanf("%lld", &nums[i]);
    cnt += nums[i];
  }
  LL tmp = (n + 1) * n;
  LL dupitem = cnt - (tmp >> 1);
  vector<int> dupos;
  for (int i = 0; i <= n; i++) {
    if (nums[i] == dupitem)
      dupos.push_back(i);
    if (dupos.size() == 2)
      break;
  }
  int d = dupos[1] - dupos[0] - 1;
  for (int k = 1; k <= n + 1; k++) {
    cout << compute(n, k, d) << endl;
  }
}


Fixed submission:
#include <iostream>
#include <vector>
using namespace std;
typedef long long LL;
static const int MOD = 1000000007;
static const int MAXN = 100000 + 10;

LL factors[MAXN];

LL power(LL base, int n) {
  LL cur = base;
  LL ret = 1;
  while (n) {
    if (n & 1) {
      ret *= cur;
      ret %= MOD;
    }
    cur *= cur;
    cur %= MOD;
    n = n >> 1;
  }
  return ret;
}

LL combine(int n, int k) {
  LL ret = 1;
  ret *= factors[n];
  ret *= power(factors[k], MOD - 2);
  ret %= MOD;
  ret *= power(factors[n - k], MOD - 2);
  ret %= MOD;
  return ret;
}

LL compute(int n, int k, int d) {
  if (k == 1)
    return n;
  if (k == n + 1)
    return 1;
  LL ret = combine(n + 1, k);
  if (n - d >= k)
    ret -= combine(n - 1 - d, k - 1);
  if (ret < 0)
    ret += MOD;
  return ret;
}

int main() {
  //    freopen("test.txt", "r", stdin);

  int n;
  scanf("%d", &n);

  factors[0] = 1;
  for (int i = 1; i <= n + 1; i++) {
    factors[i] = factors[i - 1] * i;
    factors[i] %= MOD;
  }

  vector<LL> nums(n + 1, 0);
  LL cnt = 0;
  for (int i = 0; i <= n; i++) {
    scanf("%lld", &nums[i]);
    cnt += nums[i];
  }
  LL tmp = n;
  tmp = (tmp + 1) * tmp;
  LL dupitem = cnt - (tmp >> 1);
  vector<int> dupos;
  for (int i = 0; i <= n; i++) {
    if (nums[i] == dupitem)
      dupos.push_back(i);
    if (dupos.size() == 2)
      break;
  }
  int d = dupos[1] - dupos[0] - 1;
  for (int k = 1; k <= n + 1; k++) {
    cout << compute(n, k, d) << endl;
  }
}
Problem p03676 (C++)

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

typedef long long ll;
const ll mod = 1e9 + 7;
const int maxn = 2e5;
ll fact[maxn + 20], inv[maxn + 20];
ll power(ll b, ll e) {
  ll c = 1;
  while (e) {
    if (e & 1LL)
      c = (c * b) % mod;
    e /= 2LL;
    b = (b * b) % mod;
  }
  return c;
}

void pre() {
  ll i, j;
  fact[0] = inv[0] = 1;
  for (i = 1; i <= maxn; ++i) {
    fact[i] = (i * fact[i - 1]) % mod;
    inv[i] = power(fact[i], mod - 2);
  }
  return;
}

ll C(int n, int k) {
  if (k > n)
    return 0;
  ll ans = fact[n];
  ans = (ans * inv[k]) % mod;
  ans = (ans * inv[n - k]) % mod;
  return ans;
}

int cnt[maxn + 20];

int main() {
  int i, j, n;
  pre();
  freopen("in.txt", "r", stdin);
  cin >> n;
  int l, r;
  for (i = 1; i <= n + 1; ++i) {
    int x;
    cin >> x;
    if (cnt[x]) {
      l = cnt[x];
      r = i;
    } else
      cnt[x] = i;
  }
  ll ans, z = (n + 1 - r) + (l - 1);
  for (i = 1; i <= n + 1; ++i) {
    ans = C(n + 1, i);
    ans = (ans - C(z, i - 1) + mod) % mod;
    cout << ans << endl;
  }
  return 0;
}

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

typedef long long ll;
const ll mod = 1e9 + 7;
const int maxn = 2e5;
ll fact[maxn + 20], inv[maxn + 20];
ll power(ll b, ll e) {
  ll c = 1;
  while (e) {
    if (e & 1LL)
      c = (c * b) % mod;
    e /= 2LL;
    b = (b * b) % mod;
  }
  return c;
}

void pre() {
  ll i, j;
  fact[0] = inv[0] = 1;
  for (i = 1; i <= maxn; ++i) {
    fact[i] = (i * fact[i - 1]) % mod;
    inv[i] = power(fact[i], mod - 2);
  }
  return;
}

ll C(int n, int k) {
  if (k > n)
    return 0;
  ll ans = fact[n];
  ans = (ans * inv[k]) % mod;
  ans = (ans * inv[n - k]) % mod;
  return ans;
}

int cnt[maxn + 20];

int main() {
  int i, j, n;
  pre();
  // freopen("in.txt", "r", stdin);
  cin >> n;
  int l, r;
  for (i = 1; i <= n + 1; ++i) {
    int x;
    cin >> x;
    if (cnt[x]) {
      l = cnt[x];
      r = i;
    } else
      cnt[x] = i;
  }
  ll ans, z = (n + 1 - r) + (l - 1);
  for (i = 1; i <= n + 1; ++i) {
    ans = C(n + 1, i);
    ans = (ans - C(z, i - 1) + mod) % mod;
    cout << ans << endl;
  }
  return 0;
}
Problem p03677 (C++)

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

const int N = 1e5 + 7;
const int MOD = 1e9 + 7;
const double eps = 1e-6;
#define rep(aa, bb, cc) for (int aa = (bb), ee = (cc); aa < ee; aa++)
/***********************************************/
int n, m, a[N];
LL p[N];

int main() {
  LL all = 0;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; ++i)
    scanf("%d", a + i);
  for (int i = 1; i < n; ++i) {
    int l = a[i - 1], r = a[i];
    if (l > r)
      r += m;
    all += r - l;
    if (r - l > 1) {
      p[l + 2] += 1;
      p[r + 1] -= (r - (l + 2) + 1) + 1;
      p[r + 2] += (r - (l + 2) + 1);
    }
  }
  for (int i = 1; i <= 2 * m; ++i)
    p[i] += p[i - 1];
  for (int i = 1; i <= 2 * m; ++i)
    p[i] += p[i - 1];

  LL ans = -1;
  for (int i = 1; i <= m; ++i)
    ans = max(ans, p[i] + p[i + m]);
  cout << all - ans << endl;

  return 0;
}


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

const int N = 2e5 + 7;
const int MOD = 1e9 + 7;
const double eps = 1e-6;
#define rep(aa, bb, cc) for (int aa = (bb), ee = (cc); aa < ee; aa++)
/***********************************************/
int n, m, a[N];
LL p[N];

int main() {
  LL all = 0;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; ++i)
    scanf("%d", a + i);
  for (int i = 1; i < n; ++i) {
    int l = a[i - 1], r = a[i];
    if (l > r)
      r += m;
    all += r - l;
    if (r - l > 1) {
      p[l + 2] += 1;
      p[r + 1] -= (r - (l + 2) + 1) + 1;
      p[r + 2] += (r - (l + 2) + 1);
    }
  }
  for (int i = 1; i <= 2 * m; ++i)
    p[i] += p[i - 1];
  for (int i = 1; i <= 2 * m; ++i)
    p[i] += p[i - 1];

  LL ans = -1;
  for (int i = 1; i <= m; ++i)
    ans = max(ans, p[i] + p[i + m]);
  cout << all - ans << endl;

  return 0;
}
Problem p03677 (C++)

Buggy submission (status: Time Limit Exceeded):
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define pp pair<int, int>
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define ld long double
#define al(a) (a).begin(), (a).end()
#define mk make_pair
#define check cout << "?" << endl;

ll MOD = 1000000007;
ll mod = 998244353;
int inf = 1000001000;
ll INF = 1e18 + 5;

int main() {
  ll n, m, ans = 0;
  cin >> n >> m;
  vector<ll> a(n, 0), s(m, 0);
  priority_queue<pair<ll, ll>> que;
  rep(i, n) {
    cin >> a[i];
    a[i]--;
  }
  rep(i, n - 1) {
    ll from = a[i], to = a[i + 1];
    if (from > to)
      to += m;
    ans += to - from;
    if (to - from > 1) {
      que.push(mk(-(from + 2), -1));
      que.push(mk(-(to + 1), to - from - 1));
    }
  }
  ll cur = 0, cnt = 0, idx = 0, q;
  while (!(que.empty())) {
    while (!(que.empty()) && que.top().first == -idx) {
      tie(ignore, q) = que.top();
      que.pop();
      if (q == -1)
        cnt++;
      else {
        cur -= q;
        cnt--;
      }
    }
    cur += cnt;
    s[(2 * m - idx) % m] += cur;
    idx++;
  }
  ll res = ans;
  rep(i, m) res = min(res, ans - s[i]);
  cout << res << endl;
}

Fixed submission:
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define pp pair<int, int>
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define ld long double
#define al(a) (a).begin(), (a).end()
#define mk make_pair
#define check cout << "?" << endl;

ll MOD = 1000000007;
ll mod = 998244353;
int inf = 1000001000;
ll INF = 1e18 + 5;

int main() {
  ll n, m, ans = 0;
  cin >> n >> m;
  vector<ll> a(n, 0), s(m, 0);
  priority_queue<pair<ll, ll>> que;
  rep(i, n) {
    cin >> a[i];
    a[i]--;
  }
  rep(i, n - 1) {
    ll from = a[i], to = a[i + 1];
    if (from > to)
      to += m;
    ans += to - from;
    if (to - from > 1) {
      que.push(mk(-(from + 2), -1));
      que.push(mk(-(to + 1), to - from - 1));
    }
  }
  ll cur = 0, cnt = 0, idx = 0, q;
  while (!(que.empty())) {
    while (!(que.empty()) && que.top().first == -idx) {
      tie(ignore, q) = que.top();
      que.pop();
      if (q == -1)
        cnt++;
      else {
        cur -= q;
        cnt--;
      }
    }
    cur += cnt;
    s[(2 * m - idx) % m] += cur;
    idx++;
  }
  ll res = ans;
  rep(i, m) res = min(res, ans - s[i]);
  cout << res << endl;
}
Problem p03677 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#define maxn 100010
using namespace std;
typedef long long ll;
ll read() {
  ll x = 0, f = 1;
  char ch = getchar();
  while (ch - '0' < 0 || ch - '0' > 9) {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch - '0' >= 0 && ch - '0' <= 9) {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
int n;
ll m;
ll a[maxn];
ll f[maxn];
int main() {
  n = read();
  m = read();
  for (int i = 1; i <= n; i++)
    a[i] = read();
  ll ans = 0;
  for (int i = 1; i < n; i++) {
    ll l = a[i], r = a[i + 1];
    if (l > r)
      r += m;
    ans += r - l;
    l += 2;
    if (l > r)
      continue;
    f[l]++;
    f[r + 1] -= r - l + 2;
    f[r + 2] += r - l + 1;
  }
  for (int i = 1; i <= m * 2; i++)
    f[i] += f[i - 1];
  for (int i = 1; i <= m * 2; i++)
    f[i] += f[i - 1];
  for (int i = 1; i <= m; i++)
    f[i] += f[i + m];
  ll res = 0;
  for (int i = 1; i <= m; i++)
    res = max(res, f[i]);
  printf("%lld\n", ans - res);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#define maxn 200010
using namespace std;
typedef long long ll;
ll read() {
  ll x = 0, f = 1;
  char ch = getchar();
  while (ch - '0' < 0 || ch - '0' > 9) {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch - '0' >= 0 && ch - '0' <= 9) {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
int n;
ll m;
ll a[maxn];
ll f[maxn];
int main() {
  n = read();
  m = read();
  for (int i = 1; i <= n; i++)
    a[i] = read();
  ll ans = 0;
  for (int i = 1; i < n; i++) {
    ll l = a[i], r = a[i + 1];
    if (l > r)
      r += m;
    ans += r - l;
    l += 2;
    if (l > r)
      continue;
    f[l]++;
    f[r + 1] -= r - l + 2;
    f[r + 2] += r - l + 1;
  }
  for (int i = 1; i <= m * 2; i++)
    f[i] += f[i - 1];
  for (int i = 1; i <= m * 2; i++)
    f[i] += f[i - 1];
  for (int i = 1; i <= m; i++)
    f[i] += f[i + m];
  ll res = 0;
  for (int i = 1; i <= m; i++)
    res = max(res, f[i]);
  printf("%lld\n", ans - res);
  return 0;
}
Problem p03677 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;

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

  int n, m;
  cin >> n >> m;

  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  vector<vector<int64_t>> s(3, vector<int64_t>(m + 2));
  for (int i = 1; i < n; i++) {
    int d = a[i] - a[i - 1];

    if (d > 0) {
      s[1][1] += d;
      s[1][m + 1] -= d;
      s[2][a[i - 1] + 2] += -1;
      s[2][a[i] + 1] -= -1;
      s[1][a[i] + 1] -= -1 * (d - 1);

    } else {
      d += m;

      s[1][1] += a[i];
      s[1][a[i] + 1] -= a[i];
      s[2][2] += -1;
      s[2][a[i] + 1] -= -1;
      s[1][a[i] + 1] -= -1 * (a[i] - 1);

      s[1][a[i] + 1] += d;
      s[1][m + 1] -= d;
      s[2][a[i - 1] + 2] += -1;
      s[2][m + 1] -= -1;
      s[1][m + 1] -= -1 * (m - a[i - 1] - 1);
    }
  }

  for (int k = 1; k >= 0; k--) {
    int64_t c = 0;
    for (int j = 1; j <= m + 1; j++) {
      s[k][j] += (c += s[k + 1][j]);
    }
  }

  if (s[0][m + 1] != 0)
    throw;

  cout << *min_element(&s[0][1], &s[0][m + 1]) << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;

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

  int n, m;
  cin >> n >> m;

  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  vector<vector<int64_t>> s(3, vector<int64_t>(m + 2));
  for (int i = 1; i < n; i++) {
    int d = a[i] - a[i - 1];

    if (d > 0) {
      s[1][1] += d;
      s[1][m + 1] -= d;
      s[2][a[i - 1] + 2] += -1;
      s[2][a[i] + 1] -= -1;
      s[1][a[i] + 1] -= -1 * (d - 1);

    } else {
      d += m;

      s[1][1] += a[i];
      s[1][a[i] + 1] -= a[i];
      s[2][2] += -1;
      s[2][a[i] + 1] -= -1;
      s[1][a[i] + 1] -= -1 * (a[i] - 1);

      s[1][a[i] + 1] += d;
      s[1][m + 1] -= d;
      if (a[i - 1] <= m - 2) {
        s[2][a[i - 1] + 2] += -1;
        s[2][m + 1] -= -1;
        s[1][m + 1] -= -1 * (m - a[i - 1] - 1);
      }
    }
  }

  for (int k = 1; k >= 0; k--) {
    int64_t c = 0;
    for (int j = 1; j <= m + 1; j++) {
      s[k][j] += (c += s[k + 1][j]);
    }
  }

  if (s[0][m + 1] != 0)
    throw;

  cout << *min_element(&s[0][1], &s[0][m + 1]) << endl;

  return 0;
}
Problem p03677 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>

using namespace std;

const int MAXN = 100000 + 1000;

int n, m;
long long d[MAXN], ans = 0;

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

  cin >> n >> m;
  int x = 0;
  cin >> x;
  for (int i = 2; i <= n; i++) {
    int y, y0;
    cin >> y, y0 = y;
    if (y < x)
      y += m;
    ans += y - x;
    d[x + 2] += 1;
    d[y + 1] -= 1;
    d[y + 1] -= y - x - 1;
    d[y + 2] += y - x - 1;
    x = y0;
  }
  for (int i = 1; i <= 2 * m; i++)
    d[i] += d[i - 1];
  for (int i = 1; i <= 2 * m; i++)
    d[i] += d[i - 1];
  for (int i = 1; i <= m; i++)
    d[i] += d[i + m];
  // for(int i = 1; i <= m; i++)
  // 	cerr << d[i] << ' ';
  // cerr << endl;

  long long mx = 0;
  for (int i = 1; i <= m; i++)
    mx = max(mx, d[i]);
  ans -= mx;

  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>

using namespace std;

const int MAXN = 100000 + 1000;

int n, m;
long long d[2 * MAXN], ans = 0;

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

  cin >> n >> m;
  int x = 0;
  cin >> x;
  for (int i = 2; i <= n; i++) {
    int y, y0;
    cin >> y, y0 = y;
    if (y < x)
      y += m;
    ans += y - x;
    d[x + 2] += 1;
    d[y + 1] -= 1;
    d[y + 1] -= y - x - 1;
    d[y + 2] += y - x - 1;
    x = y0;
  }
  for (int i = 1; i <= 2 * m; i++)
    d[i] += d[i - 1];
  for (int i = 1; i <= 2 * m; i++)
    d[i] += d[i - 1];
  for (int i = 1; i <= m; i++)
    d[i] += d[i + m];
  // for(int i = 1; i <= m; i++)
  // 	cerr << d[i] << ' ';
  // cerr << endl;

  long long mx = 0;
  for (int i = 1; i <= m; i++)
    mx = max(mx, d[i]);
  ans -= mx;

  cout << ans << endl;

  return 0;
}