Problem p03189 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <vector>
using namespace std;
#define pb push_back
#define mp make_pair
typedef pair<int, int> pii;
typedef long long ll;
typedef double ld;
typedef vector<int> vi;
#define fi first
#define se second
#define fe first
#define FO(x)                                                                  \
  {                                                                            \
    freopen(#x ".in", "r", stdin);                                             \
    freopen(#x ".out", "w", stdout);                                           \
  }
#define Edg                                                                    \
  int M = 0, fst[SZ], vb[SZ], nxt[SZ];                                         \
  void ad_de(int a, int b) {                                                   \
    ++M;                                                                       \
    nxt[M] = fst[a];                                                           \
    fst[a] = M;                                                                \
    vb[M] = b;                                                                 \
  }                                                                            \
  void adde(int a, int b) {                                                    \
    ad_de(a, b);                                                               \
    ad_de(b, a);                                                               \
  }
#define Edgc                                                                   \
  int M = 0, fst[SZ], vb[SZ], nxt[SZ], vc[SZ];                                 \
  void ad_de(int a, int b, int c) {                                            \
    ++M;                                                                       \
    nxt[M] = fst[a];                                                           \
    fst[a] = M;                                                                \
    vb[M] = b;                                                                 \
    vc[M] = c;                                                                 \
  }                                                                            \
  void adde(int a, int b, int c) {                                             \
    ad_de(a, b, c);                                                            \
    ad_de(b, a, c);                                                            \
  }
#define es(x, e) (int e = fst[x]; e; e = nxt[e])
#define esb(x, e, b) (int e = fst[x], b = vb[e]; e; e = nxt[e], b = vb[e])
#define SZ 666666
const int MOD = 1e9 + 7;
ll r2 = (MOD + 1) / 2;
int n, a[SZ], m, p[SZ], q[SZ], f[2333][2333];
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; ++i)
    cin >> a[i];
  for (int i = 1; i <= m; ++i)
    cin >> p[i] >> q[i];
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= n; ++j)
      if (i != j)
        f[i][j] = (a[i] > a[j]);
  ll ans = 0;
  for (int i = 1; i <= m; ++i) {
    int u = (f[p[i]][q[i]] + f[q[i]][p[i]]) % MOD * r2 % MOD;
    f[p[i]][q[i]] = f[q[i]][p[i]] = u;
    for (int t = 1; t <= n; ++t) {
      if (t != p[i] && t != q[i])
        ;
      else
        continue;
      int u = (f[t][p[i]] + f[t][q[i]]) % MOD * r2 % MOD;
      f[t][p[i]] = f[t][q[i]] = u;
      u = (f[p[i]][t] + f[q[i]][t]) % MOD * r2 % MOD;
      f[p[i]][t] = f[q[i]][t] = u;
    }
  }
  for (int i = 1; i <= n; ++i)
    for (int j = i + 1; j <= n; ++j)
      (ans += f[i][j]) %= MOD;
  for (int j = 1; j <= m; ++j)
    ans = ans * 2LL % MOD;
  ans = (ans % MOD + MOD) % MOD;
  cout << ans << "\n";
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <vector>
using namespace std;
#define pb push_back
#define mp make_pair
typedef pair<int, int> pii;
typedef long long ll;
typedef double ld;
typedef vector<int> vi;
#define fi first
#define se second
#define fe first
#define FO(x)                                                                  \
  {                                                                            \
    freopen(#x ".in", "r", stdin);                                             \
    freopen(#x ".out", "w", stdout);                                           \
  }
#define Edg                                                                    \
  int M = 0, fst[SZ], vb[SZ], nxt[SZ];                                         \
  void ad_de(int a, int b) {                                                   \
    ++M;                                                                       \
    nxt[M] = fst[a];                                                           \
    fst[a] = M;                                                                \
    vb[M] = b;                                                                 \
  }                                                                            \
  void adde(int a, int b) {                                                    \
    ad_de(a, b);                                                               \
    ad_de(b, a);                                                               \
  }
#define Edgc                                                                   \
  int M = 0, fst[SZ], vb[SZ], nxt[SZ], vc[SZ];                                 \
  void ad_de(int a, int b, int c) {                                            \
    ++M;                                                                       \
    nxt[M] = fst[a];                                                           \
    fst[a] = M;                                                                \
    vb[M] = b;                                                                 \
    vc[M] = c;                                                                 \
  }                                                                            \
  void adde(int a, int b, int c) {                                             \
    ad_de(a, b, c);                                                            \
    ad_de(b, a, c);                                                            \
  }
#define es(x, e) (int e = fst[x]; e; e = nxt[e])
#define esb(x, e, b) (int e = fst[x], b = vb[e]; e; e = nxt[e], b = vb[e])
#define SZ 666666
const int MOD = 1e9 + 7;
ll r2 = (MOD + 1) / 2;
int n, a[SZ], m, p[SZ], q[SZ], f[3333][3333];
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; ++i)
    cin >> a[i];
  for (int i = 1; i <= m; ++i)
    cin >> p[i] >> q[i];
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= n; ++j)
      if (i != j)
        f[i][j] = (a[i] > a[j]);
  ll ans = 0;
  for (int i = 1; i <= m; ++i) {
    int u = (f[p[i]][q[i]] + f[q[i]][p[i]]) % MOD * r2 % MOD;
    f[p[i]][q[i]] = f[q[i]][p[i]] = u;
    for (int t = 1; t <= n; ++t) {
      if (t != p[i] && t != q[i])
        ;
      else
        continue;
      int u = (f[t][p[i]] + f[t][q[i]]) % MOD * r2 % MOD;
      f[t][p[i]] = f[t][q[i]] = u;
      u = (f[p[i]][t] + f[q[i]][t]) % MOD * r2 % MOD;
      f[p[i]][t] = f[q[i]][t] = u;
    }
  }
  for (int i = 1; i <= n; ++i)
    for (int j = i + 1; j <= n; ++j)
      (ans += f[i][j]) %= MOD;
  for (int j = 1; j <= m; ++j)
    ans = ans * 2LL % MOD;
  ans = (ans % MOD + MOD) % MOD;
  cout << ans << "\n";
}