#include <bits/stdc++.h>
using namespace std;
using lint     = long long;
const lint inf = 1LL << 60;
const lint mod = 1000000007;

template <int mod>
struct modint {
    lint v;
    modint() : v(0) {}
    modint(signed v) : v(v) {}
    modint(lint t) {
        v = t % mod;
        if (v < 0)
            v += mod;
    }

    modint pow(lint k) {
        modint res(1), tmp(v);
        while (k) {
            if (k & 1)
                res *= tmp;
            tmp *= tmp;
            k >>= 1;
        }
        return res;
    }
    static modint add_identity() { return modint(0); }
    static modint mul_identity() { return modint(1); }
    modint inv() { return pow(mod - 2); }

    modint &operator+=(modint a) {
        v += a.v;
        if (v >= mod)
            v -= mod;
        return *this;
    }
    modint &operator-=(modint a) {
        v += mod - a.v;
        if (v >= mod)
            v -= mod;
        return *this;
    }
    modint &operator*=(modint a) {
        v = v * a.v % mod;
        return *this;
    }
    modint &operator/=(modint a) { return (*this) *= a.inv(); }

    modint operator+(modint a) const { return modint(v) += a; };
    modint operator-(modint a) const { return modint(v) -= a; };
    modint operator*(modint a) const { return modint(v) *= a; };
    modint operator/(modint a) const { return modint(v) /= a; };

    modint operator-() const { return v ? modint(mod - v) : modint(v); }

    bool operator==(const modint a) const { return v == a.v; }
    bool operator!=(const modint a) const { return v != a.v; }
    bool operator<(const modint a) const { return v < a.v; }
};
using mint = modint<mod>;
ostream &operator<<(ostream &os, mint m) { return os << m.v; }

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n, q;
    cin >> n >> q;
    vector<lint> a(n);
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }
    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]);
    }
    vector<vector<mint>> dp(n, vector<mint>(n, 0));
    for (int i = 0; i < n; ++i) {
        for (int j = i + 1; j < n; ++j) {
            if (a[i] > a[j])
                dp[i][j] = 1;
            else if (a[i] < a[j])
                dp[j][i] = 1;
            else
                continue; // equals
        }
    }
    mint inv2 = mint(1) / 2;
    for (int i = 0; i < q; ++i) {
        // flip x[i] : y[i]
        for (int j = 0; j < x[i]; ++j) {
            mint a0     = (dp[j][x[i]] + dp[j][y[i]]) * inv2;
            mint a1     = (dp[x[i]][j] + dp[y[i]][j]) * inv2;
            dp[j][x[i]] = dp[j][y[i]] = a0;
            dp[x[i]][j] = dp[y[i]][j] = a1;
        }
        for (int j = x[i] + 1; j < y[i]; ++j) {
            mint a0     = (dp[x[i]][j] + dp[y[i]][j]) * inv2;
            mint a1     = (dp[j][y[i]] + dp[j][x[i]]) * inv2;
            mint a2     = (dp[j][x[i]] + dp[j][y[i]]) * inv2;
            mint a3     = (dp[y[i]][j] + dp[x[i]][j]) * inv2;
            dp[x[i]][j] = a0;
            dp[j][y[i]] = a1;
            dp[j][x[i]] = a2;
            dp[y[i]][j] = a3;
        }
        for (int j = y[i] + 1; j < n; ++j) {
            mint a0     = (dp[j][x[i]] + dp[j][y[i]]) * inv2;
            mint a1     = (dp[x[i]][j] + dp[y[i]][j]) * inv2;
            dp[j][x[i]] = dp[j][y[i]] = a0;
            dp[x[i]][j] = dp[y[i]][j] = a1;
        }
        mint t1        = (dp[x[i]][y[i]] + dp[y[i]][x[i]]) * inv2;
        dp[x[i]][y[i]] = t1;
        dp[y[i]][x[i]] = t1;
    }
    mint ret = 0;
    for (int i = 0; i < n; ++i) {
        for (int j = i + 1; j < n; ++j) {
            ret += dp[i][j];
        }
    }
    cout << ret * mint(2).pow(q) << "\n";
    return 0;
}

Measured memory_bytes: 72704