Problem p03676 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

#ifdef __APPLE__
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define eprintf(...)
#endif

const int N = (int)1e6 + 123;
const int MOD = (int)1e9 + 7;
const int inf = (int)5e8;
const long long infll = (long long)1e17;

int a[N];
int cnt[N];
int fact[N];

int add(int a, int b) {
  long long res = a * 1ll + b;
  return (res % MOD + MOD) % MOD;
}

int mul(int a, int b) { return (a * 1ll * b) % MOD; }

int powmod(int a, int n) {
  int res = 1;
  while (n > 0) {
    if (n & 1) {
      res = mul(res, a);
    }
    a = mul(a, a);
    n >>= 1;
  }
  return res;
}

void precalc() {
  fact[1] = fact[0] = 1;
  for (int i = 2; i < N - 1; ++i) {
    fact[i] = mul(fact[i - 1], i);
  }
}

int inv(int n) { return powmod(n, MOD - 2); }

int c(int k, int n) {
  if (k > n) {
    return 0;
  }
  return mul(fact[n], inv(mul(fact[n - k], fact[k])));
}

void solve() {
  int n;
  scanf("%d", &n);
  int vl = -1;
  for (int i = 0; i < n + 1; ++i) {
    scanf("%d", &a[i]);
    ++cnt[a[i]];
    if (cnt[a[i]] == 2) {
      vl = a[i];
    }
  }
  vector<int> pos;
  for (int i = 0; i < n + 1; ++i) {
    if (a[i] == vl) {
      pos.push_back(i);
    }
  }
  int left = pos[0];
  int right = n - pos[1];
  for (int i = 1; i <= n + 1; ++i) {
    int ans = c(i, n + 1);
    for (int j = 0; j <= min(left, i - 1); ++j) {
      ans = add(ans, -mul(c(j, left), c(i - 1 - j, right)));
    }
    printf("%d\n", ans);
  }
}

int main() {
#ifdef __APPLE__
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  precalc();
  int t = 1;
  while (t--) {
    solve();
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

#ifdef __APPLE__
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define eprintf(...)
#endif

const int N = (int)1e6 + 123;
const int MOD = (int)1e9 + 7;
const int inf = (int)5e8;
const long long infll = (long long)1e17;

int a[N];
int cnt[N];
int fact[N];

int add(int a, int b) {
  long long res = a * 1ll + b;
  return (res % MOD + MOD) % MOD;
}

int mul(int a, int b) { return (a * 1ll * b) % MOD; }

int powmod(int a, int n) {
  int res = 1;
  while (n > 0) {
    if (n & 1) {
      res = mul(res, a);
    }
    a = mul(a, a);
    n >>= 1;
  }
  return res;
}

void precalc() {
  fact[1] = fact[0] = 1;
  for (int i = 2; i < N - 1; ++i) {
    fact[i] = mul(fact[i - 1], i);
  }
}

int inv(int n) { return powmod(n, MOD - 2); }

int c(int k, int n) {
  if (k > n) {
    return 0;
  }
  return mul(fact[n], inv(mul(fact[n - k], fact[k])));
}

void solve() {
  int n;
  scanf("%d", &n);
  int vl = -1;
  for (int i = 0; i < n + 1; ++i) {
    scanf("%d", &a[i]);
    ++cnt[a[i]];
    if (cnt[a[i]] == 2) {
      vl = a[i];
    }
  }
  vector<int> pos;
  for (int i = 0; i < n + 1; ++i) {
    if (a[i] == vl) {
      pos.push_back(i);
    }
  }
  int left = pos[0];
  int right = n - pos[1];
  for (int i = 1; i <= n + 1; ++i) {
    int ans = c(i, n + 1);
    if (left + right >= i - 1) {
      ans = add(ans, -c(i - 1, left + right));
    }
    printf("%d\n", ans);
  }
}

int main() {
#ifdef __APPLE__
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  precalc();
  int t = 1;
  while (t--) {
    solve();
  }
  return 0;
}