Problem p03189 (C++)

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

const LL MOD = 1000000007;
LL powmod(LL a, LL n) {
  if (n == 0)
    return 1;
  if (n % 2)
    return (a * powmod(a, n - 1)) % MOD;
  LL c = powmod(a, n / 2);
  return (c * c) % MOD;
}
LL inv(LL a) { return powmod(a, MOD - 2); }
LL fact[110000];
LL invfact[110000];

LL ncr(LL n, LL r) {
  if (r < 0 || n < 0)
    return 0;
  if (n < r)
    return 0;
  LL a = fact[n];
  a = (a * invfact[r]) % MOD;
  a = (a * invfact[n - r]) % MOD;
  return a;
}
LL inv2;
void init() {
  fact[0] = 1;
  invfact[0] = 1;
  inv2 = inv(2);
  for (LL i = 1; i < 110000; i++) {
    fact[i] = (i * fact[i - 1]) % MOD;
    invfact[i] = inv(fact[i]);
  }
}

LL prob[1000][1000]; // prob i > j
LL temp1[1000][1000];
LL temp2[1000][1000];
int c, d;
int f(int a) {
  if (a == c)
    return d;
  if (a == d)
    return c;
  return a;
}

int main() {
  init();
  int n, m;
  cin >> n >> m;
  int a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i]--;
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (a[i] > a[j]) {
        prob[i][j] = 1;
      } else {
        prob[i][j] = 0;
      }
    }
  }

  for (int i = 0; i < m; i++) {
    cin >> c >> d;
    c--;
    d--;
    vector<int> r, s;
    for (int j = 0; j < n; j++) {
      r.push_back(j);
      s.push_back(c);
      r.push_back(j);
      s.push_back(d);
      r.push_back(c);
      s.push_back(j);
      r.push_back(d);
      s.push_back(j);
    }
    for (int j = 0; j < r.size(); j++) {
      int u = r[j];
      int v = s[j];
      temp1[u][v] = prob[u][v];
      temp2[u][v] = prob[f(u)][f(v)];
    }
    for (int j = 0; j < r.size(); j++) {
      int u = r[j];
      int v = s[j];
      prob[u][v] = (temp1[u][v] + temp2[u][v]) * inv2;
      prob[u][v] %= MOD;
    }
  }
  LL ans = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (i < j) {
        ans += prob[i][j];
        ans %= MOD;
      }
    }
  }
  ans = ans * powmod(2, m);
  ans %= MOD;
  if (ans < 0)
    ans += MOD;
  cout << ans << endl;
}

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

const LL MOD = 1000000007;
LL powmod(LL a, LL n) {
  if (n == 0)
    return 1;
  if (n % 2)
    return (a * powmod(a, n - 1)) % MOD;
  LL c = powmod(a, n / 2);
  return (c * c) % MOD;
}
LL inv(LL a) { return powmod(a, MOD - 2); }
LL fact[110000];
LL invfact[110000];

LL ncr(LL n, LL r) {
  if (r < 0 || n < 0)
    return 0;
  if (n < r)
    return 0;
  LL a = fact[n];
  a = (a * invfact[r]) % MOD;
  a = (a * invfact[n - r]) % MOD;
  return a;
}
LL inv2;
void init() {
  fact[0] = 1;
  invfact[0] = 1;
  inv2 = inv(2);
  for (LL i = 1; i < 110000; i++) {
    fact[i] = (i * fact[i - 1]) % MOD;
    invfact[i] = inv(fact[i]);
  }
}

LL prob[3100][3100]; // prob i > j
LL temp1[3100][3100];
LL temp2[3100][3100];
int c, d;
int f(int a) {
  if (a == c)
    return d;
  if (a == d)
    return c;
  return a;
}

int main() {
  init();
  int n, m;
  cin >> n >> m;
  int a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i]--;
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (a[i] > a[j]) {
        prob[i][j] = 1;
      } else {
        prob[i][j] = 0;
      }
    }
  }

  for (int i = 0; i < m; i++) {
    cin >> c >> d;
    c--;
    d--;
    vector<int> r, s;
    for (int j = 0; j < n; j++) {
      r.push_back(j);
      s.push_back(c);
      r.push_back(j);
      s.push_back(d);
      r.push_back(c);
      s.push_back(j);
      r.push_back(d);
      s.push_back(j);
    }
    for (int j = 0; j < r.size(); j++) {
      int u = r[j];
      int v = s[j];
      temp1[u][v] = prob[u][v];
      temp2[u][v] = prob[f(u)][f(v)];
    }
    for (int j = 0; j < r.size(); j++) {
      int u = r[j];
      int v = s[j];
      prob[u][v] = (temp1[u][v] + temp2[u][v]) * inv2;
      prob[u][v] %= MOD;
    }
  }
  LL ans = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (i < j) {
        ans += prob[i][j];
        ans %= MOD;
      }
    }
  }
  ans = ans * powmod(2, m);
  ans %= MOD;
  if (ans < 0)
    ans += MOD;
  cout << ans << endl;
}