#include <bits/stdc++.h>

#define REP(i, n) for(int (i)=0;(i)<(n);++(i))
#define MOD 1000000007

using namespace std;

typedef long long ll;

const int match[9] = {2, 5, 5, 4, 5, 6, 3, 7, 6};

int main()
{
    int N, M;
    cin >> N >> M;
    int A[M];
    REP(i, M)
    {
        cin >> A[i];
        A[i]--;
    }

    vector<array<int, 9>> dp(N + 1, {0});
    bool isMake[N + 1] = {false};
    isMake[0] = true;
    for (int i = 1; i <= N; ++i)
    {
        vector<array<int, 9>> candidate;
        REP(j, M)
        {
            if (i - match[A[j]] >= 0)
            {
                if (isMake[i - match[A[j]]])
                {
                    array<int, 9> current = dp[i - match[A[j]]];
                    current[A[j]]++;
                    candidate.push_back(current);
                    isMake[i] = true;
                }
            }
        }
        if (isMake[i])
        {
            array<int, 9> maxArray = {0};
            int countMaxSet = 0;
            for (auto iter = candidate.begin(); iter != candidate.end(); ++iter)
            {
                int count = 0;
                REP(j, 9) count += (*iter)[j];
                if (count != countMaxSet)
                {
                    if (count > countMaxSet)
                    {
                        countMaxSet = count;
                        maxArray = *iter;
                    }
                }
                else
                {
                    for (int j = 8; j >= 0; --j)
                    {
                        if (maxArray[j] > (*iter)[j]) 
                        {
                            break;
                        }
                        else if (maxArray[j] < (*iter)[j])
                        {
                            maxArray = *iter;
                            break;
                        }
                    }
                }
            }
            dp[i] = maxArray;
        }
    }

    for(int i = 8; i >= 0; --i) REP(j, dp[N][i]) cout << i + 1;
}

#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < (int)n; i++)
using ll = long long;
const ll mod = 1e9+7;

int main(){
    int n, m;
    cin >> n >> m;
    int a[n], b[m];
    vector<ll> va(n*m+1,0), vb(n*m+1,0);
    rep(i,n) {
      cin >> a[i];
      va[a[i]]++;
    }
    rep(i,m) {
      cin >> b[i];
      vb[b[i]]++;
    }
    vector<ll> num(n*m+1,0);
    rep(i,n) {
      rep(j,m) num[min(a[i],b[j])]++;
    }
    ll ans = 1, s = 0;
    for(int i = n*m; i >= 1; i--) {
      if(va[i] >= 2 || vb[i] >= 2) {
        ans = 0;
      } else if (va[i] == 1 && vb[i] == 1){
        ans *= 1;
      } else if(va[i] == 1 || vb[i] == 1) {
        (ans *= num[i]) %= mod;
      } else {
        (ans *= num[i] + s) %= mod;
      }
      s += num[i] - 1;
    }
    cout << ans << endl;
    return 0;
}