#include <bits/stdc++.h>
#define int long long
using namespace std;
int dp[3000][3000], dp2[3000][3000];
int ch[3000][3000];
int MOD = 1000000007;
signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n, q;
    cin >> n >> q;
    vector<int> v(n);
    for (int i=0; i < n; i++){
        cin >> v[i];
    }
    vector<pair<int, int> > inv(q);
    for (int i=0; i < q; i++){
        int a, b;
        cin >> a >> b;
        inv[i] = {a-1, b-1};
    }
    for (int i=0; i < n; i++) for (int j=i+1; j < n; j++) dp[i][j] = 1;
    for (int i=0; i < n; i++) for (int j=i+1; j < n; j++) ch[i][j] = q-1;
    vector<int> pw;
    pw.push_back(1);
    for (int i=0; i < 10000; i++) pw.push_back((pw.back() * 2) % MOD);
    for (int i=q-1; i >= 0; i--){
        int A = inv[i].first, B = inv[i].second;
        for (int j=A+1; j < n; j++){
            if (j==B) continue;
            dp[A][j] *= pw[ch[A][j] - i];
            dp[A][j] %= MOD;
        }
        for (int j=0; j < A; j++){
            if (j==B) continue;
            dp[j][A] *= pw[ch[j][A] - i];
            dp[j][A] %= MOD;
        }
        for (int j=B+1; j < n; j++){
            if (j==A) continue;
            dp[B][j] *= pw[ch[B][j] - i];
            dp[B][j] %= MOD;
        }
        for (int j=0; j < B; j++){
            if (j==A) continue;
            dp[j][B] *= pw[ch[j][B] - i];
            dp[j][B] %= MOD;
        }
        int aa = A, bb = B;
        if (aa > bb) swap(aa, bb);
        dp[aa][bb] *= pw[ch[aa][bb] - i];
        dp[aa][bb] %= MOD;
        for (int j=A+1; j < n; j++){
            if (j==B) continue;
            if (B < j){
                dp2[A][j] = (dp[A][j] + dp[B][j]) % MOD;
            }
            else{
                int num = (pw[q-i-1] - dp[j][B]) % MOD;
                if (num < 0) num += MOD;
                dp2[A][j] = (dp[A][j] + num) % MOD;
            }
        }
        for (int j=0; j < A; j++){
            if (j==B) continue;
            if (B > j){
                dp2[j][A] = (dp[j][A] + dp[j][B]) % MOD;
            }
            else{
                int num = (pw[q-i-1] - dp[B][j]) % MOD;
                if (num < 0) num += MOD;
                dp2[j][A] = (dp[j][A] + num) % MOD;
            }
        }
        for (int j=B+1; j < n; j++){
            if (j==A) continue;
            if (A < j){
                dp2[B][j] = (dp[A][j] + dp[B][j]) % MOD;
            }
            else{
                int num = (pw[q-i-1] - dp[j][A]) % MOD;
                if (num < 0) num += MOD;
                dp2[B][j] = (dp[B][j] + num) % MOD;
            }
        }
        for (int j=0; j < B; j++){
            if (j==A) continue;
            if (A > j){
                dp2[j][B] = (dp[j][A] + dp[j][B]) % MOD;
            }
            else{
                int num = (pw[q-i-1] - dp[A][j]) % MOD;
                if (num < 0) num += MOD;
                dp2[j][B] = (dp[j][B] + num) % MOD;
            }
        }
        dp2[aa][bb] = pw[q-i-1];
        ch[aa][bb] = i-1;
        for (int j=A+1; j < n; j++){
            if (j==B) continue;
            dp[A][j] = dp2[A][j];
            ch[A][j] = i-1;
        }
        for (int j=0; j < A; j++){
            if (j==B) continue;
            dp[j][A] = dp2[j][A];
            ch[j][A] = i-1;
        }
        for (int j=B+1; j < n; j++){
            if (j==A) continue;
            dp[B][j] = dp2[B][j];
            ch[B][j] = i-1;
        }
        for (int j=0; j < B; j++){
            if (j==A) continue;
            dp[j][B] = dp2[j][B];
            ch[j][B] = i-1;
        }
        dp[aa][bb] = dp2[aa][bb];
//        for (int i=0; i < n; i++) for (int j=i+1; j < n; j++){
//            cout << i << " " << j << " " << dp[i][j] << " " << ch[i][j] << endl;
//        }
//        cout << endl;
    }
    int ans = 0;
    for (int i=0; i < n; i++){
        for (int j=i+1; j < n; j++){
            dp[i][j] *= pw[ch[i][j] + 1];
            dp[i][j] %= MOD;
            if (v[i] == v[j]) continue;
            if (v[i] > v[j]) ans += dp[i][j];
            else{
                ans += pw[q];
                ans -= dp[i][j];
            }
        }
    }
    ans %= MOD;
    if (ans < 0) ans += MOD;
    cout << ans;
    return 0;
}




Measured memory_bytes: 211456