Problem p03961 (C++)

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

const int64_t MOD = 1e9 + 7;
void add(int64_t &a, int64_t b) { a = (a + b) % MOD; }
void mul(int64_t &a, int64_t b) { a = a * b % MOD; }

vector<int64_t> fact, seq_inv, fact_inv;

void create_fact_mod(int num) {
  fact[0] = fact[1] = 1;
  for (int i = 2; i <= num; i++)
    fact[i] = fact[i - 1] * i % MOD;
}

void create_seq_inv_mod(int num) {
  seq_inv[0] = seq_inv[1] = 1;
  for (int i = 2; i <= num; i++)
    seq_inv[i] = (MOD - MOD / i) * seq_inv[MOD % i] % MOD;
}

void create_fact_inv_mod(int num) {
  fact_inv[0] = fact_inv[1] = 1;
  for (int i = 2; i <= num; i++)
    fact_inv[i] = fact_inv[i - 1] * seq_inv[i] % MOD;
}

void create_mod_tables(int num) {
  fact.resize(num + 1);
  seq_inv.resize(num + 1);
  fact_inv.resize(num + 1);
  create_fact_mod(num);
  create_seq_inv_mod(num);
  create_fact_inv_mod(num);
}

int64_t comb_mod(int n, int k) {
  return fact[n] * fact_inv[n - k] % MOD * fact_inv[k] % MOD;
}

int64_t perm_mod(int n, int k) { return fact[n] * fact_inv[n - k] % MOD; }

int64_t power_mod(int64_t num, int64_t power) {
  int64_t prod = 1;
  num %= MOD;
  while (power > 0) {
    if (power & 1)
      prod = prod * num % MOD;
    num = num * num % MOD;
    power >>= 1;
  }
  return prod;
}

int64_t extgcd(int64_t a, int64_t b, int64_t &x, int64_t &y) {
  int64_t d = a;
  if (b != 0) {
    d = extgcd(b, a % b, y, x);
    y -= (a / b) * x;
  } else {
    x = 1;
    y = 0;
  }
  return d;
}

int64_t inv_mod(int64_t a) {
  int64_t x, y;
  extgcd(a, MOD, x, y);
  return (MOD + x % MOD) % MOD;
}

template <typename T> struct BIT {
  int n;
  vector<T> dat;

  BIT(int n = 0) { initialize(n); }

  void initialize(int nin) {
    n = nin;
    dat.resize(n, 0);
  }

  T sum(int i) {
    T s = 0;
    while (i >= 0) {
      s += dat[i];
      i = (i & (i + 1)) - 1;
    }
    return s;
  }

  T sum_between(int i, int j) { return sum(j) - sum(i - 1); }

  void plus(int i, T x) {
    while (i < n) {
      dat[i] += x;
      i |= i + 1;
    }
  }

  // a[0]+...+a[ret] >= x
  int lower_bound(T x) {
    int ret = -1;
    int k = 1;
    while (2 * k <= n)
      k <<= 1;
    for (; k > 0; k >>= 1) {
      if (ret + k < n && dat[ret + k] < x) {
        x -= dat[ret + k];
        ret += k;
      }
    }
    return ret + 1;
  }
};

int main() {
  int N;
  cin >> N;
  vector<int> P(N);
  bitset<100000> exist;
  for (int i = 0; i < N; i++) {
    cin >> P[i];
    P[i]--;
    if (P[i] >= 0)
      exist[P[i]] = 1;
  }
  vector<int64_t> bs;
  for (int i = 0; i < N; i++)
    if (!exist[i])
      bs.push_back(i);

  BIT<int64_t> bitA(N), bitB(N);
  for (int b : bs)
    bitB.plus(b, 1);

  int64_t meanB = 0, z = bs.size(), iz = 0, i2 = inv_mod(2);
  if (z) {
    iz = inv_mod(z);
    meanB = accumulate(bs.begin(), bs.end(), 0LL) % MOD * iz % MOD;
  }

  vector<int64_t> num(N);
  int64_t k = 0, s = 0;
  for (int i = 0; i < N; i++) {
    if (P[i] >= 0) {
      num[i] = P[i];
      int64_t sub = k * bitB.sum(P[i]) % MOD * iz % MOD;
      add(sub, bitA.sum(P[i]));
      add(num[i], MOD - sub);
      add(s, bitB.sum_between(P[i], N - 1));
      bitA.plus(P[i], 1);
    } else {
      num[i] = meanB;
      int64_t sub = s * iz % MOD;
      add(sub, k * i2);
      add(num[i], MOD - sub);
      k++;
    }
  }

  int64_t ans = 1;
  int64_t base = 1;
  for (int i = N - 1; i >= 0; i--) {
    add(ans, num[i] * base);
    mul(base, N - i);
  }
  for (int i = 1; i <= z; i++)
    mul(ans, i);
  cout << ans << endl;
}


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

const int64_t MOD = 1e9 + 7;
void add(int64_t &a, int64_t b) { a = (a + b) % MOD; }
void mul(int64_t &a, int64_t b) { a = a * b % MOD; }

vector<int64_t> fact, seq_inv, fact_inv;

void create_fact_mod(int num) {
  fact[0] = fact[1] = 1;
  for (int i = 2; i <= num; i++)
    fact[i] = fact[i - 1] * i % MOD;
}

void create_seq_inv_mod(int num) {
  seq_inv[0] = seq_inv[1] = 1;
  for (int i = 2; i <= num; i++)
    seq_inv[i] = (MOD - MOD / i) * seq_inv[MOD % i] % MOD;
}

void create_fact_inv_mod(int num) {
  fact_inv[0] = fact_inv[1] = 1;
  for (int i = 2; i <= num; i++)
    fact_inv[i] = fact_inv[i - 1] * seq_inv[i] % MOD;
}

void create_mod_tables(int num) {
  fact.resize(num + 1);
  seq_inv.resize(num + 1);
  fact_inv.resize(num + 1);
  create_fact_mod(num);
  create_seq_inv_mod(num);
  create_fact_inv_mod(num);
}

int64_t comb_mod(int n, int k) {
  return fact[n] * fact_inv[n - k] % MOD * fact_inv[k] % MOD;
}

int64_t perm_mod(int n, int k) { return fact[n] * fact_inv[n - k] % MOD; }

int64_t power_mod(int64_t num, int64_t power) {
  int64_t prod = 1;
  num %= MOD;
  while (power > 0) {
    if (power & 1)
      prod = prod * num % MOD;
    num = num * num % MOD;
    power >>= 1;
  }
  return prod;
}

int64_t extgcd(int64_t a, int64_t b, int64_t &x, int64_t &y) {
  int64_t d = a;
  if (b != 0) {
    d = extgcd(b, a % b, y, x);
    y -= (a / b) * x;
  } else {
    x = 1;
    y = 0;
  }
  return d;
}

int64_t inv_mod(int64_t a) {
  int64_t x, y;
  extgcd(a, MOD, x, y);
  return (MOD + x % MOD) % MOD;
}

template <typename T> struct BIT {
  int n;
  vector<T> dat;

  BIT(int n = 0) { initialize(n); }

  void initialize(int nin) {
    n = nin;
    dat.resize(n, 0);
  }

  T sum(int i) {
    T s = 0;
    while (i >= 0) {
      s += dat[i];
      i = (i & (i + 1)) - 1;
    }
    return s;
  }

  T sum_between(int i, int j) { return sum(j) - sum(i - 1); }

  void plus(int i, T x) {
    while (i < n) {
      dat[i] += x;
      i |= i + 1;
    }
  }

  // a[0]+...+a[ret] >= x
  int lower_bound(T x) {
    int ret = -1;
    int k = 1;
    while (2 * k <= n)
      k <<= 1;
    for (; k > 0; k >>= 1) {
      if (ret + k < n && dat[ret + k] < x) {
        x -= dat[ret + k];
        ret += k;
      }
    }
    return ret + 1;
  }
};

int main() {
  int N;
  cin >> N;
  vector<int> P(N);
  bitset<500000> exist;
  for (int i = 0; i < N; i++) {
    cin >> P[i];
    P[i]--;
    if (P[i] >= 0)
      exist[P[i]] = 1;
  }
  vector<int64_t> bs;
  for (int i = 0; i < N; i++)
    if (!exist[i])
      bs.push_back(i);

  BIT<int64_t> bitA(N), bitB(N);
  for (int b : bs)
    bitB.plus(b, 1);

  int64_t meanB = 0, z = bs.size(), iz = 0, i2 = inv_mod(2);
  if (z) {
    iz = inv_mod(z);
    meanB = accumulate(bs.begin(), bs.end(), 0LL) % MOD * iz % MOD;
  }

  vector<int64_t> num(N);
  int64_t k = 0, s = 0;
  for (int i = 0; i < N; i++) {
    if (P[i] >= 0) {
      num[i] = P[i];
      int64_t sub = k * bitB.sum(P[i]) % MOD * iz % MOD;
      add(sub, bitA.sum(P[i]));
      add(num[i], MOD - sub);
      add(s, bitB.sum_between(P[i], N - 1));
      bitA.plus(P[i], 1);
    } else {
      num[i] = meanB;
      int64_t sub = s * iz % MOD;
      add(sub, k * i2);
      add(num[i], MOD - sub);
      k++;
    }
  }

  int64_t ans = 1;
  int64_t base = 1;
  for (int i = N - 1; i >= 0; i--) {
    add(ans, num[i] * base);
    mul(base, N - i);
  }
  for (int i = 1; i <= z; i++)
    mul(ans, i);
  cout << ans << endl;
}