#ifdef LOCAL
#  define _GLIBCXX_DEBUG
#else
#  define cerr __get_ce
#endif
#include <bits/stdc++.h>
#define ff first
#define ss second
#define szof(x) ((int)x.size())

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
int const INF = (int)1e9 + 1e3;
ll const INFL = (ll)1e18 + 1e6;
mt19937 tw(9450189);
uniform_int_distribution<ll> ll_distr;
ll rnd(ll a, ll b) { return ll_distr(tw) % (b - a + 1) + a; }

const int MOD = 1000000007;

void add(int& a, int b) {
	a += b;
	if (a >= MOD) {
		a -= MOD;
	}
}

int sum(int a, int b) {
	add(a, b);
	return a;
}

int mult(int a, int b) {
	return (ll) a * b % MOD;
}

int mpow(int a, int b) {
	int ret = 1;
	while (b) {
		if (b & 1) {
			ret = mult(ret, a);
		}
		a = mult(a, a);
		b >>= 1;
	}

	return ret;
}

int inv(int num) {
	return mpow(num, MOD - 2);
}

void solve() {
	int n, q;
	cin >> n >> q;
	vector<int> inp;
	for (int i = 0; i < n; ++i) {
		int num;
		cin >> num;
		inp.push_back(num);
	}
	vector<pii> reqs;
	for (int i = 0; i < q; ++i) {
		int a, b;
		cin >> a >> b;
		if (a > b) {
			swap(a, b);
		}
		--a; --b;
		reqs.push_back({a, b});
	}

	/*
	vector<int> order(n);
	iota(order.begin(), order.end(), 0);
	vector<vector<int>> cnt(n, vector<int>(n));

	int ans0 = 0;

	function<void(int)> rec = [&](int pos) {
		if (pos == q) {
			for (int num : order) {
				cerr << num + 1 << " ";
			}
			cerr << endl;
			for (int i = 0; i < n; ++i) {
				for (int j = i + 1; j < n; ++j) {
					if (order[i] < order[j]) {
						cnt[order[i]][order[j]]++;
					}
					if (inp[order[i]] > inp[order[j]]) {
						++ans0;
					}
				}
			}
			return;
		}
		rec(pos + 1);
		swap(order[reqs[pos].ff], order[reqs[pos].ss]);
		rec(pos + 1);
		swap(order[reqs[pos].ff], order[reqs[pos].ss]);
	};

	rec(0);

	for (int i = 0; i < n; ++i) {
		for (int j = i + 1; j < n; ++j) {
			cerr << cnt[i][j] << " ";
		}
		cerr << endl;
	}
	*/
	vector<vector<int>> matr(n, vector<int>(n, 1));
	int inv2 = inv(2);
	int inv4 = mult(inv2, inv2);
	int tot = 1;

	for (int i = q - 1; i >= 0; --i) {
		int a, b;
		tie(a, b) = reqs[i];
		for (int j = a + 1; j < b; ++j) {
			int tmp1 = matr[a][j];
			int tmp2 = matr[j][b];
			matr[a][j] = mult(sum(tmp1, sum(1, MOD - tmp2)), inv2);
			matr[j][b] = mult(sum(tmp2, sum(1, MOD - tmp1)), inv2);
		}
		for (int j = b + 1; j < n; ++j) {
			int tmp1 = matr[a][j];
			int tmp2 = matr[b][j];
			matr[a][j] = mult(sum(tmp1, tmp2), inv2);
			matr[b][j] = matr[a][j];
		}
		for (int j = 0; j < a; ++j) {
			int tmp1 = matr[j][a];
			int tmp2 = matr[j][b];
			matr[j][a] = mult(sum(tmp1, tmp2), inv2);
			matr[j][b] = matr[j][a];
		}
		matr[a][b] = inv2;
		tot = mult(tot, 2);
	}

	int ans = 0;

	for (int i = 0; i < n; ++i) {
		for (int j = i + 1; j < n; ++j) {
			matr[i][j] = mult(matr[i][j], tot);
			// cerr << matr[i][j] << " ";
			if (inp[i] > inp[j]) {
				add(ans, matr[i][j]);
			} else if (inp[i] < inp[j]) {
				add(ans, sum(tot, MOD - matr[i][j]));
			}
		}
		// cerr << endl;
	}

	cout << ans << "\n";
	// cerr << ans0 << endl;
}


int main() {
#ifdef LOCAL
	auto start_time = clock();
	cerr << setprecision(3) << fixed;
#endif
	cout << setprecision(15) << fixed;
	ios::sync_with_stdio(false);
	cin.tie(nullptr);

	int test_count = 1;
	// cin >> test_count;
	for (int test = 1; test <= test_count; ++test) {
		solve();
	}
	
#ifdef LOCAL
	auto end_time = clock();
	cerr << "Execution time: " << (end_time - start_time) * (int)1e3 / CLOCKS_PER_SEC << " ms\n";
#endif
}


Measured memory_bytes: 37640