// ${url}
//
#![allow(unused_imports)]
use std::io::*;
use std::fmt::*;
use std::str::*;
use std::cmp::*;
use std::collections::*;

macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

macro_rules! input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};

    ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };
}

macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };

    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
    };

    ($iter:expr, [ next / $t:tt ]) => {
        {
            let len = read_value!($iter, usize);
            (0..len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
        }
    };

    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };

    ($iter:expr, usize1) => {
        read_value!($iter, usize) - 1
    };

    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}

#[allow(unused_macros)]
macro_rules! dvec {
    ($t:expr ; $len:expr) => {
        vec![$t; $len]
    };

    ($t:expr ; $len:expr, $($rest:expr),*) => {
        vec![dvec!($t; $($rest),*); $len]
    };
}

#[allow(unused_macros)]
macro_rules! ifv {
    ($t:expr, $a:expr, $b: expr) => {
        if $t { $a } else { $b }
    }
}

#[allow(unused_macros)]
macro_rules! fill {
    ($t:expr, $v:expr) => {
        for i in 0..$t.len() {
            $t[i] = $v;
        }
    };
}

#[allow(unused_macros)]
macro_rules! join {
    ($t:expr, $glue:expr) => {
        $t.into_iter().map(|w| w.to_string()).collect::<Vec<_>>().join($glue)
    };
}

#[allow(unused_macros)]
macro_rules! debug {
    ($($a:expr),*) => {
        println!(concat!($(stringify!($a), " = {:?}, "),*), $($a),*);
    }
}

const MOD: i64 = 1e9 as i64 + 7;

fn get(u: usize, v: usize, qi: usize, dp: &mut Vec<Vec<i64>>, dp_last: &mut Vec<Vec<usize>>, p2: &Vec<i64>) -> i64 {
    assert!(u != v);
    assert!(qi >= dp_last[u][v]);
    let bai = qi - dp_last[u][v];
    dp[u][v] *= p2[bai];
    dp[u][v] %= MOD;
    dp_last[u][v] = qi;
    dp[u][v]
}

fn main() {
    input! {
        n: usize, q: usize,
        a: [i32; n],
        queries: [(usize1, usize1); q]
    };


    let mut dp = dvec!(0; n, n);
    let mut dp_last = dvec!(0; n, n);
    for i in 0..n {
        for j in 0..n {
            dp[i][j] = ifv!(a[i] < a[j], 1, 0);
        }
    }

    let mut p2 = vec![0; q+1];
    p2[0] = 1;
    for i in 0..q {
        p2[i+1] = p2[i] * 2 % MOD;
    }

    let mut tmpu = vec![0; n];
    let mut tmpv = vec![0; n];
    let mut cr = 0;
    for (u, v) in queries {
        // u,v
        tmpu[u] = 0;
        tmpv[v] = 0;

        let uv = get(u, v, cr, &mut dp, &mut dp_last, &p2);
        let vu = get(v, u, cr, &mut dp, &mut dp_last, &p2);
        tmpu[v] = (uv + vu) % MOD;
        tmpv[u] = (uv + vu) % MOD;

        for i in 0..n {
            if i != u && i != v {
                let ui = get(u, i, cr, &mut dp, &mut dp_last, &p2);
                let vi = get(v, i, cr, &mut dp, &mut dp_last, &p2);
                tmpu[i] = (ui + vi) % MOD;
                tmpv[i] = (ui + vi) % MOD;
                let iu = get(i, u, cr, &mut dp, &mut dp_last, &p2);
                let iv = get(i, v, cr, &mut dp, &mut dp_last, &p2);
                let nw = (iu + iv) % MOD;
                dp[i][u] = nw;
                dp[i][v] = nw;
                dp_last[i][u] = cr+1;
                dp_last[i][v] = cr+1;
            }
        }
        for i in 0..n {
            dp[u][i] = tmpu[i];
            dp[v][i] = tmpv[i];
            dp_last[u][i] = cr+1;
            dp_last[v][i] = cr+1;
        }
        cr += 1;
    }

    // debug!(dp);

    let mut ans = 0;
    for i in 0..n {
        for j in i+1..n {
            ans += get(j, i, q, &mut dp, &mut dp_last, &p2);
            ans %= MOD;
        }
    }



    println!("{}", ans);
}


Measured memory_bytes: 176508