Problem p00266 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

#define REP(i, a, b) for (int i = a; i < (int)b; i++)
#define rep(i, n) REP(i, 0, n)
#define all(c) (c).begin(), (c).end()
#define zero(a) memset(a, 0, sizeof a)
#define minus(a) memset(a, -1, sizeof a)
#define watch(a)                                                               \\
  { cout << #a << " = " << a << endl; }
template <class T1, class T2> inline bool minimize(T1 &a, T2 b) {
  return b < a && (a = b, 1);
}
template <class T1, class T2> inline bool maximize(T1 &a, T2 b) {
  return a < b && (a = b, 1);
}

typedef long long ll;
int const inf = 1 << 29;

constexpr ll MOD = 1e9 + 7;

namespace tree {
template <class T> class BIT {
public:
  vector<T> dat;
  BIT() {}
  BIT(int N) { dat.assign(N, 0); }
  T sum(int i) { // sum [0, i)
    int ret = 0;
    for (--i; i >= 0; i = (i & (i + 1)) - 1)
      ret += dat[i];
    return ret;
  }
  T sum(int i, int j) { return sum(j) - sum(i); } // sum [i, j)
  void add(int i, T x) {
    for (; i < dat.size(); i |= i + 1)
      dat[i] += x;
  } // add x to i
};
} // namespace tree

ll fact[100001];

int main() {

  fact[0] = 1;
  REP(i, 1, 100001) { fact[i] = fact[i - 1] * i % MOD; }

  for (int N, R; cin >> N && N;) {
    cin >> R;
    tree::BIT<int> bit(N);
    vector<int> vs(N);

    rep(i, N) vs[i] = i, bit.add(i, 1);

    rep(i, R) {
      int s, t;
      cin >> s >> t;
      s--, t--;
      swap(vs[s], vs[t]);
    }

    ll ans = 0;

    rep(i, N) {
      ans += (ll)bit.sum(vs[i]) * fact[N - i - 1] % MOD;
      bit.add(vs[i], -1);
      ans %= MOD;
    }

    cout << ans << endl;
  }

  return 0;
}

Error: -6
munmap_chunk(): invalid pointer


Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tag_and_trait.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

using namespace std;

#define REP(i, a, b) for (int i = a; i < (int)b; i++)
#define rep(i, n) REP(i, 0, n)
#define all(c) (c).begin(), (c).end()
#define zero(a) memset(a, 0, sizeof a)
#define minus(a) memset(a, -1, sizeof a)
#define watch(a)                                                               \\
  { cout << #a << " = " << a << endl; }
template <class T1, class T2> inline bool minimize(T1 &a, T2 b) {
  return b < a && (a = b, 1);
}
template <class T1, class T2> inline bool maximize(T1 &a, T2 b) {
  return a < b && (a = b, 1);
}

typedef long long ll;
int const inf = 1 << 29;

constexpr int MOD = 1e9 + 7;

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    set_gccext;
set_gccext st;

int main() {

  static ll fact[100001];
  fact[0] = 1;
  REP(i, 1, 100001) { fact[i] = fact[i - 1] * i % MOD; }

  for (int N, R; cin >> N && N;) {
    cin >> R;
    vector<ll> vs(N);
    st.clear();

    REP(i, 1, N + 1) vs[i] = i, st.insert(i);

    do {
      rep(i, N) cout << vs[i] << " ";
      cout << endl;
    } while (next_permutation(all(vs)));

    rep(i, R) {
      int s, t;
      cin >> s >> t;
      s--, t--;
      swap(vs[s], vs[t]);
    }

    //    rep(i, N) {     cout << vs[i] << " ";   }cout << endl;

    ll ans = 0;

    rep(i, N) {
      ans += (st.order_of_key(vs[i]) - 1) * fact[N - i - 1];
      st.erase(vs[i]);
      ans %= MOD;
    }

    cout << ans - 1 << endl;
  }

  return 0;
}