#include<bits/stdc++.h>
using namespace std;
#define int long long
const int mod = 1e9 + 7;
int N , M;
map<int,vector<pair<int,int>>> MAP;
int dp[1 << 18];

bool issafe(int mask){
	int count = __builtin_popcount(mask);
	for(auto itr: MAP[count]){
		int cnt = 0;
		for(int k = 0 ; k < N ; k ++){
			if(mask&(1 << k) && (k + 1)<=itr.first){
				cnt ++;
			}
		}
		if(cnt > itr.second)
			return false;
	}
	return true;
}

int rec(int mask){
	if(mask == ((1 << N) - 1))
		return 1;
	if(dp[mask]!=-1)
		return dp[mask];
	int ans = 0;
	for(int i = 1 ; i <= N ; i ++){
		if(!(mask & (1 << (i - 1)))){
			int MASK = mask|(1 << (i - 1));
			if(issafe(MASK)){
				ans += rec(MASK);
			}
		}
	}
	return dp[mask] = ans;
}

signed main(){
		
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int T;
	T = 1;
	while(T--){
		cin >> N >> M;
		while(M --){
			int x , y , z;
			cin >> x >> y >> z;
			MAP[x].push_back({y,z});
		}
		memset(dp,-1,sizeof dp);
		cout << rec(0) << "\n";
	}
	
		
		
		
}

#include <bits/stdc++.h>
 
using namespace std;
 
//#define MULTITEST
 
#define x first
#define y second
#define mp make_pair
#define pb push_back
#define sqr(a) ((a) * (a))
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define forn(i, n) for(int i = 0; i < int(n); i++) 
#define fore(i, l, r) for(int i = int(l); i < int(r); i++)
 
typedef long long li;
typedef long double ld;
typedef pair<int, int> pt;
 
template <class A, class B> ostream& operator << (ostream& out, const pair<A, B> &a) {
	return out << "(" << a.x << ", " << a.y << ")";
}
 
template <class A> ostream& operator << (ostream& out, const vector<A> &v) {
	out << "[";
	forn(i, sz(v)) {
		if(i) out << ", ";
		out << v[i];
	}
	return out << "]";
}
 
mt19937 rnd(time(NULL));
 
const int INF = int(1e9);
const li INF64 = li(1e18);
const int MOD = int(1e9) + 7;
const ld EPS = 1e-9;
const ld PI = acos(-1.0);

const int N = 200 * 1000 + 13;

int n, m;
int a[N];
int b[N];
vector<int> g[N];

bool read () {
	if (scanf("%d%d", &n, &m) != 2)
		return false;
	forn(i, n)
		scanf("%d", &a[i]);
	forn(i, n)
		scanf("%d", &b[i]);
	forn(i, n)
		g[i].clear();
	forn(i, m){
		int v, u;
		scanf("%d%d", &v, &u);
		--v, --u;
		g[v].pb(u);
		g[u].pb(v);
	}
	return true;
}

li suma, sumb;
bool used[N];

void dfs(int v){
	used[v] = true;
	suma += a[v];
	sumb += b[v];
	for (int u : g[v]) if (!used[u])
		dfs(u);
}
 
void solve() {
	memset(used, 0, sizeof(used));
	forn(i, n) if (!used[i]){
		suma = sumb = 0;
		dfs(i);
		if (suma != sumb){
			puts("No");
			return;
		}
	}
	puts("Yes");
}
 
int main() {
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);
	
	int tt = clock();
	
#endif
	
	cerr.precision(15);
	cout.precision(15);
	cerr << fixed;
	cout << fixed;
 
#ifdef MULTITEST
	int tc;
	scanf("%d", &tc);
	while(tc--){
		read();
#else
	while(read()) {	
#endif
		solve();
		
#ifdef _DEBUG
	cerr << "TIME = " << clock() - tt << endl;
	tt = clock();
#endif
 
	}
}