Problem p03674 (C++)

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

using namespace std;
using namespace __gnu_pbds;

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define fbo find_by_order
#define ook order_of_key

typedef long long ll;
typedef pair<ll, ll> ii;
typedef vector<int> vi;
typedef long double ld;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    pbds;

const long long mod = 1000000007;
const long long inf = 1e18;

int a[100001];
ii arr[200003];

ll power(ll a, ll b) { // to calculate a^b %mod
  if (b == 0)
    return 1;
  ll temp = power(a, b >> 1) % mod;
  temp = (temp * temp) % mod;
  if (b & 1)
    temp = temp * a % mod;
  return temp;
}

void buildtable(void) {
  ll count = 1;
  for (ll i = 1; i <= 200002; i++) {
    count *= i;
    if (count > mod) {
      count = count % mod;
    }
    arr[i].fi = count;
    ll invmod = power(count, mod - 2);
    arr[i].se = invmod;
  }
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  vi v;
  int l, r;
  for (int i = 1; i <= n + 1; i++) {
    int k;
    cin >> k;
    if (a[k] == 0) {
      a[k] = i;
    } else {
      l = a[k];
      r = i;
    }
    v.pb(k);
  }
  arr[0].fi = 1;
  arr[0].se = 1;
  buildtable();

  for (int i = 1; i <= n + 1; i++) {
    ll c1 = arr[n + 1].fi * arr[i].se;
    c1 = c1 % mod;
    c1 = c1 * arr[n + 1 - i].se;
    c1 = c1 % mod;

    ll c2 = arr[l + n - r].fi * arr[i - 1].se;
    c2 = c2 % mod;
    c2 = c2 * arr[l + n - r - i + 1].se;
    c2 = c2 % mod;

    if (c1 - c2 > 0) {
      cout << c1 - c2 << endl;
    } else {
      cout << c1 - c2 + mod << endl;
    }
  }

  return 0;
}


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

using namespace std;
using namespace __gnu_pbds;

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define fbo find_by_order
#define ook order_of_key

typedef long long ll;
typedef pair<ll, ll> ii;
typedef vector<int> vi;
typedef long double ld;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    pbds;

const long long mod = 1000000007;
const long long inf = 1e18;

int a[100001];
ii arr[200003];

ll power(ll a, ll b) { // to calculate a^b %mod
  if (b == 0)
    return 1;
  ll temp = power(a, b >> 1) % mod;
  temp = (temp * temp) % mod;
  if (b & 1)
    temp = temp * a % mod;
  return temp;
}

void buildtable(void) {
  ll count = 1;
  for (ll i = 1; i <= 200002; i++) {
    count *= i;
    if (count > mod) {
      count = count % mod;
    }
    arr[i].fi = count;
    ll invmod = power(count, mod - 2);
    arr[i].se = invmod;
  }
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  vi v;
  int l, r;
  for (int i = 1; i <= n + 1; i++) {
    int k;
    cin >> k;
    if (a[k] == 0) {
      a[k] = i;
    } else {
      l = a[k];
      r = i;
    }
    v.pb(k);
  }
  arr[0].fi = 1;
  arr[0].se = 1;
  buildtable();

  for (int i = 1; i <= n + 1; i++) {
    ll c1 = arr[n + 1].fi * arr[i].se;
    c1 = c1 % mod;
    c1 = c1 * arr[n + 1 - i].se;
    c1 = c1 % mod;

    ll c2 = 0;
    if (l + n - r - i + 1 >= 0) {
      c2 = arr[l + n - r].fi * arr[i - 1].se;
      c2 = c2 % mod;
      c2 = c2 * arr[l + n - r - i + 1].se;
      c2 = c2 % mod;
    }

    if (c1 - c2 > 0) {
      cout << c1 - c2 << endl;
    } else {
      cout << c1 - c2 + mod << endl;
    }
  }

  return 0;
}