Problem p03189 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <utility>
#define MAX_N 105

using namespace std;
typedef long long ll;

const ll mod = 1000000007;

ll mpow(ll x, ll n) {
  ll ret = 1;
  while (n > 0) {
    if ((n & 1) == 1) {
      ret *= x;
      ret %= mod;
    }
    x = (x * x) % mod;
    n >>= 1;
  }
  return ret % mod;
}

ll inv(ll x) { return mpow(x, mod - 2); }

int main() {
  int N, Q;
  ll A[MAX_N];
  ll dp[2][MAX_N][MAX_N];
  cin >> N >> Q;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  ll _2q = mpow(2, Q);
  ll inv2 = inv(2);
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      if (A[i] > A[j]) {
        dp[0][i][j] = dp[1][i][j] = 1;
      } else {
        dp[0][i][j] = dp[1][i][j] = 0;
      }
    }
  }
  for (int qqq = 0; qqq < Q; qqq++) {
    int x, y;
    cin >> x >> y;
    x--, y--;
    for (int i = 0; i < N; i++) {
      if (i != x && i != y) {
        dp[1][x][i] = (dp[0][x][i] * inv2 + dp[0][y][i] * inv2) % mod;
        dp[1][i][x] = (dp[0][i][x] * inv2 + dp[0][i][y] * inv2) % mod;
        dp[1][y][i] = (dp[0][y][i] * inv2 + dp[0][x][i] * inv2) % mod;
        dp[1][i][y] = (dp[0][i][y] * inv2 + dp[0][i][x] * inv2) % mod;
      }
    }
    dp[1][x][y] = dp[1][y][x] = (dp[0][x][y] * inv2 + dp[0][y][x] * inv2) % mod;
    for (int i = 0; i < N; i++) {
      dp[0][i][x] = dp[1][i][x];
      dp[0][i][y] = dp[1][i][y];
      dp[0][x][i] = dp[1][x][i];
      dp[0][y][i] = dp[1][y][i];
    }
  }
  ll ans = 0;

  for (int i = 0; i < N; i++) {
    for (int j = i + 1; j < N; j++) {
      ans += dp[0][i][j];
      ans %= mod;
      //            cout<<(_2q*dp[0][i][j])%mod<<" ";
    }
    //        cout<<endl;
  }
  cout << (ans * _2q) % mod << endl;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <utility>
#define MAX_N 3005

using namespace std;
typedef long long ll;

const ll mod = 1000000007;

ll mpow(ll x, ll n) {
  ll ret = 1;
  while (n > 0) {
    if ((n & 1) == 1) {
      ret *= x;
      ret %= mod;
    }
    x = (x * x) % mod;
    n >>= 1;
  }
  return ret % mod;
}

ll inv(ll x) { return mpow(x, mod - 2); }

int main() {
  int N, Q;
  ll A[MAX_N];
  ll dp[2][MAX_N][MAX_N];
  cin >> N >> Q;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  ll _2q = mpow(2, Q);
  ll inv2 = inv(2);
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      if (A[i] > A[j]) {
        dp[0][i][j] = dp[1][i][j] = 1;
      } else {
        dp[0][i][j] = dp[1][i][j] = 0;
      }
    }
  }
  for (int qqq = 0; qqq < Q; qqq++) {
    int x, y;
    cin >> x >> y;
    x--, y--;
    for (int i = 0; i < N; i++) {
      if (i != x && i != y) {
        dp[1][x][i] = (dp[0][x][i] * inv2 + dp[0][y][i] * inv2) % mod;
        dp[1][i][x] = (dp[0][i][x] * inv2 + dp[0][i][y] * inv2) % mod;
        dp[1][y][i] = (dp[0][y][i] * inv2 + dp[0][x][i] * inv2) % mod;
        dp[1][i][y] = (dp[0][i][y] * inv2 + dp[0][i][x] * inv2) % mod;
      }
    }
    dp[1][x][y] = dp[1][y][x] = (dp[0][x][y] * inv2 + dp[0][y][x] * inv2) % mod;
    for (int i = 0; i < N; i++) {
      dp[0][i][x] = dp[1][i][x];
      dp[0][i][y] = dp[1][i][y];
      dp[0][x][i] = dp[1][x][i];
      dp[0][y][i] = dp[1][y][i];
    }
  }
  ll ans = 0;

  for (int i = 0; i < N; i++) {
    for (int j = i + 1; j < N; j++) {
      ans += dp[0][i][j];
      ans %= mod;
      //            cout<<(_2q*dp[0][i][j])%mod<<" ";
    }
    //        cout<<endl;
  }
  cout << (ans * _2q) % mod << endl;
}