#include // c #include // io #include #include #include #include // container #include #include #include #include #include #include // other #include #include #include #include #include using namespace std; typedef long long ll;typedef unsigned long long ull;typedef long double ld; #define ALL(c) c.begin(),c.end() #define IN(l,v,r) (l<=v && v < r) template void UNIQUE(T& v){v.erase(unique(ALL(v)),v.end());} //debug #define DUMP(x) cerr << #x <<" = " << (x) #define LINE() cerr<< " (L" << __LINE__ << ")" struct range{ struct Iter{ int v,step; Iter& operator++(){v+=step;return *this;} bool operator!=(Iter& itr){return vitr.v;} int& operator*(){return v;} }; Iter i, n; rrange(int i, int n,int step):i({i-1,step}), n({n-1,step}){} rrange(int i, int n):rrange(i,n,1){} rrange(int n) :rrange(0,n){} Iter& begin(){return n;} Iter& end(){return i;} }; //input template istream& operator >> (istream& is,pair& p){return is>>p.first>>p.second;} template istream& operator >> (istream& is,tuple& t){return is >> get<0>(t);} template istream& operator >> (istream& is,tuple& t){return is >> get<0>(t) >> get<1>(t);} template istream& operator >> (istream& is,tuple& t){return is >>get<0>(t)>>get<1>(t)>>get<2>(t);} template istream& operator >> (istream& is,tuple& t){return is >> get<0>(t)>>get<1>(t)>>get<2>(t)>>get<3>(t);} template istream& operator >> (istream& is,vector& as){for(int i:range(as.size()))is >>as[i];return is;} //output template ostream& operator << (ostream& os, const set& ss){for(auto a:ss){if(a!=ss.begin())os<<" "; os< ostream& operator << (ostream& os, const pair& p){return os< ostream& operator << (ostream& os, const map& m){bool isF=true;for(auto& p:m){if(!isF)os< ostream& operator << (ostream& os, const tuple& t){return os << get<0>(t);} template ostream& operator << (ostream& os, const tuple& t){return os << get<0>(t)<<" "<(t);} template ostream& operator << (ostream& os, const tuple& t){return os << get<0>(t)<<" "<(t)<<" "<(t);} template ostream& operator << (ostream& os, const tuple& t){return os << get<0>(t)<<" "<(t)<<" "<(t)<<" "<(t);} template ostream& operator << (ostream& os, const vector& as){for(int i:range(as.size())){if(i!=0)os<<" "; os< ostream& operator << (ostream& os, const vector>& as){for(int i:range(as.size())){if(i!=0)os< inline T INF(){assert(false);}; template<> inline int INF(){return 1<<28;}; template<> inline ll INF(){return 1LL<<58;}; template<> inline double INF(){return 1e16;}; template<> inline long double INF(){return 1e16;}; template inline T EPS(){assert(false);}; template<> inline int EPS(){return 1;}; template<> inline ll EPS(){return 1LL;}; template<> inline double EPS(){return 1e-8;}; template<> inline long double EPS(){return 1e-8;}; // min{2^r | n < 2^r} template T upper_pow2(T n){ T res=1;while(res T msb(T n){ int d=62;while((1LL<n)d--;return d;} template T pmod(T v,U M){return (v%M+M)%M;} ll MOD = 1e9+7; class Main{ public: void run(){ int N,M;cin >> N >> M; vector ws(N);cin >> ws; vector> ps(M); cin >> ps; for(int i:range(M))ps[i].first--,ps[i].second--; vector> gs(2*N,vector(2*N,0)); for(int i:range(N)) gs[i][i+N] = ws[i]; for(int i:range(M)){ int f,t;tie(f,t)=ps[i]; gs[f+N][t] = INF(); } // (+,min) -> (min,max) for(int k:range(2*N))for(int i:range(2*N))for(int j:range(2*N)){ gs[i][j] = max(gs[i][j],min(gs[i][k],gs[k][j])); } vector> dp(1050,vector(1050)); dp[0][0] = 1; for(int i:range(1,1050))for(int j:range(0,1050)){ // i-1個で既に全射 or i個目で新たに全射 dp[i][j] = (j*(dp[i-1][j] + dp[i-1][j-1])%MOD)%MOD; } ll res = 0; for(int i:range(N))for(int j:range(N))if(gs[i][j+N] == ws[j]){ res = (res + dp[ws[i]][ws[j]])%MOD; } cout << res << endl; } }; int main(){ cout <