#include <iostream>
#include <string>
#include <cmath>
#include<algorithm>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<iomanip>
#define _USE_MATH_DEFINES
#include <math.h>
#include <functional>
#include<complex>
using namespace std;

#define rep(i,x) for(ll i=0;i<x;i++)
#define repn(i,x) for(ll i=1;i<=x;i++)

typedef long long ll;
const ll INF = 1e17;
const ll MOD = 1000000007;
const ll MAX = 4000001;
const double eps = 1E-18;

ll max(ll a, ll b) {
	if (a > b) { return a; }
	return b;
}

ll min(ll a, ll b) {
	if (a > b) { return b; }
	return a;
}

ll gcd(ll a, ll b) {
	if (b == 0) { return a; }
	if (a < b) { return gcd(b, a); }
	return gcd(b, a % b);
}

ll lcm(ll a, ll b) {
	return a / gcd(a, b) * b;
}

struct edge {
	ll ind;
	ll fr;
	ll to;
	ll d;
};

class mint {
	long long x;
public:
	mint(long long x = 0) : x((x% MOD + MOD) % MOD) {}
	mint operator-() const {
		return mint(-x);
	}
	mint& operator+=(const mint& a) {
		if ((x += a.x) >= MOD) x -= MOD;
		return *this;
	}
	mint& operator-=(const mint& a) {
		if ((x += MOD - a.x) >= MOD) x -= MOD;
		return *this;
	}
	mint& operator*=(const  mint& a) {
		(x *= a.x) %= MOD;
		return *this;
	}
	mint operator+(const mint& a) const {
		mint res(*this);
		return res += a;
	}
	mint operator-(const mint& a) const {
		mint res(*this);
		return res -= a;
	}
	mint operator*(const mint& a) const {
		mint res(*this);
		return res *= a;
	}

	mint pow(ll t) const {
		if (!t) return 1;
		mint a = pow(t >> 1);
		a *= a;
		if (t & 1) a *= *this;
		return a;
	}

	mint operator^(const ll& t)const {
		if (!t) return 1;
		mint a = *this^(t >> 1);
		a *= a;
		if (t & 1) a *= *this;
		return a;
	}

	// for prime MOD
	mint inv() const {
		return pow(MOD - 2);
	}
	mint& operator/=(const mint& a) {
		return (*this) *= a.inv();
	}
	mint operator/(const mint& a) const {
		mint res(*this);
		return res /= a;
	}

	friend ostream& operator<<(ostream& os, const mint& m) {
		os << m.x;
		return os;
	}
};

///////////////////////////


int main() {
	ll N, Q;
	cin >> N >> Q;
	vector<ll> A(N + 1);
	repn(i, N) cin >> A[i];

	vector<ll> X(Q + 1), Y(Q + 1);
	repn(i, Q) cin >> X[i] >> Y[i];

	vector<vector<mint>> dp(N + 1, vector<mint>(N + 1,0));
	vector<vector<ll>> up(N + 1, vector<ll>(N + 1, 0));

	vector<mint> pw(Q + 1, 1);
	repn(i, Q)pw[i] = pw[i - 1] * 2;

	repn(i, N)repn(j, N) {
		if (A[i] > A[j]) { dp[i][j] = 1; }
	}
	
	repn(q, Q) {
		ll x = X[q];
		ll y = Y[q];

		repn(i, N) {
			if (i == x || i == y) { continue; }

			mint t, u;

			t = pw[q - up[x][i] - 1] * dp[x][i];
			t += pw[q - up[y][i] - 1] * dp[y][i];

			u = pw[q - up[x][i] - 1] * dp[x][i];
			u += pw[q - up[y][i] - 1] * dp[y][i];

			dp[x][i] = t;
			dp[y][i] = u;

			t = 0;
			u = 0;

			t = pw[q - up[i][x] - 1] * dp[i][x];
			t += pw[q - up[i][y] - 1] * dp[i][y];

			u = pw[q - up[i][x] - 1] * dp[i][x];
			u += pw[q - up[i][y] - 1] * dp[i][y];

			dp[i][x] = t;
			dp[i][y] = u;

			up[x][i] = q;
			up[y][i] = q;
			up[i][x] = q;
			up[i][y] = q;

		}

		mint t, u;

		t = pw[q - up[x][y] - 1] * dp[x][y];
		t += ((mint)2 ^ (q - up[y][x] - 1)) * dp[y][x];

		u = pw[q - up[x][y] - 1] * dp[x][y];
		u+= pw[q - up[y][x] - 1] * dp[y][x];

		dp[x][y] = t;
		dp[y][x] = u;

		up[x][y] = q;
		up[y][x] = q;


	}

	mint ans = 0;
	repn(i, N)repn(j, N) {
		dp[i][j] *= ((mint)2 ^ (Q - up[i][j]));
		if (j > i) { ans += dp[i][j]; }
	}

	cout << ans << endl;

}


Measured memory_bytes: 143232