Problem:


Score : 500 points

Problem Statement

   #nck {
      width: 30px;
      height: auto;
   }

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



Sample input:
2
0
10
20
30


Sample output:
2

Submission status: Wrong Answer

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

a.sort()
b.sort()
ans=sum(abs(a[i]-b[i]) for i in range(n))
print(ans)

Submission status: Wrong Answer

Solution code:
def factorial(n, mod):
    ans = 1
    while not n == 0:
        ans *= n
        ans %= mod
        n -= 1
    return ans 

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, cnt1, cnt2 = 1, 0, 0
for i in range(2 * n):
    cnt1 += x[i][1]
    cnt2 += 1
    if i % 2 == 1:
        if 2 * cnt1 == cnt2:
            ans *= factorial(cnt1, mod)
            ans %= mod
            cnt1, cnt2 = 0, 0
print(ans)

Submission status: Wrong Answer

Solution code:
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
fctr = [1]
for i in range(1,2*10**5+10):
  fctr.append(fctr[-1]*i%mod)
x = []
for i in range(2*n):
  if not x or b[i] != x[-1][0]:
    x.append([b[i],1])
  else:
    x[-1][1] += 1
stack = []
while x:
  s,q = x.pop()
  if not stack:
    stack.append([s,q])
  elif stack[-1][0] == s:
    stack[-1][1] += q
  elif stack[-1][1] > q:
    stack[-1][1] -= q
    ans *= fctr[q]
    ans %= mod
  else:
    q -= stack[-1][1]
    ans *= fctr[stack[-1][1]]
    ans %= mod
    stack.pop()
    if q:
      stack.append([s,q])
print(ans)

Submission status: Wrong Answer

Solution code:
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
cnt = 0
ans = 1
prv = -1
for i in range(2*n):
  if b[i] == 1:
    cnt += 1
  else:
    cnt -= 1
  if cnt == 0:
    ans *= ((i-prv)//2)**2
    ans %= mod
    prv = i
print(ans)

Submission status: Wrong Answer

Solution code:
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
cnt = 0
ans = 0
prv = -1
for i in range(2*n):
  if b[i] == 1:
    cnt += 1
  else:
    cnt -= 1
  if cnt == 0:
    ans += ((i-prv)//2)**2
    ans %= mod
    prv = i
print(ans)

Submission status: Wrong Answer

Solution code:
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
cnt = 0
ans = 1
prv = -1
for i in range(2*n):
  if b[i] == 1:
    cnt += 1
  else:
    cnt -= 1
  if cnt == 0:
    ans += ((i-prv)//2)**2
    ans %= mod
    prv = i
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque
N = int(input())
a = [int(input()) for i in range(N)]
b = [int(input()) for i in range(N)]

mod = 10**9+7

a.sort()
b.sort()


a = deque(a)
b = deque(b)

ans = 1

power = [1 for i in range(10**5+1)]
for i in range(1, 10**5+1):
    power[i] = power[i-1]*i % mod

cnt = 0
while a and b:
    if b[-1] > a[-1]:
        while b and b[-1] > a[-1]:
            b.pop()
            cnt += 1
        ans *= power[cnt] % mod
        for _ in range(cnt):
            a.pop()
    else:
        while a and a[-1] > b[-1]:
            a.pop()
            cnt += 1
        ans *= power[cnt] % mod
        for _ in range(cnt):
            b.pop()
    cnt = 0
print(ans % mod)

Submission status: Wrong Answer

Solution code:
from collections import deque
N = int(input())
a = [int(input()) for i in range(N)]
b = [int(input()) for i in range(N)]

mod = 10**9+7

a.sort()
b.sort()


a = deque(a)
b = deque(b)

ans = 1

power = [1 for i in range(10**5+1)]
for i in range(1, 10**5+1):
    power[i] = power[i-1]*i % mod

cnt = 0
while a and b:

    while b and b[-1] > a[-1]:
        b.pop()
        cnt += 1
        ans *= power[cnt] % mod
    if cnt == 0:
        a.pop()
        b.pop()
    for _ in range(cnt):
        a.pop()
    cnt = 0
print(ans % mod)

Submission status: Wrong Answer

Solution code:
MOD = 10 ** 9 + 7
fac = [1] * (10 ** 5 + 10)
for i in range(len(fac) - 1):
    fac[i + 1] = fac[i] * (i + 1) % MOD
n = int(input())
a = [(int(input()), 0) for _ in range(n)]
b = [(int(input()), 1) for _ in range(n)]
l = sorted(a + b)
a = 0
b = 0
li = [0]
for i, t in enumerate(l):
    p, x = t
    if x:
        b += 1
    else:
        a += 1
    if a == b:
        li.append(i + 1)
ans = 1
for i in range(len(li) - 1):
    ans *= fac[(li[i + 1] - li[i]) // 2]
    ans %= MOD
print(ans)

Submission status: Wrong Answer

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

fact = [1]
for i in range(1, 10 ** 5 + 10):
    fact.append(fact[-1] * i % mod)

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)

X, Y = 0, 0
ans = 1
while A and B:
    if X:
        if A[-1] < B[-1]:
            A.pop()
            X += 1
        else:
            ans = ans * fact[X] % mod
            for _ in range(X):
                B.pop()
            X = 0
    elif Y:
        if A[-1] > B[-1]:
            B.pop()
            Y += 1
        else:
            ans = ans * fact[Y] % mod
            for _ in range(Y):
                A.pop()
            Y = 0
    else:
        if A[-1] > B[-1]:
            B.pop()
            Y = 1
        else:
            A.pop()
            X = 1

if X:
    ans = ans * fact[X] % mod
if Y:
    ans = ans * fact[Y] % mod
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
N = int(input())
mod = 10 ** 9 + 7
a = [0] * N
b = [0] * N
for i in range(N): a[i] = int(input())
for i in range(N): b[i] = int(input())
a.sort()
b.sort()
da = [0] * (N - 1)
db = [0] * (N - 1)
for i in range(N - 1): da[i] = a[i + 1] - a[i]
for i in range(N - 1): db[i] = b[i + 1] - b[i]

class Factorial:
  def __init__(self, n, mod):
    self.f = [1]
    for i in range(1, n + 1):
      self.f.append(self.f[-1] * i % mod)
    self.i = [pow(self.f[-1], mod - 2, mod)]
    for i in range(1, n + 1)[: : -1]:
      self.i.append(self.i[-1] * i % mod)
    self.i.reverse()
  def factorial(self, i):
    return self.f[i]
  def ifactorial(self, i):
    return self.i[i]
  def combi(self, n, k):
    return self.f[n] * self.i[n - k] % mod * self.i[k] % mod

f = Factorial(N, mod)
res = 1
k = 1
for i in range(N - 1):
  if da[i] == db[i]:
    k += 1
  else:
    res *= f.factorial(k)
    res %= mod
    k = 1
res *= f.factorial(k)
res %= mod
print(res)

Submission status: Wrong Answer

Solution code:
N = int(input())

mod = int(1e9+7)

def cmb(n, r):
    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)
    return fact[n] * factinv[r] * factinv[n-r] % mod

NMAX = 2*N + 5
fact = [0 for _ in range(NMAX)]
factinv = [0 for _ in range(NMAX)]
inv = [0 for _ in range(NMAX)]

fact[0:2] = [1, 1]  # fact[n] = (n! mod p)
factinv[0:2] = [1, 1]  # factinv[n] = ((n!)^(-1) mod p)
inv[0:2] = [0, 1]  # factinv 計算用
 
for i in range(2, NMAX):
  fact[i] = (fact[i-1] * i) % mod
  inv[i] = (-inv[mod % i] * (mod // i)) % mod
  factinv[i] = (factinv[i-1] * inv[i]) % mod

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
prevN = 0
for a in A:
  if a[1] == 1:
    ca += 1
  else:
    cb += 1
  if ca == cb:

    wk = ca - prevN
    ans = ans * fact[wk] % mod

    prevN = ca
    
print(ans)

Submission status: Wrong Answer

Solution code:
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()
print(0)

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np

read = sys.stdin.read

N, *ab = map(int, read().split())
mod = 10 ** 9 + 7
a = np.array(ab[:N], np.int64)
b = np.array(ab[N:], np.int64)

a.sort()
b.sort()
idx = np.searchsorted(b, a)
print(0)

Submission status: Wrong Answer

Solution code:
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
    # print(line)
    for point in line:
        pt, st = point
        if st == "P":
            if pc == 0 and cn != 0:
                ans *= cn
                cn -= 1
            else:
                pc += 1

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

    print(ans)



if __name__ == '__main__':
    main()

"""
9999
3

2916
"""

Submission status: Wrong Answer

Solution code:
n=int(input())
a=[int(input())for _ in range(n)]
b=[int(input())for _ in range(n)]
a.sort()
b.sort()
ai=0
bi=0
ans=1
while ai!=n or bi!=n:
  if ai==n:
    bi+=1
  elif bi==n:
    ai+=1
  else:
    if a[ai]>b[bi]:
      bi+=1
    else:
      ai+=1
  ans*=max(ai-bi,bi-ai,1)
  ans%=10**9+7
print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=[int(input())for _ in range(n)]
b=[int(input())for _ in range(n)]
a.sort()
b.sort()
ai=0
bi=0
ans=1
while ai!=n or bi!=n:
  if ai==n:
    bi+=1
  elif bi==n:
    ai+=1
  else:
    if a[ai]>b[bi]:
      bi+=1
    else:
      ai+=1
  ans*=max(ai-bi,bi-ai,1)
print(ans)

Submission status: Wrong Answer

Solution code:
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())
A = [int(input()) for _ in range(n)]
B = [int(input()) for _ in range(n)]
# a<b または b>a が続く区間ごとに要素の数xを数えて,x!の積を取る
A.sort()
B.sort()
d = A[0] > B[0] # a, bの大小関係
elm_num = 1

ans = 1
for a, b in zip(A[1:], B[1:]):
  pd = a > b
  if d and pd:
    elm_num += 1
  elif d and not pd:
    ans *= mod_fact(elm_num) % MOD
    d = pd
    elm_num = 1
  elif not d and not pd:
    elm_num += 1
  else:
    d = pd
    ans *= mod_fact(elm_num) % MOD
    elm_num = 1
ans *= mod_fact(elm_num) % MOD
print(ans % MOD)

Submission status: Wrong Answer

Solution code:
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:
        cnt += t
        if cnt != 0:
            ans = (ans * abs(cnt)) % MOD
    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))

Submission status: Wrong Answer

Solution code:
MOD = 10**9 + 7

def solve(n, a, b):
    v = []
    for i in range(n):
        v.append((a[i], True))
        v.append((b[i], False))
    v = sorted(v, key=lambda _:_[0])
    dp = [0] * (n + 1)
    dp[0] = 1
    for i in range(n):
        dp[i+1] = (dp[i] * (i+1)) % MOD
    num_a, num_b = 0, 0
    ans = 1
    for _, t in v:
        if t:
            num_a += 1
        else:
            num_b += 1
        if (num_a > 0) and (num_a == num_b):
            ans = (ans * dp[num_a]) % MOD
            num_a, num_b = 0, 0
    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))

Submission status: Wrong Answer

Solution code:
n,*a=map(int,open(0))
d={}
for i in a[:n]:d[i]=-1
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]
print(a)

Submission status: Wrong Answer

Solution code:
n,*a=map(int,open(0))
d={}
for i in a[:n]:d[i]=1
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]
print(a)

Submission status: Wrong Answer

Solution code:
from math import factorial
mod = 10**9+7

n = int(input())
a = [int(input()) for i in range(n)]
b = [int(input()) for i in range(n)]
a.sort()
b.sort()

if a[-1] < b[-1]:
    a,b = b,a

ans = 1
while a:
    cnt = 0
    bot = b[-1]
    while a and bot < a[-1]:
        a.pop()
        cnt += 1
    ans *= factorial(cnt)
    ans %= mod
    for i in range(cnt):
        b.pop()
    a,b = b,a

print(ans)

Submission status: Wrong Answer

Solution code:
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*=A[1]
      A[1]-=1
    else:
      A[0]+=1
  else:
    if A[0]>0:
      ans*=A[0]
      A[0]-=1
    else:
      A[1]+=1
print(ans)

Submission status: Wrong Answer

Solution code:
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)

Submission status: Wrong Answer

Solution code:
from heapq import heappush

mod = 10 ** 9 + 7

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

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

print(res)

Submission status: Wrong Answer

Solution code:
from heapq import heappush

mod = 10 ** 9 + 7

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

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

print(res)

Submission status: Wrong Answer

Solution code:
from functools import lru_cache

mod = 10 ** 9 + 7

@lru_cache(maxsize=10 ** 5 + 10)
def factorial(n):
    res = 1
    for i in range(2, n + 1):
        res *= i
        res %= mod
    return res

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

C = sorted(A + B)

res = 1
cnt = 0
a = b = 0
for _, ab in C:

    if ab == "A":
        a += 1
    else:
        b += 1

    if a != 0 and a == b:
        res *= factorial(a)
        res %= mod
        a = b = 0

print(res)

Submission status: Wrong Answer

Solution code:
from functools import lru_cache

mod = 10 ** 9 + 7

@lru_cache(maxsize=10 ** 5 + 10)
def factorial(n):
    res = 1
    for i in range(2, n + 1):
        res *= i
        res %= mod
    return res

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

C = sorted(A + B)

res = 1
cnt = 0
a = b = 0
for _, ab in C:

    if ab == "A":
        a += 1
    else:
        b += 1

    if a == b:
        res *= factorial(a)
        res %= mod
        a = b = 0

print(res)

Submission status: Wrong Answer

Solution code:
from functools import lru_cache

mod = 10 ** 9 + 7

@lru_cache(maxsize=10 ** 5 + 10)
def factorial(n):
    res = 1
    for i in range(2, n + 1):
        res *= i
        res %= mod
    return res

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:
        res *= factorial(abs(cnt))
        res %= mod
    cnt += delta

print(res)

Submission status: Wrong Answer

Solution code:
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)

ans = 1
acc = 0
for _, ab in C:
    if acc != 0 and (acc > 0) != ab:
        ans *= abs(acc)
        ans %= mod
    acc += ab

print(ans)

Submission status: Wrong Answer

Solution code:
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)

ans = 1
acc = 0
for _, ab in C:
    if acc != 0 and (acc > 0) != ab:
        ans *= abs(acc)
        ans %= mod
    acc += ab

print(ans)

Submission status: Wrong Answer

Solution code:
from functools import lru_cache

mod = 10 ** 9 + 7

@lru_cache(maxsize=10010)
def factorial(n):
    res = 1
    for i in range(1, n + 1):
        res *= i
        res %= mod
    return res

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

C = sorted(A + B)

ans = 1
a = b = 0
for _, ab in C:
    if ab == "A":
        a += 1
    else:
        b += 1

    if a == b:
        ans *= factorial(a)
        a = b = 0

print(ans)

Submission status: Wrong Answer

Solution code:
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:
      cntb-=1
  else:
    if cnta>=1:
      ans*=cnta
      ans%=mod
      cnta-=1
    else:
      cntb+=1
print(ans)

Submission status: Wrong Answer

Solution code:
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:
        if cand[p[1] ^ 1]:
            ans *= (cand[p[1] ^ 1]) % MOD
            cand[p[1] ^ 1] -= 1
        else:
            cand[p[1]] += 1
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline


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()
    cand0 = 0
    cand1 = 0
    ans = 1
    mn = P[0][1]
    for i in range(2*N):
        if P[i][1] == 0:
            if cand1:
                ans *= cand1
                cand1 -= 1
            else:
                cand0 += 1
        else:
            if cand0:
                ans *= cand0
                cand0 -= 1
            else:
                cand1 += 1
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
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:
    d += ab
    c += 1
    if d==0:
        ans *= fact[c//2]
        ans %= mod
        c=0
print(ans)

Submission status: Wrong Answer

Solution code:
import math
MOD = 10**9 + 7
n = int(input())
a_s = []; b_s = []
for i in range(n):
    a_s.append(int(input()))
for i in range(n):
    b_s.append(int(input()))
a_s.sort()
b_s.sort()
match = []
a_itr = 0; b_itr = 0
while True:
    cnt = 0
    if a_s[a_itr] < b_s[b_itr]:
        while a_itr < n and a_s[a_itr] <= b_s[b_itr]:
            a_itr += 1
            cnt += 1
        match.append(cnt)
        b_itr += cnt
    elif b_s[b_itr] < a_s[a_itr]:
        while b_itr < n and b_s[b_itr] <= a_s[a_itr]:
            b_itr += 1
            cnt += 1
        match.append(cnt)
        a_itr += cnt
    elif b_s[b_itr] == a_s[a_itr]:
        a_itr += 1
        b_itr += 1
    if a_itr >= n or b_itr >= n:
        break
    
ans = 1
cnt = 0
for item in match:
    ans *= math.factorial(item) % MOD
    ans %= MOD
print(ans)

Submission status: Wrong Answer

Solution code:
MOD = 10**9 + 7
n = int(input())
a_s = []; b_s = []
for i in range(n):
    a_s.append(int(input()))
for i in range(n):
    b_s.append(int(input()))
match = [0] * (n - 1)
for i, (a, b) in enumerate(zip(a_s[1:], b_s)):
    if a == b:
        match[i] = 1
for i, (a, b) in enumerate(zip(a_s, b_s[1:])):
    if a == b:
        match[i] = 1
match += [0]
ans = 1
cnt = 0
for item in match:
    if item == 1:
        cnt += 1
    else:
        ans *= pow(2, cnt, MOD)
        ans %= MOD
        cnt = 0
print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())
A=[]
B=[]
for i in range(N):
    n=int(input())
    A.append([n,1])
for i in range(N):
    n=int(input())
    B.append([n,-1])
    
C=A+B
C.sort()
mod=(10**9+7)
K=[0,1]
for i in range(2,N+1):
    K.append(K[len(K)-1]*i%mod)

S=0
cnt=0
ans=1
for i in range(2*N):
    cnt+=1
    S+=C[i][1]
    if S==0:
        ans*=K[cnt//2]%mod
        cnt=0
        S=0
print(ans)

Submission status: Wrong Answer

Solution code:
import math
n=int(input())
mod=10**9+7
X=[]
for i in range(n):
  X.append([int(input()),0])
for i in range(n):
  X.append([int(input()),1])
X.sort(key=lambda x:x[0])
ct=0
Y=[]
for a,b in X:
  if b==0:
    ct+=1
  else:
    ct-=1
  Y.append(ct)
ans=1
m=0
for y in Y:
  if y==0:
    ans*=math.factorial((m+1)//2)%mod
    m=0
  else:
    m+=1
print(ans%mod)

Submission status: Wrong Answer

Solution code:
import math
n=int(input())
mod=10**9+7
X=[]
for i in range(n):
  X.append([int(input()),0])
for i in range(n):
  X.append([int(input()),1])
X.sort(key=lambda x:x[0])
ct=0
Y=[]
for a,b in X:
  if b==0:
    ct+=1
  else:
    ct-=1
  Y.append(ct)
ans=1
m=0
for y in Y:
  if y==0:
    ans*=math.factorial(m)%mod
    m=0
  else:
    m=max(m,abs(y))
print(ans%mod)

Submission status: Wrong Answer

Solution code:
n=int(input())
mod=10**9+7
X=[]
for i in range(n):
  X.append([int(input()),0])
for i in range(n):
  X.append([int(input()),1])
X.sort(key=lambda x:x[0])
ct=0
Y=[]
for a,b in X:
  if b==0:
    ct+=1
  else:
    ct-=1
  Y.append(ct)
ans=1
m=0
for y in Y:
  if y==0:
    ans*=m
    m=0
  else:
    m=max(m,abs(y))
print(ans%mod)

Submission status: Wrong Answer

Solution code:
n=int(input())
mod=10**9+7
X=[]
for i in range(n):
  X.append([int(input()),0])
for i in range(n):
  X.append([int(input()),1])
X.sort(key=lambda x:x[0])
ct=0
Y=[]
for a,b in X:
  if b==0:
    ct+=1
  else:
    ct-=1
  Y.append(ct)
ans=1
m=1
for y in Y:
  if y==0:
    ans*=m
  else:
    m=max(m,abs(y))
print(ans%mod)

Submission status: Wrong Answer

Solution code:
n=int(input())
mod=10**9+7
X=[]
for i in range(n):
  X.append([int(input()),0])
for i in range(n):
  X.append([int(input()),1])
X.sort(key=lambda x:x[0])
ct=0
Y=[]
for a,b in X:
  if b==0:
    ct+=1
  else:
    ct-=1
  Y.append(ct)
ans=1
m=1
for y in Y:
  if y==0:
    ans*=m
  else:
    m=max(m,y)
print(ans%mod)

Submission status: Wrong Answer

Solution code:
n=int(input())
X=[]
for i in range(n):
  X.append([int(input()),0])
for i in range(n):
  X.append([int(input()),1])
X.sort(key=lambda x:x[0])
ct=0
Y=[]
for a,b in X:
  if b==0:
    ct+=1
  else:
    ct-=1
  Y.append(ct)
ans=1
m=1
for y in Y:
  if y==0:
    ans*=m
  else:
    m=max(m,y)
print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

import sys
from math import factorial

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()
A=sorted([INT() for i in range(N)])
B=sorted([INT() for i in range(N)])

i=j=cur=cnt=0
L=[]
if A[0]>B[0]:
    cur=1
while i<N and j<N:
    if cur==0 and A[i]<B[j]:
        i+=1
        cnt+=1
    elif cur==1 and A[i]<B[j]:
        i=j
        if cnt:
            L.append(cnt)
        cnt=0
        cur=0
    elif cur==1 and A[i]>B[j]:
        j+=1
        cnt+=1
    elif cur==0 and A[i]>B[j]:
        j=i
        if cnt:
            L.append(cnt)
        cnt=0
        cur=1
if cnt!=0:
    L.append(cnt)

ans=1
for a in L:
    ans=(ans*factorial(a))%MOD
print(ans)

Submission status: Wrong Answer

Solution code:
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)

Submission status: Wrong Answer

Solution code:
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:
		ans = ans * abs(bb[i] - i)
		ans = ans % 1000000007

print(ans)

Submission status: Wrong Answer

Solution code:
from math import factorial
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) #門番
ABsub = [None] * (2*N)
mod = 7 + 10**9

ai, bi = 0, 0
if A[0] < B[0]:
    ABsub[0] = (1, 0)
    ai = 1
else:
    ABsub[0] = (0, 1)
    bi = 1

for i in range(1, 2*N):
    if A[ai] < B[bi]:
        ai += 1
        ABsub[i] = (ai, bi)
        
    else:
        bi += 1
        ABsub[i] = (ai, bi)

start, Total = 0, 1
for a, b in ABsub:
    if a == b:
        Total *= (factorial(a - start)) % mod
        Total %= mod
        start = a
print(Total)

Submission status: Wrong Answer

Solution code:
from math import factorial
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) #門番
ABsub = [None] * (2*N)

ai, bi = 0, 0
if A[0] < B[0]:
    ABsub[0] = (1, 0)
    ai = 1
else:
    ABsub[0] = (0, 1)
    bi = 1

for i in range(1, 2*N):
    if A[ai] < B[bi]:
        ABsub[i] = (ABsub[i-1][0] + 1, ABsub[i-1][1])
        ai += 1
    else:
        ABsub[i] = (ABsub[i-1][0], ABsub[i-1][1] + 1)
        bi += 1

start, Total = 0, 1
for a, b in ABsub:
    if a == b:
        Total *= factorial(a - start)
        start = a
print(Total)

Submission status: Wrong Answer

Solution code:
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) #門番
ANU, BNU = 0, 0 #A not used, B not used
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
            BNU -= 1
        ai += 1
    else:
        if ANU == 0:
            BNU += 1
        else:
            Total *= ANU
            ANU -= 1
        bi += 1
print(Total)

Submission status: Wrong Answer

Solution code:
from math import factorial
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)
ai, bi = 0, 0
ABsub = [0] * (2*N)
if A[0] < B[0]:
    ABsub[0] = 1
    ai = 1
else:
    ABsub[0] = -1
    bi = 1

for i in range(1, 2*N):
    if A[ai] < B[bi]:
        ABsub[i] = ABsub[i-1] + 1
        ai += 1
    else:
        ABsub[i] = ABsub[i-1] - 1
        bi += 1

Total = 1
maxAbs = 1
for i in range(1, 2*N):
    if ABsub[i] == 0:
        Total *= factorial(maxAbs)
        maxAbs = 0
    else:
        maxAbs = max(maxAbs, abs(ABsub[i]))

print(Total)

Submission status: Wrong Answer

Solution code:
N=int(input())
A=[]
B=[]
for i in range(N):
    a=int(input())
    A.append(a)
for i in range(N):
    b=int(input())
    B.append(b)
    
A.sort()
B.sort()

KAI=[0,1]
for i in range(2,2*10**5+2):
    KAI.append((KAI[i-1]*i)%(10**9+7))
    
M=0
A1=0
B1=0
ANS=[]
for i in range(2*N):
    if A==[] or B==[]:
        break
    elif A[0]<B[0]:
        A1+=1
        del A[0]
    elif B[0]<A[0]:
        B1+=1
        del B[0]
    if abs(A1-B1)==0:
        ANS.append(M)
        M=0
    elif abs(A1-B1)>M:
        M=abs(A1-B1)
ANS.append(M)

ans=1
for i in range(len(ANS)):
    ans=(ans*KAI[ANS[i]])%(10**9+7)
    
print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=[[int(input()),1] for i in range(n)]+[[int(input()),-1] for i in range(n)]
a.sort()
c=0
b=a[0][1]
d=[]
for i,j in a:
    if j!=b and c*j<0:d.append(abs(c))
    c+=j
    b=j
ans=1
kai=[1]
for i in range(2,10**5+1):
    kai.append((kai[-1]*i)%(10**9+7))
for i in d:
    ans*=kai[i-1]%(10**9+7)
print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=[[int(input()),1] for i in range(n)]+[[int(input()),-1] for i in range(n)]
a.sort()
c=0
b=a[0][1]
d=[]
for i,j in a:
    if j!=b and c*j<0:d.append(abs(c))
    c+=j
    b=j
ans=1
kai=[1]
for i in range(2,10**5+1):
    kai.append((kai[-1]*i)%(10**9+7))
for i in d:
    ans*=kai[i-1]
print(ans)

Submission status: Wrong Answer

Solution code:
import math
n=int(input())
a=[[int(input()),1] for i in range(n)]+[[int(input()),-1] for i in range(n)]
a.sort()
c=0
r=0
b=a[0][1]
d=[]
for i,j in a:
    if j!=b and c*j<0:d.append(abs(r))
    c+=j
    if c*j>0:r+=1
    else:r=0
    b=j
ans=1
for i in d:
    ans*=math.factorial(i)
print(ans)

Submission status: Wrong Answer

Solution code:
def i1():
 return int(input())
def i2():
 return [int(i) for i in input().split()]

n=i1()
x=[]
for i in range(n):
 x.append([i1(),0])
for i in range(n):
 x.append([i1(),1])
x.sort()
d=x[0][1]
c=0
dd=[]
cc=0
for i in range(2*n):
 if x[i][1]==d:
    c+=1
    cc=0
 else:
    if cc==0:
       dd.append(c)
       cc=1
    
    c-=1
    if c<0:
       c=1
       d^=1
    
f=[i for i in range(max(dd)+1)]
f[0]=1
for i in range(max(dd)):
 f[i+1]*=f[i]
 f[i+1]%=10**9+7
ddd=1
for i in dd:
 ddd*=f[i]
 ddd%=10**9+7
print(ddd)

Submission status: Wrong Answer

Solution code:
def i1():
 return int(input())
def i2():
 return [int(i) for i in input().split()]

n=i1()
x=[]
for i in range(n):
 x.append([i1(),0])
for i in range(n):
 x.append([i1(),1])
x.sort()
d=x[0][1]
c=0
dd=[]
cc=0
for i in range(2*n):
 if x[i][1]==d:
    c+=1
    cc=0
 else:
    if cc==0:
       dd.append(c)
       cc=1
    if c>0:
      c-=1
if c>0:
   dd.append(c)    
f=[i for i in range(max(dd)+1)]
f[0]=1
for i in range(max(dd)):
 f[i+1]*=f[i]
 f[i+1]%=10**9+7
ddd=1
for i in dd:
 ddd*=f[i]
 ddd%=10**9+7
print(ddd)