//Link : https://atcoder.jp/contests/agc030/tasks/agc030_d

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define N 3010
#define mod 1000000007

int A[N];

struct qqq{
  int dp,dp2,lazy;
} mdp[N][N][2];

int getState(int i,int j) {
  if(i<j) {
    return 0;
  } else {
    return 1;
  }
}

inline int add(int x,int y) {
  int ret = x+y;
  if(ret>=mod) {
    return ret-mod;
  }
  return ret;
}

inline int mul(int x,int y) {
  return (ll)x*y%mod;
}


void getTransition(int &st,int &i,int &j,int x,int y) {
  int ni = i;
  if(x==i) {
    ni = y;
  } else if(ni==y) {
    ni = x;
  }
  int nj = j;
  if(x==j) {
    nj = y;
  } else if(nj==y) {
    nj = x;
  }
  int ijState = (i<j) ? 1 : 0;
  int ni_nj_state = (ni<nj) ? 1 :0;
  int nst = st;
  if(ijState!=ni_nj_state) {
    nst ^=1;
  }
  st = nst;
  i = ni;
  j = nj;
}

int powerOfTwo[N];
int getValue(int x,int y,int st,int curQ) {
  int v = powerOfTwo[curQ- mdp[x][y][st].lazy];
  return mul(v,mdp[x][y][st].dp);
}

void solve() {
  int n,q;
  scanf("%d %d", &n,&q);
  powerOfTwo[0] = 1;
  for(int i=1;i<N;++i) {
    powerOfTwo[i] = mul(powerOfTwo[i-1],2);
  }

  for(int i=0;i<n;++i) {
    scanf("%d ", &A[i]);
  }

  for(int i=0;i<n;++i) {
    for(int j=i+1;j<n;++j) {
      if(A[i]==A[j]) {
        continue;
      }
      if(A[i]>A[j]) {
        ++mdp[i][j][1].dp;
      } else {
        ++mdp[i][j][0].dp;
      }
    }
  }

  int lazy_value = 0;
  pair<int,pair<int,int> >trans;
  int val,i,st;
  int a,b,c;
  while(q--) {
    int x,y;scanf("%d %d ", &x,&y);
    if(x>y) {
      swap(x,y);
    }

    --x,--y;
    //optimize by lazy-eval
    for(i=0;i<n;++i) {
      for(st=0;st<2;++st) {
        if(i!=x) {
          mdp[x][i][st].dp2 = 0;
          mdp[i][x][st].dp2 = 0;
        }
        if(i!=y && i!=x) {
          mdp[y][i][st].dp2 = 0;
          mdp[i][y][st].dp2 = 0;
        }
      }
    }
    for(i=0;i<n;++i) {
      for(st=0;st<2;++st) {
        if(i!=x) {
          //dp2[i][x][st] = 0;
          val = getValue(i,x,st,lazy_value);
          if(val!=0) {
            a = st,b= i,c = x;
            getTransition(a,b,c,x,y);

            mdp[i][x][st].dp2 = add(mdp[i][x][st].dp2, val);
            mdp[b][c][a].dp2 = add(mdp[b][c][a].dp2,val);
          }
          val = getValue(x,i,st,lazy_value);
          if(val!=0) {
            a = st,b = x,c = i;
            getTransition(a,b,c,x,y);
            mdp[x][i][st].dp2 = add(mdp[x][i][st].dp2, val);
            mdp[b][c][a].dp2 = add(mdp[b][c][a].dp2,val);
          }
        }
        if(i!=y && i!=x) {
          val = getValue(i,y,st,lazy_value);
          if(val!=0) {
            a = st,b= i,c = y;
            getTransition(a,b,c,x,y);
            mdp[i][y][st].dp2 = add(mdp[i][y][st].dp2, val);
            mdp[b][c][a].dp2 = add(mdp[b][c][a].dp2,val);
          }

          val = getValue(y,i,st,lazy_value);
          if(val!=0) {
            a = st,b= y,c = i;
            getTransition(a,b,c,x,y);
            mdp[y][i][st].dp2 = add(mdp[y][i][st].dp2, val);
            mdp[b][c][a].dp2 = add(mdp[b][c][a].dp2,val);
          }
        }
      }
    }

    for(i=0;i<n;++i) {
      for(st=0;st<2;++st) {
        if(i!=x) {
          mdp[x][i][st].dp = mdp[x][i][st].dp2;
          mdp[i][x][st].dp = mdp[i][x][st].dp2;
          mdp[x][i][st].lazy = lazy_value+1;
          mdp[i][x][st].lazy = lazy_value+1;
        }
        if(i!=y && i!=x) {
          mdp[y][i][st].dp = mdp[y][i][st].dp2;
          mdp[i][y][st].dp = mdp[i][y][st].dp2;
          mdp[y][i][st].lazy = lazy_value+1;
          mdp[i][y][st].lazy = lazy_value+1;
        }
      }
    }
    ++lazy_value;
  }
  int ret = 0,j;
  for(i=0;i<n;++i) {
    for(j=0;j<n;++j) {
      ret = add(ret, getValue(i,j,1,lazy_value));
    }
  }

  printf("%d\n", ret);
}

int main() {
    //freopen("input.txt","r",stdin);
    solve();
    return 0;
}


Measured memory_bytes: 212096