Problem p03189 (C++)

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

using i64 = int64_t;

#define rep(i, x, y)                                                           \
  for (i64 i = i64(x), i##_max_for_repmacro = i64(y);                          \
       i < i##_max_for_repmacro; ++i)
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define print(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define print(x)
#endif

const int inf = 1.01e9;
const i64 inf64 = 4.01e18;
const double eps = 1e-9;

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}

void solve() {
  const i64 mod = 1'000'000'007;
  i64 N, Q;
  cin >> N >> Q;

  if (max(Q * N * N, N * N * N) >= 300000000) {
    assert(false);
  }

  vector<i64> A(N);
  rep(i, 0, N) { cin >> A[i]; }

  vector<i64> X(Q), Y(Q);
  rep(i, 0, Q) {
    cin >> X[i] >> Y[i];
    --X[i];
    --Y[i];
  }

  const i64 size = 3000;
  static i64 pow2[size + 1];
  pow2[0] = 1;
  rep(i, 1, size + 1) { pow2[i] = pow2[i - 1] * 2 % mod; }
  static i64 dp[size][size];
  static i64 last[size][size];
  fill_n((i64 *)dp, size * size, 0);
  fill_n((i64 *)last, size * size, -1);
  rep(i, 0, N) {
    rep(j, 0, N) {
      if (A[j] <= A[i])
        continue;
      ++dp[j][i];
    }
  }

  rep(j, 0, Q) {
    i64 x = X[j], y = Y[j];

    auto evaluate = [&](i64 k, i64 l) {
      if (last[k][l] < j) {
        dp[k][l] *= pow2[j - last[k][l] - 1];
        dp[k][l] %= mod;
      }
      last[k][l] = j;
    };

    vector<i64> xk(N), kx(N), yk(N), ky(N);
    rep(k, 0, N) {
      evaluate(x, k);
      xk[k] = dp[x][k];

      evaluate(k, x);
      kx[k] = dp[k][x];

      evaluate(y, k);
      yk[k] = dp[y][k];

      evaluate(k, y);
      ky[k] = dp[k][y];
    }
    rep(k, 0, N) {
      if (k == x)
        continue;
      dp[x][k] += yk[k];
      dp[x][k] %= mod;

      dp[k][x] += ky[k];
      dp[k][x] %= mod;
    }

    rep(k, 0, N) {
      if (k == y)
        continue;
      dp[y][k] += xk[k];
      dp[y][k] %= mod;

      dp[k][y] += kx[k];
      dp[k][y] %= mod;
    }

    dp[y][x] += xk[y];
    dp[y][x] %= mod;

    dp[x][y] += yk[x];
    dp[x][y] %= mod;
  }

  i64 ans = 0;
  rep(j, 0, N) {
    rep(k, j + 1, N) {
      dp[j][k] *= pow2[Q - last[j][k] - 1];
      dp[j][k] %= mod;
      ans += dp[j][k];
      ans %= mod;
    }
  }

  cout << ans << endl;
}

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  cout.setf(ios::fixed);
  cout.precision(16);
  solve();
  return 0;
}


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

using i64 = int64_t;

#define rep(i, x, y)                                                           \
  for (i64 i = i64(x), i##_max_for_repmacro = i64(y);                          \
       i < i##_max_for_repmacro; ++i)
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define print(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define print(x)
#endif

const int inf = 1.01e9;
const i64 inf64 = 4.01e18;
const double eps = 1e-9;

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}

void solve() {
  const i64 mod = 1'000'000'007;
  i64 N, Q;
  cin >> N >> Q;

  vector<i64> A(N);
  rep(i, 0, N) { cin >> A[i]; }

  vector<i64> X(Q), Y(Q);
  rep(i, 0, Q) {
    cin >> X[i] >> Y[i];
    --X[i];
    --Y[i];
  }

  const i64 size = 3000;
  static i64 pow2[size + 1];
  pow2[0] = 1;
  rep(i, 1, size + 1) { pow2[i] = pow2[i - 1] * 2 % mod; }
  static i64 dp[size][size];
  static i64 last[size][size];
  fill_n((i64 *)dp, size * size, 0);
  fill_n((i64 *)last, size * size, -1);
  rep(i, 0, N) {
    rep(j, 0, N) {
      if (A[j] <= A[i])
        continue;
      ++dp[j][i];
    }
  }

  rep(j, 0, Q) {
    i64 x = X[j], y = Y[j];

    auto evaluate = [&](i64 k, i64 l) {
      if (last[k][l] < j) {
        dp[k][l] *= pow2[j - last[k][l] - 1];
        dp[k][l] %= mod;
      }
      last[k][l] = j;
    };

    vector<i64> xk(N), kx(N), yk(N), ky(N);
    rep(k, 0, N) {
      evaluate(x, k);
      xk[k] = dp[x][k];

      evaluate(k, x);
      kx[k] = dp[k][x];

      evaluate(y, k);
      yk[k] = dp[y][k];

      evaluate(k, y);
      ky[k] = dp[k][y];
    }
    rep(k, 0, N) {
      if (k == x)
        continue;
      dp[x][k] += yk[k];
      dp[x][k] %= mod;

      dp[k][x] += ky[k];
      dp[k][x] %= mod;
    }

    rep(k, 0, N) {
      if (k == y)
        continue;
      dp[y][k] += xk[k];
      dp[y][k] %= mod;

      dp[k][y] += kx[k];
      dp[k][y] %= mod;
    }

    dp[y][x] += xk[y];
    dp[y][x] %= mod;

    dp[x][y] += yk[x];
    dp[x][y] %= mod;
  }

  i64 ans = 0;
  rep(j, 0, N) {
    rep(k, j + 1, N) {
      dp[j][k] *= pow2[Q - last[j][k] - 1];
      dp[j][k] %= mod;
      ans += dp[j][k];
      ans %= mod;
    }
  }

  cout << ans << endl;
}

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  cout.setf(ios::fixed);
  cout.precision(16);
  solve();
  return 0;
}