Language: C++

#include <bits/stdc++.h>
using namespace std;
// define
#define int long long
#define UNIQUE(v) v.erase(unique(all(v)), v.end());
#define ZIP(v) sort(all(v)),UNIQUE(v)
#define ADD(a, b) a = (a + b) % mod
#define SUB(a, b) a = (a+mod-b)%mod
#define MUL(a, b) a = (a * b) % mod
#define rollcall cout << "I'm Sucu." << endl;
#define repi(i,m,n) for(int i = m;i < n;i++)
#define drep(i,n,m) for(int i = n;i >= m;i--)
#define rep(i,n) repi(i,0,n)
#define rrep(i,n) repi(i,1,n+1)
#define chmin(x,y) x = min(x,y)
#define chmax(x,y) x = max(x,y)
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(), v.rend()
#define dmp(x,y) make_pair(x,y)
#define pb(x) push_back(x)
#define pf(x) push_front(x)
#define fi first
#define se second
// debug
template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &p){
  os << "(" << p.first << "," << p.second << ")";return os;
}
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v){
  for (auto it = v.begin();it != v.end();++it){
    if(it != v.begin())os << " ";os << *it;
      }return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &mp){
  for(auto x: mp)os << "(" << x.first << "," << x.second << ")" << endl;
  return os;
}
template<typename T, int SIZE>
int array_length(const T (&)[SIZE]){return SIZE;}
template<typename T, int N>
void PRINTF(const T (&a)[N], int s = N, int t = -1, bool f = true){
	if(t == -1){rep(i,s){if(i)cout << " ";cout << a[i];}}
	else repi(i,s,t){if(i!=s)cout << " ";cout << a[i];}
	if(f)cout << "\n";
}
template<typename T, int N1, int N2>
void PRINTF(const T (&a)[N1][N2], int h = N1, int w = N2){
	rep(i,h){rep(j,w){cout << a[i][j] << " \n"[j==w-1];}}
}
string substr(const string &str, int S1, int S2 = -1){
  if(S2 == -1)return str.substr(S1);
  return str.substr(S1, S2-S1);
}
// typedef
typedef complex<double> Point;
typedef pair<int, int> P;
typedef pair<int, P> PP;
typedef pair<P, int> Pi;
typedef vector<int> vi;
typedef deque<int> dq;
const int inf = 1e9+7;
const int INF = 1e18+7;

const int mod = inf;
int a[2000], b[2000], f[2000000], g[1010][1010];
vector<int> vec; 
int h, w, ans = 1;
int init(){
  rep(i,h){
    int ok = 0, cnt = 0, cnt_max = 0;
    rep(j,w){
      if(a[i] == b[j])cnt++;
      if(b[j] >= a[i])ok = 1;
      if(a[i] < b[j])cnt_max++;
    }
    if(cnt == 0)ans = (ans*cnt_max)%mod;
    if(ok == 0 or cnt > 1)return 1;
    rep(j,w){
      int mn = min(a[i], b[j]);
      if(cnt == 1 and a[i] == b[j]){
        if(f[a[i]])return 1;f[a[i]]++;
        g[i][j] = 1;break;
      }
      if(cnt == 0 and mn == a[i]){
        if(f[a[i]])return 1;f[a[i]]++;
        g[i][j] = 1;break;
      }
    }
  }
  rep(j,w){
    int ok = 0, cnt = 0, cnt_max = 0;
    rep(i,h){
      if(a[i] == b[j])cnt++;
      if(a[i] >= b[j])ok = 1;
      if(b[j] < a[i])cnt_max++;
    }
    if(cnt == 0)ans = (ans*cnt_max)%mod;
    if(ok == 0 or cnt > 1)return 1;
    if(cnt == 1)continue;
    rep(i,h){
      if(g[i][j])continue;
      int mn = min(a[i], b[j]);
      if(cnt == 0 and mn == b[j]){
        if(f[b[j]])return 1;f[b[j]]++;
        g[i][j] = 1;break;
      }
    }
  }
  rep(i,h)rep(j,w){
    int mn = min(a[i], b[j]);
    if(g[i][j] == 0)vec.pb(mn);
  }
  return 0;
}
signed main(){
  scanf("%lld%lld", &h, &w);
  rep(i,h)scanf("%lld", &a[i]);
  rep(i,w)scanf("%lld", &b[i]);
  if(init()){
    printf("0\n");return 0;
  }
  sort(all(vec));
  // cout << vec << endl;
  rrep(i,h*w)f[i] += f[i-1];
  int num = 1, add = 0;
  rep(i,vec.size()){  // num以下
    while(f[num])num++;
    int d = vec[i] - (add+f[num]);
    d = min(d, vec.size()-i);
    if(num > f[i] or d <= 0){
      printf("0\n");return 0;
    }
    ans = (ans*d)%mod;
    f[num]++;add++;
  }
  printf("%lld\n", ans);
  return 0;
}






Compiler output:
a.cc: In function 'int main()':
a.cc:131:12: error: no matching function for call to 'min(long long int&, long long unsigned int)'
  131 |     d = min(d, vec.size()-i);
      |         ~~~^~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_algobase.h:233:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::min(const _Tp&, const _Tp&)'
  233 |     min(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:233:5: note:   template argument deduction/substitution failed:
a.cc:131:12: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'long long unsigned int')
  131 |     d = min(d, vec.size()-i);
      |         ~~~^~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::min(const _Tp&, const _Tp&, _Compare)'
  281 |     min(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note:   candidate expects 3 arguments, 2 provided
In file included from /usr/include/c++/14/algorithm:61:
/usr/include/c++/14/bits/stl_algo.h:5686:5: note: candidate: 'template<class _Tp> constexpr _Tp std::min(initializer_list<_Tp>)'
 5686 |     min(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5686:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5696:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::min(initializer_list<_Tp>, _Compare)'
 5696 |     min(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5696:5: note:   template argument deduction/substitution failed:
a.cc:131:12: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
  131 |     d = min(d, vec.size()-i);
      |         ~~~^~~~~~~~~~~~~~~~~