Problem:
 Score : 500 points    Problem Statement  There are N computers and N sockets in a one-dimensional world.
The coordinate of the i -th computer is a_i , and the coordinate of the i -th socket is b_i .
It is guaranteed that these 2N coordinates are pairwise distinct.  Snuke wants to connect each computer to a socket using a cable.
Each socket can be connected to only one computer.  In how many ways can he minimize the total length of the cables?
Compute the answer modulo 10^9+7 .      Constraints  1 ≤ N ≤ 10^5  0 ≤ a_i, b_i ≤ 10^9  The coordinates are integers.  The coordinates are pairwise distinct.         Input The input is given from Standard Input in the following format:  N  a_1 : a_N  b_1 : b_N       Output Print the number of ways to minimize the total length of the cables, modulo 10^9+7 .        Sample Input 1 2
0
10
20
30      Sample Output 1 2  There are two optimal connections: 0-20, 10-30 and 0-30, 10-20 .
In both connections the total length of the cables is 40 .       Sample Input 2 3
3
10
8
7
12
5      Sample Output 2 1

Solution (Python):
n = int(input())
mod = pow(10, 9) + 7
x = []
for i in range(2 * n):
    if i < n:
        x.append([int(input()), 0])
    else:
        x.append([int(input()), 1])
x.sort()
ans, cnt0, cnt1 = 1, 0, 0
for i in range(2 * n):
    if x[i][1] == 0:
        if cnt1 >= 1:
            ans *= cnt1
            ans %= mod
            cnt1 -= 1
        else:
            cnt0 += 1
    else:
        if cnt0 >= 1:
            ans *= cnt0
            ans %= mod
            cnt0 -= 1
        else:
            cnt1 += 1
print(ans)

Solution (Python):
n = int(input())
M = 10**9+7
L = []
for _ in range(n):
  a = int(input())
  L.append((a, 1))
for _ in range(n):
  b = int(input())
  L.append((b, 0))
L.sort()
C = [0, 0]
ans = 1
for d, f in L:
  if C[f^1]:
    ans *= C[f^1]
    ans %= M
    C[f^1] -= 1
  else:
    C[f] += 1
print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
n = int(input())
a = [(int(input()),1) for i in range(n)]
a += [(int(input()),2) for i in range(n)]
a.sort()
b = list(zip(*a))[1]
mod = 10**9+7
ans = 1
stack = []
for i in range(2*n):
  if not stack or stack[-1] == b[i]:
    stack.append(b[i])
  else:
    ans *= len(stack)
    ans %= mod
    stack.pop()
print(ans)

Solution (Python):
from collections import deque
N = int(input())

white = 0  
black = 0  

ans = 1

mod = 10**9 + 7
que = []
for i in range(N):
    x = int(input())
    que.append((x, 0))
for i in range(N):
    y = int(input())
    que.append((y, 1))
que.sort(reverse=True)
for i in range(2*N):
    value, index = que[i]
    if index == 0:
        white += 1
        if black > 0:
            ans *= black
            ans %= mod
            black -= 1
            white -= 1
    else:
        black += 1
        if white > 0:
            ans *= white
            ans %= mod
            white -= 1
            black -= 1
print(ans)

Solution (Python):
n = int(input())
a = [(int(input()),0) for i in range(n)]
b = [(int(input()),1) for i in range(n)]
diff = 0
ans = 1
mod = 10 ** 9 + 7
ab = sorted(a+b)
prev = 0
for x,now in ab:
  if prev != now:
    diff = -diff
  if diff > 0:
    ans *= diff
    ans %= mod
  diff -= 1
  prev = now
print(ans)

Solution (Python):
import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 7)
mod = 10 ** 9 + 7

N = int(input())
AB = []
for _ in range(N):
    AB.append((int(input()), 0))
for _ in range(N):
    AB.append((int(input()), 1))
AB.sort(reverse=True)

ans = 1
Aset = 0
Bset = 0

while AB:
    _, x = AB.pop()
    if x:
        if Bset:
            ans *= Bset
            ans %= mod
            Bset -= 1
        else:
            Aset += 1
    else:
        if Aset:
            ans *= Aset
            ans %= mod
            Aset -= 1
        else:
            Bset += 1

print(ans)

Solution (Python):

def calculate_cables_length(x: int, y: int, total: int) -> tuple:
    MOD = 10 ** 9 + 7
    if y:
        total = (total * y) % MOD
        y -= 1
    else:
        x += 1
    return x, y, total


def main():
    
    
    N, *AB = map(int, open(0))
    C = [(i, 1) for i in AB[:N]] + [(i, 0) for i in AB[N:]]
    C.sort()
    ans, a, b = 1, 0, 0
    for i, t in C:
        if t:  
            a, b, ans = calculate_cables_length(a, b, ans)
        else:  
            b, a, ans = calculate_cables_length(b, a, ans)
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
N = int(input())

mod = int(1e9+7)

A = []
for _ in range(N):
  A.append([int(input()),1])
for _ in range(N):
  A.append([int(input()),2])
A.sort()

ans = 1
ca,cb = 0,0
for a in A:
  if a[1] == 1:
    if cb == 0:
      ca += 1
    else:
      ans = ans * cb % mod
      cb -= 1      
  else:
    if ca == 0:
      cb += 1
    else:
      ans = ans * ca % mod
      ca -= 1
    
print(ans)

Solution (Python):
import sys
from operator import itemgetter

read = sys.stdin.read

N, *ab = map(int, read().split())
mod = 10 ** 9 + 7
ab = list(zip(ab, [0] * N + [1] * N))
ab.sort(key=itemgetter(0))

remain_pc = 0
remain_power = 0
answer = 1

for x, p in ab:
    if p == 0:
        if remain_power == 0:
            remain_pc += 1
            continue
        answer *= remain_power
        remain_power -= 1
    else:
        if remain_pc == 0:
            remain_power += 1
            continue
        answer *= remain_pc
        remain_pc -= 1
    answer %= mod

print(answer)

Solution (Python):
import sys
from operator import itemgetter

read = sys.stdin.read

N, *ab = map(int, read().split())
mod = 10 ** 9 + 7
ab = list(zip(ab[:N], [0] * N)) + list(zip(ab[N:], [1] * N))
ab.sort(key=itemgetter(0))

remain_pc = 0
remain_power = 0
answer = 1

for x, p in ab:
    if p == 0:
        if remain_power == 0:
            remain_pc += 1
            continue
        answer *= remain_power
        remain_power -= 1
    else:
        if remain_pc == 0:
            remain_power += 1
            continue
        answer *= remain_pc
        remain_pc -= 1
    answer %= mod

print(answer)

Solution (Python):
import sys

read = sys.stdin.read

N, *ab = map(int, read().split())
mod = 10 ** 9 + 7
ab = list(zip(ab[:N], [0] * N)) + list(zip(ab[N:], [1] * N))
ab.sort()

remain_pc = 0
remain_power = 0
answer = 1

for x, p in ab:
    if p == 0:
        if remain_power == 0:
            remain_pc += 1
            continue
        answer *= remain_power
        remain_power -= 1
    else:
        if remain_pc == 0:
            remain_power += 1
            continue
        answer *= remain_pc
        remain_pc -= 1
    answer %= mod

print(answer)

Solution (Python):
mod = 1000000007
N = int(input())
A = []
B = []
for i in range(N):
	A.append(int(input()))
for i in range(N):
	B.append(int(input()))

A.sort()
B.sort()
A.append(mod)
B.append(mod)

ans = 1
a = 0
b = 0

while a != N or b != N:
	if A[a] < B[b]:
		a += 1
		if a > b:
			ans = ans * (a - b) % mod
	else:
		b += 1
		if b > a:
			ans = ans * (b - a) % mod
print(ans)

Solution (Python):
import sys
input = sys.stdin.buffer.readline
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getlist():
    return list(map(int, input().split()))
import math
import heapq
from collections import defaultdict, Counter, deque
MOD = 10**9 + 7
INF = 10**15


def main():
    n = getN()
    line = []
    for i in range(n):
        line.append((getN(), "P"))
    for i in range(n):
        line.append((getN(), "C"))

    line.sort()
    pc = 0
    cn = 0
    ans = 1
    
    for point in line:
        pt, st = point
        if st == "P":
            if pc == 0 and cn != 0:
                ans *= cn
                ans %= MOD
                cn -= 1
            else:
                pc += 1

        else:
            if cn == 0 and pc != 0:
                ans *= pc
                ans %= MOD
                pc-=1
            else:
                cn += 1

    print(ans)



if __name__ == '__main__':
    main()

"""
9999
3

2916
"""

Solution (Python):
n = int(input())
a = [[int(input()),0] for _ in range(n)]
b = [[int(input()),1] for _ in range(n)]
mod = 10**9 + 7
ab = sorted(a+b)
ans = 1
now = [0,0]
for x,y in ab:
    if now[1-y] > 0:
        ans *= now[1-y]
        ans %= mod
        now[1-y] -= 1
    else:
        now[y] += 1

print(ans)

Solution (Python):
n=int(input())
a=[int(input())for _ in range(n)]+[10**9]
b=[int(input())for _ in range(n)]+[10**9]
a.sort(reverse=1)
b.sort(reverse=1)
aa=0
bb=0
if a[-1]>b[-1]:
  bb=1
  b.pop()
else:
  aa=1
  a.pop()
ans=1
while len(a)!=1 or len(b)!=1:
  if a[-1]>b[-1]:
    bb+=1
    b.pop()
  else:
    aa+=1
    a.pop()
  if aa*bb:
    ans*=max(aa,bb)
    ans%=10**9+7
    aa-=1
    bb-=1
print(ans)

Solution (Python):

N = int(input())
mod = 10**9+7

lis = []

for i in range(N):

    a = int(input())
    lis.append([a,1])

for i in range(N):

    b = int(input())
    lis.append([b,-1])

ans = 1

lis.sort()

now = 0
for i in range(2*N):

    nex = lis[i][1]

    if nex == 1:

        if now >= 0:
            now += 1
        else:
            ans *= abs(now)
            now += 1

    else:

        if now <= 0:
            now -= 1
        else:
            ans *= abs(now)
            now -= 1

    ans %= mod

print (ans)

Solution (Python):
MOD = 10 ** 9 + 7
def mod_fact(n):
  ret = 1
  for i in range(1, n + 1):
    ret = (ret * i) % MOD
  return ret

n = int(input())
AB = []
for i in range(2 * n):
  AB.append((int(input()), i//n)) 

AB.sort()

a, b = 0, 0
ans = 1
for i, j in AB:
  if j == 0:
    if b == 0:
      a += 1
    else:
      ans = (ans * b) % MOD
      b -= 1
  else:
    if a == 0:
      b += 1
    else:
      ans = (ans * a) % MOD 
      a -= 1
print(ans % MOD)

Solution (Python):
def main():
    from bisect import bisect_left as bl
    n = int(input())
    a = [int(input()) for _ in [0]*n]
    b = [int(input()) for _ in [0]*n]
    a.sort()
    b.sort()
    for i in range(n):
        if a[i] > b[i]:
            a[i], b[i] = b[i], a[i]
    
    
    mod = 10**9+7
    ans = 1
    for i in range(n):
        ans = ans*(i+1-bl(b, a[i])) % mod
    print(ans)


main()

Solution (Python):
import sys
input = sys.stdin.readline

mod = 10**9+7

N = int(input())
A = [int(input()) for _ in range(N)]
B = [int(input()) for _ in range(N)]

A.sort(reverse=True)
B.sort(reverse=True)

P = []
while A or B:
    if len(A) == 0:
        b = B.pop()
        P.append(0)
    elif len(B) == 0:
        a = A.pop()
        P.append(1)
    else:
        a = A.pop()
        b = B.pop()
        if a > b:
            P.append(0)
            A.append(a)
        else:
            P.append(1)
            B.append(b)

a = 0
b = 0
ans = 1
for p in P:
    if p == 0:
        if b > 0:
            ans = ans * b % mod
            b -= 1
        else:
            a += 1
    else:
        if a > 0:
            ans = ans * a % mod
            a -= 1
        else:
            b += 1
print(ans)

Solution (Python):
N = int(input())
Q = sorted([[int(input()), i//N] for i in range(2*N)])

mod = 10**9 + 7
ans = 1
S = [0, 0]

for i in Q:
    if S[1-i[1]] == 0:
        S[i[1]] += 1
    else:
        ans = (ans*S[1-i[1]]) % mod
        S[1-i[1]] -= 1

print(ans)

Solution (Python):
N = int(input())
AB = []
for _ in range(N):
  AB.append([int(input()),True])
for _ in range(N):
  AB.append([int(input()),False])
AB.sort(key = lambda x:x[0])

ans = 1
c = 0
for _,f in AB:
  if f:
    c += 1
  else:
    if c>0:
      ans *= c
      c -= 1
      ans %= 10**9+7
    else:
      c -= 1
      ans *= -1*c
      ans %= 10**9+7
print(ans)

Solution (Python):
MOD = 10**9 + 7
def solve(n, a, b):
    v = []
    for i in range(n):
        v.append((a[i], 1))
        v.append((b[i], -1))
    v = sorted(v, key=lambda _:_[0])
    ans, cnt = 1, 0
    for _, t in v:
        if (cnt != 0) and (cnt * t < 0):
            ans = (ans * abs(cnt)) % MOD
        cnt += t
    return ans

n = int(input())
a = [int(input()) for i in range(n)]
b = [int(input()) for i in range(n)]
print(solve(n, a, b))

Solution (Python):
from bisect import *
N=int(input())
A=[int(input()) for _ in range(N)]
B=[int(input()) for _ in range(N)]
A=sorted(A)
B=sorted(B)
ans=1
mod=10**9+7
for a,b in zip(A,B):
  if a>b:
    a,b=b,a
  ans*=(bisect_right(A,b)-bisect_left(A,a))
  ans%=mod
print(ans)

Solution (Python):

def calc(x, y, total):
    MOD = 10 ** 9 + 7
    if y:
        total = (total * y) % MOD
        y -= 1
    else:
        x += 1
    return x, y, total


def main():
    
    
    N, *AB = map(int, open(0))
    C = sorted([(i, 1) for i in AB[:N]] + [(i, 0) for i in AB[N:]])
    ans, a, b = 1, 0, 0
    for i, t in C:
        if t:  
            a, b, ans = calc(a, b, ans)
        else:  
            b, a, ans = calc(b, a, ans)
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):

def calc(x, y, total):
    MOD = 10 ** 9 + 7
    if y:
        total = (total * y) % MOD
        y -= 1
    else:
        x += 1
    return x, y, total


def main():
    
    
    N, *AB = map(int, open(0))
    C = sorted([(i, 1) for i in AB[:N]] + [(i, 0) for i in AB[N:]])
    ans, a, b = 1, 0, 0
    for i, t in C:
        if t:  
            a, b, ans = calc(a, b, ans)
        else:  
            b, a, ans = calc(b, a, ans)
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):

def calc(x, y, total):
    if y:
        total = (total * y) % MOD
        y -= 1
    else:
        x += 1
    return x, y, total


def main():
    global MOD
    
    
    N, *AB = map(int, open(0))
    C = sorted([(i, 1) for i in AB[:N]] + [(i, 0) for i in AB[N:]])
    MOD = 10 ** 9 + 7
    ans, a, b = 1, 0, 0
    for i, t in C:
        if t:  
            a, b, ans = calc(a, b, ans)
        else:  
            b, a, ans = calc(b, a, ans)
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
N,*E=map(int,open(0))
a,c=1,0
for _,d in sorted((e,2*(i<N)-1)for i,e in enumerate(E)):
	if c*d<0:a=abs(a*c)%(10**9+7)
	c+=d
print(a)

Solution (Python):
n,*a=map(int,open(0))
d={}
for i in a[:n]:d[i]=0
for i in a[n:]:d[i]=1
a,c=1,0
for i in sorted(d):
  if((c>0)!=d[i])*c:a=a*abs(c)%(10**9+7)
  c+=d[i]or-1
print(a)

Solution (Python):
n,*a=map(int,open(0))
d={}
for i in a[:n]:d[i]=0
for i in a[n:]:d[i]=1
a,c=1,0
for i in sorted(d):
  if((c>0)!=d[i])*c:a=a*abs(c)%(10**9+7)
  c+=d[i]or-1
print(a)

Solution (Python):
mod = 10**9+7

n = int(input())
s = []
for i in range(2):
    for j in range(n):
        a = int(input())
        s.append((a, i))
s.sort()

ans = 1
l = []
cur = None
for num, c in s:
    if not l:
        l.append(s[i])
        cur = c
    elif c == cur:
        l.append(num)
    else:
        ans *= len(l)
        ans %= mod
        l.pop()
print(ans)

Solution (Python):
n=int(input())
ans=1
mod=10**9+7
A=[0,0]
AB=[]
for i in range(n):
  a=int(input())
  AB.append((a,0))
for i in range(n):
  b=int(input())
  AB.append((b,1))
AB.sort(key=lambda x:x[0])
for i in range(2*n):
  a,b=AB[i]
  if b==0:
    if A[1]>0:
      ans=(ans*A[1])%mod
      A[1]-=1
    else:
      A[0]+=1
  else:
    if A[0]>0:
      ans=(ans*A[0])%mod
      A[0]-=1
    else:
      A[1]+=1
print(ans)

Solution (Python):
n=int(input())
ans=1
mod=10**9+7
A=[0,0]
AB=[]
for i in range(n):
  a=int(input())
  AB.append((a,0))
for i in range(n):
  b=int(input())
  AB.append((b,1))
AB.sort(key=lambda x:x[0])
for i in range(2*n):
  a,b=AB[i]
  if b==0:
    if A[1]>0:
      ans=(ans*A[1])%mod
      A[1]-=1
    else:
      A[0]+=1
  else:
    if A[0]>0:
      ans=(ans*A[0])%mod
      A[0]-=1
    else:
      A[1]+=1
print(ans)

Solution (Python):
import sys

n = int(input())
lines = sys.stdin.readlines()
aaa = list(map(int, lines[:n]))
bbb = list(map(int, lines[n:]))
coords = [(a, 0) for a in aaa] + [(b, 1) for b in bbb]
coords.sort()
MOD = 10 ** 9 + 7

remain_type = 0
remain_count = 0
ans = 1
for x, t in coords:
    if remain_type == t:
        remain_count += 1
        continue
    if remain_count == 0:
        remain_type = t
        remain_count = 1
        continue
    ans = ans * remain_count % MOD
    remain_count -= 1
print(ans)

Solution (Python):
from collections import deque
import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline
import bisect
def isprime(n):
  if n <= 1:
    return False
  else:
    for i in range(2,(int(n**0.5)+2)):
      if n%i == 0:
        return False
    return True

def solve():
  n = int(input())
  a = sorted([int(input()) for _ in range(n)])+[10**9 + 1]
  b = sorted([int(input()) for _ in range(n)])+[10**9 + 1]
  mod = 10**9+7
  ans = 1
  if n == 1:
    print(1)
  else:
    na,nb = 0,0
    for i in range(2*n):
      if na>nb and a[na]>b[nb]:
        ans *= na-nb
        ans %= mod
      elif na<nb and a[na]<b[nb]:
        ans *= nb-na
        ans %= mod
      
      if a[na] < b[nb]:na += 1
      else:nb += 1
  print(ans)
      
    
solve()

Solution (Python):
N,*E=map(int,open(0).read().split())
a,c=1,0
for _,d in sorted((e,2*(i<N)-1)for i,e in enumerate(E)):
	if c*d<0:a=abs(a*c)%(10**9+7)
	c+=d
print(a)

Solution (Python):
N,*E=map(int,open(0).read().split())
E=sorted((e,2*(i<N)-1)for i,e in enumerate(E))
a,c=1,0
for _,d in E:
	if c*d<0:a=abs(a*c)%(10**9+7)
	c+=d
print(a)

Solution (Python):
N,*E=map(int,open(0).read().split())
E=sorted((e,2*(i<N)-1)for i,e in enumerate(E))
a,c=1,0
for _,d in E:
	if c*d<0:a=abs(a*c)%(10**9+7)
	c+=d
print(a)

Solution (Python):
N,*E=map(int,open(0).read().split())
E=sorted((e,2*(i<N)-1)for i,e in enumerate(E))
a,c=1,0
for _,d in E:
	if c*d<0:a=(a*abs(c))%(10**9+7)
	c+=d
print(a)

Solution (Python):
N,*E=map(int,open(0).read().split())
E=sorted((e,2*(i<N)-1)for i,e in enumerate(E))
a,c=1,0
for _,d in E:
	if c*d<0:
		a=(a*abs(c))%(10**9+7)
	c+=d
print(a)

Solution (Python):
mod = 10 ** 9 + 7

N, *E = map(int, open(0).read().split())
E = sorted((e, 2 * (i < N) - 1) for i, e in enumerate(E))

res = 1
cnt = 0
for _, delta in E:
    if cnt * delta < 0:
        res *= abs(cnt)
        res %= mod
    cnt += delta

print(res)

Solution (Python):
mod = 10 ** 9 + 7

N, *E = map(int, open(0).read().split())
E = sorted((e, 1 if i < N else -1) for i, e in enumerate(E))

res = 1
cnt = 0
for _, delta in E:
    if cnt * delta < 0:
        res *= abs(cnt)
        res %= mod
    cnt += delta

print(res)

Solution (Python):
from heapq import heappush, heappop

mod = 10 ** 9 + 7

N, *E = map(int, open(0).read().split())
E = sorted((e, 1 if i < N else -1) for i, e in enumerate(E))

res = 1
cnt = 0
for _, delta in E:
    if cnt * delta < 0:
        res *= abs(cnt)
        res %= mod
    cnt += delta

print(res)

Solution (Python):
from heapq import heappush, heappop

mod = 10 ** 9 + 7

N, *E = map(int, open(0).read().split())
Q = []
for i, e in enumerate(E):
    heappush(Q, (e, 1 if i < N else -1))

res = 1
cnt = 0
while Q:
    _, delta = heappop(Q)
    if cnt * delta < 0:
        res *= abs(cnt)
        res %= mod
    cnt += delta

print(res)

Solution (Python):
mod = 10 ** 9 + 7

N = int(input())
A = [(int(input()), 1) for _ in range(N)]
B = [(int(input()), -1) for _ in range(N)]

C = sorted(A + B)

res = 1
cnt = 0
for _, delta in C:
    if cnt * delta < 0:
        res *= abs(cnt)
        res %= mod
    cnt += delta

print(res)

Solution (Python):
mod = 10 ** 9 + 7

N = int(input())
A = [(int(input()), -1) for _ in range(N)]
B = [(int(input()), 1) for _ in range(N)]

C = sorted(A + B)

res = 1
cnt = 0
for _, delta in C:
    if cnt != 0 and cnt * delta < 0:
        res *= abs(cnt)
        res %= mod
    cnt += delta

print(res)

Solution (Python):
mod = 10 ** 9 + 7

N = int(input())
A = [(int(input()), -1) for _ in range(N)]
B = [(int(input()), 1) for _ in range(N)]

C = sorted(A + B)

res = 1
cnt = 0
for _, delta in C:
    if cnt != 0 and (cnt > 0) != (delta > 0):
        res *= abs(cnt)
        res %= mod
    cnt += delta

print(res)

Solution (Python):
mod = 10 ** 9 + 7

N = int(input())
A = [(int(input()), False) for _ in range(N)]
B = [(int(input()), True) for _ in range(N)]

C = sorted(A + B)

res = 1
cnt = 0
for _, ab in C:
    delta = 1 if ab else -1
    if cnt != 0 and (cnt > 0) != ab:
        res *= abs(cnt)
        res %= mod
    cnt += delta

print(res)

Solution (Python):
mod = 10 ** 9 + 7
INF = 10 ** 10

N = int(input())
A = [int(input()) for _ in range(N)] + [INF]
B = [int(input()) for _ in range(N)] + [INF]

A.sort()
B.sort()

ans = 1
a = b = 0
i = j = 0
while i < N or j < N:
    if A[i] < B[j]:
        if b > 0:
            ans *= b
            b -= 1
        else:
            a += 1
        i += 1
    else:
        if a > 0:
            ans *= a
            a -= 1
        else:
            b += 1
        j += 1

    ans %= mod

print(ans)

Solution (Python):
n = int(input())
a = [(int(input()), "a") for i in range(n)]
b = [(int(input()), "b") for i in range(n)]
li = sorted((a+b))
MOD = 10**9 + 7

def solve(li):
    ans = 1
    for i in range(len(li) - 1):
        if abs(li[i]) < abs(li[i+1]):
            ans *= abs(li[i+1])
            ans %= MOD
    return ans

num = 0
ans = 1
tmp = []
for i in range(2*n):
    if li[i][1] == "a":
        num += 1
    else:
        num -= 1
    if num == 0:
        ans *= solve(tmp)
        ans %= MOD
        tmp = []
    else:
        tmp.append(num)
print(ans)

Solution (Python):
from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,copy,time
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())

N = inp()
ab = []

for i in range(N):
    a = inp()
    ab.append([a,1])

for i in range(N):
    b = inp()
    ab.append([b,-1])

ab.sort()
cnt = 0
ans = 1
for x,k in ab:
    cnt += k
    if (cnt > 0) and (k > 0):
        ans *= cnt
    elif (cnt < 0) and (k < 0):
        ans *= -cnt
    ans %= mod
    
print(ans%mod)

Solution (Python):
N = int(input())
MOD = 10 ** 9 + 7
A = []
B = []
for i in range(N):
    A.append((int(input()), 0))

for i in range(N):
    B.append((int(input()), 1))
D = A + B
D = sorted(D)

a = 0
b = 0
ans = 1
for x, i in D:
    if i == 0:
        if b == 0:
            a += 1
        else:
            ans *= b
            b -= 1
    else:
        if a == 0:
            b += 1
        else:
            ans *= a
            a -= 1
    ans %= MOD
print(ans)

Solution (Python):
n=int(input())
arr=[(int(input()),i//n) for i in range(2*n)]
arr=sorted(arr,key=lambda x:x[0])
mod=10**9+7
ans=1
cnta=0
cntb=0
for pos,flag in arr:
  if flag==0:
    if cntb==0:
      cnta+=1
    else:
      ans*=cntb
      ans%=mod
      cntb-=1
  else:
    if cnta==0:
      cntb+=1
    else:
      ans*=cnta
      ans%=mod
      cnta-=1
print(ans)

Solution (Python):

import sys
input = sys.stdin.readline

def calc(x, y, total):
    MOD = 10 ** 9 + 7
    if y:
        total = (total * y) % MOD
        y -= 1
    else:
        x += 1
    return x, y, total

def main():
    N = int(input())
    A = tuple(map(int, [input() for _ in range(N)]))
    B = tuple(map(int, [input() for _ in range(N)]))
    C = sorted([(i, 1) for i in A] + [(i, 0) for i in B])
    ans, a, b = 1, 0, 0
    for i, t in C:
        if t:  
            a, b, ans = calc(a, b, ans)
        else:  
            b, a, ans = calc(b, a, ans)
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
N = int(input())
E = []
for _ in range(N):
    E += [(int(input()), 1)]
for _ in range(N):
    E += [(int(input()), -1)]
E.sort()
mod = 10**9 + 7
ans = 1
ab = 0
for e in E:
    if e[1] * ab < 0:
        ans *= abs(ab)
        ans %= mod
    ab += e[1]
print(ans)

Solution (Python):
N=int(input())
A=sorted(int(input())for _ in'_'*N)
A+=10**10,
B=sorted(int(input())for _ in'_'*N)
B+=10**10,
z=1
a=b=i=j=0
M=10**9+7
while i<N or j<N:
	if A[i]<B[j]:
		if b:
			z=z*b%M
			b-=1
		else:
			a+=1
		i+=1
	else:
		if a:
			z=z*a%M
			a-=1
		else:
			b+=1
		j+=1
print(z)

Solution (Python):
p=1
m=10**9+7
s=[0]*2
n=int(input())
q=sorted([[int(input()),i//n]for i in range(2*n)])
for i in q:
    if s[1-i[1]]==0:
        s[i[1]]+=1
    else:
        p=(p*s[1-i[1]])%m
        s[1-i[1]]-=1
print(p)

Solution (Python):
from heapq import heappush, heappop

mod = 10 ** 9 + 7

n = int(input())
a = [int(input()) for _ in range(n)]
b = [int(input()) for _ in range(n)]

h = []
for aa in a:
    heappush(h, (aa, True))
for bb in b:
    heappush(h, (bb, False))

ca = 0
cb = 0
ans = 1
while h:
    pos, is_elm_of_a = heappop(h)
    if is_elm_of_a:
        if cb > 0:
            ans *= cb
            ans %= mod
            cb -= 1
        else:
            ca += 1
    else:
        if ca > 0:
            ans *= ca
            ans %= mod
            ca -= 1
        else:
            cb += 1
print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

MOD = 10 ** 9 + 7

N = int(input())
A,B = [[int(input()) for _ in range(N)] for _ in [0,1]]

AB = [(x,0) for x in A] + [(x,1) for x in B]
AB.sort()

a = 0
b = 0
answer = 1
for x,i in AB:
    if i == 0:
        if b == 0:
            a += 1
            continue
        answer *= b
        b -= 1
    else:
        if a == 0:
            b += 1
            continue
        answer *= a
        a -= 1
    answer %= MOD

print(answer)

Solution (Python):
from operator import itemgetter
import sys
input = sys.stdin.readline

MOD = int(1e9+7)


def main():
    N = int(input())
    P = [None] * N * 2
    for i in range(N):
        a = int(input())
        P[i] = (a, 0)
    for i in range(N):
        b = int(input())
        P[i+N] = (b, 1)
    P.sort(itemgetter(0))
    cand = [0] * 2
    ans = 1
    for p in P:
        cur = p[1]
        peer = cur ^ 1
        if cand[peer]:
            ans = (ans * cand[peer]) % MOD
            cand[peer] -= 1
        else:
            cand[cur] += 1
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
N = int(input())
*A, = [(int(input()),1) for _ in [0]*N]
*B, = [(int(input()),-1) for _ in [0]*N]

mod = 10**9+7
fact = [1]*(N+1)
for i in range(N):
    fact[i+1] = fact[i]*(i+1)%mod

AB = A+B
AB.sort()
ans = 1
c = 0
d = 0
for l,ab in AB:
    if d*ab<0:
        ans *= abs(d)
        ans %= mod
    d += ab
    c += 1
print(ans)

Solution (Python):
MOD = 10**9 + 7
n = int(input())
nodes = []
for i in range(n):
    nodes.append([int(input()), 0])
for i in range(n):
    nodes.append([int(input()), 1])

nodes.sort()
ans = 1
cnt = 0
state = 0
for x, label in nodes:
    if cnt == 0:
        cnt += 1
        state = label
    else:
        if state == label:
            cnt += 1
        else:
            ans *= cnt
            ans %= MOD
            cnt -= 1
print(ans)

Solution (Python):
MOD = 10**9 + 7
n = int(input())
a = []
for _ in range(n):
	x = int(input())
	a.append([x, 1])
for _ in range(n):
	x = int(input())
	a.append([x, -1])
a.sort()
ans = 1
tmp = 0
for i in range(2*n):
	x = a[i][1]
	if x == 1 and tmp < 0:
		ans *= -tmp
	elif x == -1 and tmp > 0:
		ans *= tmp
	tmp += x
	ans %= MOD
print(ans)

Solution (Python):
n = int(input())
pat = 1
mod = 10**9+7
l = []
for _ in range(n):
  a = int(input())
  l.append((a,0))
for _ in range(n):
  a = int(input())
  l.append((a,1))
l.sort()
cur = {0:0, 1:0}
for x in l:
  if cur[1-x[1]]:
    pat = pat*cur[1-x[1]]%mod
    cur[1-x[1]] -= 1
  else:
    cur[x[1]] += 1
print(pat)

Solution (Python):


import sys
from collections import Counter

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

N=INT()
AB=[(INT(), 'pc') for i in range(N)]+[(INT(), 'plug') for i in range(N)]
AB.sort()

C=Counter()
ans=1
for i in range(N*2):
    a,_type=AB[i]
    if C['pc']==C['plug']:
        C[_type]+=1
    elif C['pc']>C['plug']:
        if _type=='pc':
            C['pc']+=1
        else:
            ans=(ans*C['pc'])%MOD
            C['pc']-=1
    elif C['pc']<C['plug']:
        if _type=='plug':
            C['plug']+=1
        else:
            ans=(ans*C['plug'])%MOD
            C['plug']-=1
print(ans)

Solution (Python):
n = int(input())
alist = list(reversed(sorted([int(input()) for _ in range(n)])))
blist = list(reversed(sorted([int(input()) for _ in range(n)])))
INF = pow(10, 9) + 7
a = alist.pop()
b = blist.pop()
aset, bset = 0, 0
ans = 1
while len(alist) > 0 or len(blist) > 0:
  if a < b:
    if bset > 0:
      ans = (ans * bset) % INF
      bset -= 1
    else:
      aset += 1
    a = INF
    if len(alist) > 0:
      a = alist.pop()
  else:
    if aset > 0:
      ans = (ans * aset) % INF
      aset -= 1
    else:
      bset += 1
    b = INF
    if len(blist) > 0:
      b = blist.pop()
print(ans % INF)

Solution (Python):
import sys
input=sys.stdin.readline
N=int(input())
data=[]
ans=1
mod=10**9+7
for i in range(N):
    data.append(('a',int(input())))
for i in range(N):
    data.append(('b',int(input())))
cnt=0
bef=data[0][0]
data.sort(key=lambda x:x[1])
for d in data:
    if cnt==0:
        bef=d[0]
        cnt+=1
    elif bef==d[0]:
        cnt+=1
    elif bef!=d[0]:
        ans=(ans*cnt)%mod
        cnt-=1
print(ans)

Solution (Python):
N = int(input())
A = [int(input()) for _ in range(N)]
B = [int(input()) for _ in range(N)]
mod = 10**9+7

from bisect import bisect_right as br
from bisect import bisect_left  as bl

A.sort(reverse=True)
B.sort(reverse=True)


ans = 1

def mbr(ls, a):
    ok = N
    ng = 0
    mid = (ok+ng)//2
    while abs(ok-ng) > 1:
        if ls[mid] >= a:
            ng = mid
        else:
            ok = mid
        mid = (ok+ng)//2

    return ok

for i in range(N):
    a,b = A[i], B[i]
    if a > b:
        idx = mbr(A,b)
        ans = ans * (idx-i) % mod
    else:
        idx = mbr(B,a)
        ans = ans * (idx-i) % mod
print( ans % mod)

Solution (Python):
import bisect

N = int(input())
a = []
b = []
bb = []

for i in range(N):
	a.append(int(input()))

for i in range(N):
	b.append(int(input()))

a = sorted(a)
b = sorted(b)

for i in range(N):
	bb.append(bisect.bisect_left(a, b[i]))

ans = 1
for i in range(N):
	if bb[i] - i != 0:
		if bb[i] > i:
			ans = ans * (bb[i] - i)
		else:
			ans = ans * (i - bb[i] + 1)
		ans = ans % 1000000007

print(ans)

Solution (Python):
N = int(input())
A = [int(input()) for n in range(N)]
B = [int(input()) for n in range(N)]
A.sort()
B.sort()
A.append(10**10) 
B.append(10**10) 
mod = 7 + 10**9
ANU, BNU = 0, 0 
Total = 1
 
ai, bi = 0, 0
if A[0] < B[0]:
    ANU += 1
    ai += 1
else:
    BNU += 1
    bi += 1
while ai < N or bi < N:
    if A[ai] < B[bi]:
        if BNU == 0:
            ANU += 1
        else:
            Total *= BNU
            Total %= mod
            BNU -= 1
        ai += 1
    else:
        if ANU == 0:
            BNU += 1
        else:
            Total *= ANU
            Total %= mod
            ANU -= 1
        bi += 1
print(Total)

Solution (Python):
import sys

MOD = 10**9 + 7

N = int(input())
As = [int(sys.stdin.readline()) for _ in range(N)] + [10**9 + 1]
Bs = [int(sys.stdin.readline()) for _ in range(N)] + [10**9 + 1]

As.sort()
Bs.sort()

ans = 1
iA = iB = 0
for _ in range(2*N):
    if iA > iB and As[iA] > Bs[iB]:
        ans *= iA - iB
        ans %= MOD
    elif iA < iB and As[iA] < Bs[iB]:
        ans *= iB - iA
        ans %= MOD

    if As[iA] < Bs[iB]:
        iA += 1
    else:
        iB += 1

print(ans)