#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <vector>
using namespace std;
using ll = long long;
#define fst first
#define snd second

/* clang-format off */
template <class T, size_t D> struct _vec { using type = vector<typename _vec<T, D - 1>::type>; };
template <class T> struct _vec<T, 0> { using type = T; };
template <class T, size_t D> using vec = typename _vec<T, D>::type;
template <class T> vector<T> make_v(size_t size, const T& init) { return vector<T>(size, init); }
template <class... Ts> auto make_v(size_t size, Ts... rest) { return vector<decltype(make_v(rest...))>(size, make_v(rest...)); }
template <class T> inline void chmin(T &a, const T& b) { if (b < a) a = b; }
template <class T> inline void chmax(T &a, const T& b) { if (b > a) a = b; }
/* clang-format on */

const ll MOD = 1e9 + 7;

ll power(ll x, ll e) {
  ll v = 1;
  for (; e > 0; e >>= 1) {
    if (e & 1) {
      v = v * x % MOD;
    }
    x = x * x % MOD;
  }
  return v;
}

ll inverse(ll x) {
  return power(x, MOD - 2);
}

using P = tuple<int, int, ll>;

int main() {
#ifdef DEBUG
  ifstream cin("in.txt");
#endif
  int N, Q;
  while (cin >> N >> Q) {
    vector<ll> A(N);
    for (ll& x : A) cin >> x;
    vector<int> X(Q), Y(Q);
    for (int i = 0; i < Q; i++) {
      cin >> X[i] >> Y[i];
      --X[i];
      --Y[i];
      if (X[i] > Y[i]) swap(X[i], Y[i]);
    }
    vec<ll, 2> dp = make_v(N, N, 0ll);
    ll inv2 = inverse(2);
    for (int q = Q - 1; q >= 0; q--) {
      int x = X[q], y = Y[q];
      // (x, *)
      vector<P> ps;
      for (int j = x + 1; j < N; j++) {
        if (j == y) continue;
        if (y < j) {
          ps.emplace_back(x, j, (dp[x][j] + dp[y][j]) * inv2 % MOD);
        } else {
          ps.emplace_back(x, j, (dp[x][j] + (1 - dp[j][y] + MOD)) * inv2 % MOD);
        }
      }
      // (*, y)
      for (int i = 0; i < y; i++) {
        if (i == x) continue;
        if (i < x) {
          ps.emplace_back(i, y, (dp[i][y] + dp[i][x]) * inv2 % MOD);
        } else {
          ps.emplace_back(i, y, (dp[i][y] + (1 - dp[x][i] + MOD)) * inv2 % MOD);
        }
      }
      // (y, *)
      for (int j = y + 1; j < N; j++) {
        ps.emplace_back(y, j, (dp[y][j] + dp[x][j]) * inv2 % MOD);
      }
      // (*, x)
      for (int i = 0; i < x; i++) {
        ps.emplace_back(i, x, (dp[i][x] + dp[i][y]) * inv2 % MOD);
      }
      // (x, y)
      ps.emplace_back(x, y, (dp[x][y] + (1 - dp[x][y] + MOD)) * inv2 % MOD);
      for (auto& p : ps) {
        int i, j;
        ll v;
        tie(i, j, v) = p;
        dp[i][j] = v;
      }
    }
    ll res = 0;
    for (int i = 0; i < N; i++) {
      for (int j = i + 1; j < N; j++) {
        if (A[i] < A[j]) {
          res += dp[i][j];
          res %= MOD;
        } else if (A[i] > A[j]) {
          res += 1 - dp[i][j] + MOD;
          res %= MOD;
        }
      }
    }
    res = res * power(2, Q) % MOD;
    cout << res << endl;
  }
  return 0;
}


Measured memory_bytes: 73036