#include #include #include #include #define repeat(i,n) for (int i = 0; (i) < (n); ++(i)) #define repeat_from(i,m,n) for (int i = (m); (i) < (n); ++(i)) typedef long long ll; using namespace std; const int mod = 1e9+7; ll second_kind_stirling(ll n, ll k) { // O(nk) assert (0 <= n and 0 <= k); if (n < k) return 0; if (n == k) return 1; if (k == 0) return 0; static vector > memo; if (memo.size() <= n) { int l = memo.size(); memo.resize(n + 1); repeat_from (i,l,n+1) memo[i].resize(i); } if (memo[n][k]) return memo[n][k]; return memo[n][k] = (second_kind_stirling(n-1, k-1) + k * second_kind_stirling(n-1, k) % mod) % mod; } ll fact(ll n) { static vector memo(1,1); if (memo.size() <= n) { int l = memo.size(); memo.resize(n + 1); repeat_from (i,l,n+1) memo[i] = memo[i-1] * i % mod; } return memo[n]; } int main() { int n, m; cin >> n >> m; vector w(n); repeat (i,n) cin >> w[i]; vector > g(n); repeat (k,m) { int i, j; cin >> i >> j; -- i; -- j; g[j].push_back(i); // inverse } ll ans = 0; repeat (i,n) { vector used(n); function go = [&](int j) { used[j] = true; for (int k : g[j]) if (not used[k]) { if (w[i] <= w[k]) { go(k); } } }; go(i); repeat (j,n) if (used[j]) { ans += fact(w[i]) * second_kind_stirling(w[j], w[i]) % mod; } } ans = (ans % mod + mod) % mod; cout << ans << endl; return 0; }