Problem p03961 (C++)

Buggy submission (status: Time Limit Exceeded):
const int N = 600001;
const int LG = 21;
const long long MOD = 1e9 + 7;
const long long INF = 1e9;
const long long INFLL = 1e18;

#include <algorithm>
#include <bitset>
#include <cmath>
#include <deque>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <time.h>
#include <unordered_set>
#include <vector>

using namespace std;

#define int long long
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<ll> vi;

#define forn(i, n) for (int(i) = 0; (i) != (n); (i)++)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define popcount(x) __builtin_popcount(x)
#define popcountll(x) __builtin_popcountll(x)
#define fi first
#define se second
#define re return
#define uniq(x)                                                                \
  sort(all(x));                                                                \
  (x).resize(unique(all(x)) - (x).begin())

#ifdef LOCAL
#define dbg(x) cerr << __LINE__ << " " << #x << " " << x << endl
#define ln cerr << __LINE__ << endl
#else
#define dbg(x) void(0)
#define ln void(0)
#endif // LOCAL

int cx[4] = {-1, 0, 1, 0};
int cy[4] = {0, -1, 0, 1};

ll inq(ll x, ll y) {
  if (!y)
    re 1 % MOD;
  ll l = inq(x, y / 2);
  if (y % 2)
    re l *l % MOD *x % MOD;
  re l *l % MOD;
}

ll rev(ll x) { return inq(x, MOD - 2); }

bool __precomputed_combinatorics = 0;
vector<ll> __fact, __ufact, __rev;

void __precompute_combinatorics() {
  __precomputed_combinatorics = 1;
  __fact.resize(N);
  __ufact.resize(N);
  __rev.resize(N);
  __rev[1] = 1;
  for (int i = 2; i < N; i++)
    __rev[i] = MOD - __rev[MOD % i] * (MOD / i) % MOD;
  __fact[0] = 1, __ufact[0] = 1;
  for (int i = 1; i < N; i++)
    __fact[i] = __fact[i - 1] * i % MOD,
    __ufact[i] = __ufact[i - 1] * __rev[i] % MOD;
}

ll fact(int x) {
  if (!__precomputed_combinatorics)
    __precompute_combinatorics();
  return __fact[x];
}

ll cnk(int n, int k) {
  if (!__precomputed_combinatorics)
    __precompute_combinatorics();
  return __fact[n] * __ufact[n - k] % MOD * __ufact[k] % MOD;
}

int n, p[N], fr[N], cu[N];
ll ans;

int fenw[N];
int fenw2[N];

int Get(int x) {
  int res = 0;
  while (x >= 0) {
    res += fenw[x];
    x = (x & (x + 1)) - 1;
  }
  return res;
}

void Add(int x) {
  while (x < N) {
    fenw[x]++;
    x = x | (x + 1);
  }
}

int Get2(int x) {
  int res = 0;
  while (x >= 0) {
    res += fenw2[x];
    x = (x & (x + 1)) - 1;
  }
  return res;
}

void Add2(int x) {
  while (x < N) {
    fenw2[x]++;
    x = x | (x + 1);
  }
}

signed main() {
  srand(time(NULL));
#ifdef ONLINE_JUDGE
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
#endif // ONLINE_JUDGE
  cin >> n;
  fill(fr, fr + n, 1);
  for (int i = 0; i < n; i++) {
    cin >> p[i];
    p[i]--;
    fr[p[i]] = 0;
  }
  for (int i = 0; i < n; i++) {
    if (fr[i])
      Add2(i);
  }
  cu[n - 1] = (fr[n - 1] == 1);
  for (int i = n - 2; i >= 0; i--) {
    cu[i] = (cu[i + 1] + (fr[i] == 1));
  }
  int k = 0;
  for (int i = 0; i < n; i++) {
    if (p[i] == -1)
      k++;
  }
  int cnt = 0;
  ll cusi = 0;
  ll si = 0;
  ll get2summ = 0;
  for (int i = 0; i < k; i++) {
    get2summ = (get2summ + i) % MOD;
  }
  for (int i = 0; i < n; i++) {
    if (fr[i] == 1)
      si += i;
  }
  for (int i = 0; i < n; i++) {
    if (p[i] != -1) {
      ll x = p[i];
      x = (x - Get(p[i] - 1) + MOD) % MOD;
      ans = (ans + x * fact(n - i - 1) % MOD * fact(k) % MOD) % MOD;
      ans = ((ans - fact(n - i - 1) * cnt % MOD * Get2(p[i] - 1) % MOD *
                        fact(k - 1)) %
                 MOD +
             MOD) %
            MOD;
      Add(p[i]);
      cusi += cu[p[i]];
    } else {
      ll s1 = ((si - cusi + MOD) % MOD + MOD) % MOD;
      ans = (ans + s1 * fact(n - i - 1) % MOD * fact(k - 1)) % MOD;
      ans =
          ((ans - fact(n - i - 1) * cnt % MOD * get2summ % MOD * fact(k - 2)) %
               MOD +
           MOD) %
          MOD;
      ll s2 = 0;
      for (int j = 0; j < n; j++) {
        if (fr[j] == 1) {
          ll x = j;
          x = (x - Get(j - 1) + MOD) % MOD;
          s2 += x;
        }
      }
      cnt++;
    }
  }
  // cout << ans << " " << fact(k) << endl;
  cout << (ans + fact(k)) % MOD;
}

/* Note:
Check constants at the beginning of the code.
N is set to 4e5 but be careful in problems with large constant factor.
Check corner cases.
No def int long long for now.
*/


Fixed submission:
const int N = 600001;
const int LG = 21;
const long long MOD = 1e9 + 7;
const long long INF = 1e9;
const long long INFLL = 1e18;

#include <algorithm>
#include <bitset>
#include <cmath>
#include <deque>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <time.h>
#include <unordered_set>
#include <vector>

using namespace std;

#define int long long
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<ll> vi;

#define forn(i, n) for (int(i) = 0; (i) != (n); (i)++)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define popcount(x) __builtin_popcount(x)
#define popcountll(x) __builtin_popcountll(x)
#define fi first
#define se second
#define re return
#define uniq(x)                                                                \
  sort(all(x));                                                                \
  (x).resize(unique(all(x)) - (x).begin())

#ifdef LOCAL
#define dbg(x) cerr << __LINE__ << " " << #x << " " << x << endl
#define ln cerr << __LINE__ << endl
#else
#define dbg(x) void(0)
#define ln void(0)
#endif // LOCAL

int cx[4] = {-1, 0, 1, 0};
int cy[4] = {0, -1, 0, 1};

ll inq(ll x, ll y) {
  if (!y)
    re 1 % MOD;
  ll l = inq(x, y / 2);
  if (y % 2)
    re l *l % MOD *x % MOD;
  re l *l % MOD;
}

ll rev(ll x) { return inq(x, MOD - 2); }

bool __precomputed_combinatorics = 0;
vector<ll> __fact, __ufact, __rev;

void __precompute_combinatorics() {
  __precomputed_combinatorics = 1;
  __fact.resize(N);
  __ufact.resize(N);
  __rev.resize(N);
  __rev[1] = 1;
  for (int i = 2; i < N; i++)
    __rev[i] = MOD - __rev[MOD % i] * (MOD / i) % MOD;
  __fact[0] = 1, __ufact[0] = 1;
  for (int i = 1; i < N; i++)
    __fact[i] = __fact[i - 1] * i % MOD,
    __ufact[i] = __ufact[i - 1] * __rev[i] % MOD;
}

ll fact(int x) {
  if (!__precomputed_combinatorics)
    __precompute_combinatorics();
  return __fact[x];
}

ll cnk(int n, int k) {
  if (!__precomputed_combinatorics)
    __precompute_combinatorics();
  return __fact[n] * __ufact[n - k] % MOD * __ufact[k] % MOD;
}

int n, p[N], fr[N], cu[N];
ll ans;

int fenw[N];
int fenw2[N];

int Get(int x) {
  int res = 0;
  while (x >= 0) {
    res += fenw[x];
    x = (x & (x + 1)) - 1;
  }
  return res;
}

void Add(int x) {
  while (x < N) {
    fenw[x]++;
    x = x | (x + 1);
  }
}

int Get2(int x) {
  int res = 0;
  while (x >= 0) {
    res += fenw2[x];
    x = (x & (x + 1)) - 1;
  }
  return res;
}

void Add2(int x) {
  while (x < N) {
    fenw2[x]++;
    x = x | (x + 1);
  }
}

signed main() {
  srand(time(NULL));
#ifdef ONLINE_JUDGE
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
#endif // ONLINE_JUDGE
  cin >> n;
  fill(fr, fr + n, 1);
  for (int i = 0; i < n; i++) {
    cin >> p[i];
    p[i]--;
    fr[p[i]] = 0;
  }
  for (int i = 0; i < n; i++) {
    if (fr[i])
      Add2(i);
  }
  cu[n - 1] = (fr[n - 1] == 1);
  for (int i = n - 2; i >= 0; i--) {
    cu[i] = (cu[i + 1] + (fr[i] == 1));
  }
  int k = 0;
  for (int i = 0; i < n; i++) {
    if (p[i] == -1)
      k++;
  }
  int cnt = 0;
  ll cusi = 0;
  ll si = 0;
  ll get2summ = 0;
  for (int i = 0; i < k; i++) {
    get2summ = (get2summ + i) % MOD;
  }
  for (int i = 0; i < n; i++) {
    if (fr[i] == 1)
      si += i;
  }
  for (int i = 0; i < n; i++) {
    if (p[i] != -1) {
      ll x = p[i];
      x = (x - Get(p[i] - 1) + MOD) % MOD;
      ans = (ans + x * fact(n - i - 1) % MOD * fact(k) % MOD) % MOD;
      ans = ((ans - fact(n - i - 1) * cnt % MOD * Get2(p[i] - 1) % MOD *
                        fact(k - 1)) %
                 MOD +
             MOD) %
            MOD;
      Add(p[i]);
      cusi += cu[p[i]];
    } else {
      ll s1 = ((si - cusi + MOD) % MOD + MOD) % MOD;
      ans = (ans + s1 * fact(n - i - 1) % MOD * fact(k - 1)) % MOD;
      ans =
          ((ans - fact(n - i - 1) * cnt % MOD * get2summ % MOD * fact(k - 2)) %
               MOD +
           MOD) %
          MOD;
      cnt++;
    }
  }
  // cout << ans << " " << fact(k) << endl;
  cout << (ans + fact(k)) % MOD;
}

/* Note:
Check constants at the beginning of the code.
N is set to 4e5 but be careful in problems with large constant factor.
Check corner cases.
No def int long long for now.
*/