import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, std.bitmanip;

void main() {
    immutable long MOD = 10^^9 + 7;
    
    auto s = readln.split.map!(to!int);
    auto N = s[0];
    auto M = s[1];
    auto W = readln.split.map!(to!int).array;
    auto G = new int[][](N);
    foreach (_; 0..M) {
        s = readln.split.map!(to!int);
        G[s[0]-1] ~= s[1]-1;
    }

    auto ok = new bool[][](N, N);
    foreach (i; 0..N) ok[i][i] = true;

    foreach (i; 0..N) {
        auto max_k = new int[](N);
        auto q = new BinaryHeap!(Array!(Tuple!(int, int)), "a[1] < b[1]");
        q.insert(tuple(i, W[i]));
        
        while (!q.empty) {
            auto t = q.front;
            auto n = t[0];
            auto k = t[1];
            q.removeFront;
            if (k <= max_k[n]) continue;
            max_k[n] = k;
            if (k >= W[n]) ok[i][n] = true;
            
            foreach (m; G[n]) {
                auto nk = min(k, W[m]);
                if (nk <= max_k[m]) continue;
                q.insert(tuple(m, nk));
            }
        }
    }

    auto F = new long[](1001);
    F[0] = F[1] = 1;
    foreach (i; 2..1001) F[i] = F[i-1] * i % MOD;

    long comb(int n, int k) {
        return F[n] * powmod(F[k], MOD-2, MOD) % MOD * powmod(F[n-k], MOD-2, MOD) % MOD;
    }

    long ans = 0;

    foreach (i; 0..N) {
        foreach (j; 0..N) {
            if (ok[i][j]) {
                foreach (k; 1..W[j]+1) {
                    long tmp = (W[j] - k) % 2 == 0 ? 1 : -1;;
                    tmp = tmp * powmod(k, W[i], MOD) % MOD;
                    tmp = tmp * comb(W[j], k) % MOD;
                    ans = ((ans + tmp) % MOD + MOD) % MOD;
                }
            }
        }
    }

    ans.writeln;
}

long powmod(long a, long x, long m) {
    long ret = 1;
    while (x) {
        if (x % 2) ret = ret * a % m;
        a = a * a % m;
        x /= 2;
    }
    return ret;
}