Problem p03189 (C++)

Buggy submission (status: Runtime Error):
/* --- author: dxm --- */
#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;

#define INF 1e9
#define Inf 1e18
#define rep(i, n) for (int(i) = 0; (i) < n; i++)
#define REP(i, n) for (int(i) = 1; (i) <= n; i++)
#define mp make_pair
#define pb push_back
#define fst first
#define snd second
typedef long long ll;
typedef double db;
typedef long double ldb;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;

void qread(int &x) {
  int neg = 1;
  x = 0;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      neg = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9')
    x = 10 * x + c - '0', c = getchar();
  x *= neg;
}

const int maxn = 2005;
const int mod = 1e9 + 7;
const int inv = (mod + 1) / 2;
int n, q;
int a[maxn], dp[maxn][maxn];

int main() {
#ifdef FILE
  freopen("FILE.in", "r", stdin);
  freopen("FILE.out", "w", stdout);
#endif
  scanf("%d%d", &n, &q);
  REP(i, n) scanf("%d", &a[i]);
  REP(i, n) REP(j, n) if (a[i] > a[j]) dp[i][j] = 1;
  REP(i, q) {
    int x, y;
    scanf("%d%d", &x, &y);
    REP(j, n) {
      if (j == x || j == y)
        continue;
      int sum = 1LL * (dp[j][x] + dp[j][y]) * inv % mod;
      dp[j][x] = dp[j][y] = sum;
      sum = 1LL * (dp[x][j] + dp[y][j]) * inv % mod;
      dp[x][j] = dp[y][j] = sum;
    }
    int sum = 1LL * (dp[x][y] + dp[y][x]) * inv % mod;
    dp[x][y] = dp[y][x] = sum;
  }
  int ans = 0;
  REP(j, n) for (int k = j + 1; k <= n; k++) ans = (ans + dp[j][k]) % mod;
  REP(i, q) ans = 2LL * ans % mod;
  printf("%d\n", ans);
#ifdef TIME
  printf("Running Time = %d ms\n", int(clock() * 1000.0 / CLOCKS_PER_SEC));
#endif
  return 0;
}

/*
Input:
-----------------
Output:
*/


Fixed submission:
/* --- author: dxm --- */
#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;

#define INF 1e9
#define Inf 1e18
#define rep(i, n) for (int(i) = 0; (i) < n; i++)
#define REP(i, n) for (int(i) = 1; (i) <= n; i++)
#define mp make_pair
#define pb push_back
#define fst first
#define snd second
typedef long long ll;
typedef double db;
typedef long double ldb;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;

void qread(int &x) {
  int neg = 1;
  x = 0;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      neg = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9')
    x = 10 * x + c - '0', c = getchar();
  x *= neg;
}

const int maxn = 3005;
const int mod = 1e9 + 7;
const int inv = (mod + 1) / 2;
int n, q;
int a[maxn], dp[maxn][maxn];

int main() {
#ifdef FILE
  freopen("FILE.in", "r", stdin);
  freopen("FILE.out", "w", stdout);
#endif
  scanf("%d%d", &n, &q);
  REP(i, n) scanf("%d", &a[i]);
  REP(i, n) REP(j, n) if (a[i] > a[j]) dp[i][j] = 1;
  REP(i, q) {
    int x, y;
    scanf("%d%d", &x, &y);
    REP(j, n) {
      if (j == x || j == y)
        continue;
      int sum = 1LL * (dp[j][x] + dp[j][y]) * inv % mod;
      dp[j][x] = dp[j][y] = sum;
      sum = 1LL * (dp[x][j] + dp[y][j]) * inv % mod;
      dp[x][j] = dp[y][j] = sum;
    }
    int sum = 1LL * (dp[x][y] + dp[y][x]) * inv % mod;
    dp[x][y] = dp[y][x] = sum;
  }
  int ans = 0;
  REP(j, n) for (int k = j + 1; k <= n; k++) ans = (ans + dp[j][k]) % mod;
  REP(i, q) ans = 2LL * ans % mod;
  printf("%d\n", ans);
#ifdef TIME
  printf("Running Time = %d ms\n", int(clock() * 1000.0 / CLOCKS_PER_SEC));
#endif
  return 0;
}

/*
Input:
-----------------
Output:
*/