CodeEff/ECCO huggingface dataset:
# Original solution
n = int(eval(input()))
a_list = [int(i) for i in input().split()]
minus_count_even_flug = True
zero_exist_flug = False
  
for a in a_list:
    if a < 0:
        minus_count_even_flug = not minus_count_even_flug
    if a == 0:
        zero_exist_flug = True
     
a_abs_list = [abs(a) for a in a_list]
sum_a_abs_list = sum(a_abs_list)
if minus_count_even_flug or zero_exist_flug:
    print(sum_a_abs_list)
else:
    print((sum_a_abs_list - 2 * min(a_abs_list)))

# Optimized solution
N = int(eval(input()))
A = list(map(int, input().split()))
B = list(map(abs, A))
C = [a for a in A if a < 0]
if len(C) % 2 == 0:
    print((sum(B)))
else:
    print((sum(B) - min(B)*2))
# Original solution
import itertools
import math
import fractions
import functools
import copy

n = int(eval(input()))
a = list(map(int, input().split()))

pm = [0,0]

for i in range(n):
    if a[i] == 0:
        print((sum(list(map(abs,a)))))
        quit()
    elif a[i] > 0:
        pm[0] += 1
    else:
        pm[1] += 1
if pm[1] % 2 != 0:
    print((sum(list(map(abs,a))) - 2 * min(list(map(abs,a)))))
else:
    print((sum(list(map(abs,a)))))

# Optimized solution
import math


def main():
    n = int(eval(input()))
    # a, b, t = map(int, input().split())
    # h = list(map(int, input().split()))
    # s = input()
    # h = [int(input()) for _ in rane(n)]

    a = list(map(int, input().split()))
    abs_a = []
    minus = 0
    for ai in a:
        if ai < 0:
            minus += 1
        abs_a.append(abs(ai))

    if minus % 2 == 0:
        print((sum(abs_a)))
    else:
        mini = min(abs_a)
        print((sum(abs_a) - 2*mini))


if __name__ == '__main__':
    main()
# Original solution
import sys
sys.setrecursionlimit(10**6)

def dp(i,is_reverse):
  if i == 0:
    if is_reverse == 0: return 0
    else: return -float('inf')
  else:
    if is_reverse == 0: return max(dp(i-1,0)+A[i-1],dp(i-1,1)-A[i-1])
    else: return max(dp(i-1,0)-A[i-1],dp(i-1,1)+A[i-1])

N = int(eval(input()))
A = list(map(int,input().split()))
print((dp(N,0)))

# Optimized solution
N = int(eval(input()))
A = list(map(int,input().split()))
neg_num = 0
min_abs_a = float('inf')
sum_abs_A = 0
include_zero = False
for a in A:
  sum_abs_A += abs(a)
  min_abs_a = min(abs(a),min_abs_a)
  if a == 0: include_zero = True
  if a < 0 : neg_num += 1
print((sum_abs_A if neg_num%2==0 or include_zero is True else sum_abs_A - 2*min_abs_a))
# Original solution
# -*- coding: utf-8 -*-
n = int(eval(input()))
a = list(map(int, input().split()))

ans = 0
count = 0
for i in range(n):
    if a[i] < 0:
        a[i] = -a[i]
        count += 1
    ans += a[i]

if count % 2 == 0:
    print(ans)
else:
    print((ans - min(a) * 2))


# Optimized solution
# -*- coding: utf-8 -*-
n = int(eval(input()))
a = list(map(int, input().split()))

count = 0
for i in range(n):
    if a[i] < 0:
        a[i] = -a[i]
        count += 1

ans = sum(a)
if count % 2 == 0:
    print(ans)
else:
    print((ans - min(a) * 2))
# Original solution
import sys
sys.setrecursionlimit(10**6)


def dp(i, j):
    if i == 0:
        if j:
            return -float('inf')
        else:
            return 0
    if j:
        return max(dp(i-1, False) - a[i-1], dp(i-1, True) + a[i-1])
    else:
        return max(dp(i-1, False) + a[i-1], dp(i-1, True) - a[i-1])


n = int(eval(input()))
a = list(map(int, input().split()))
print((dp(n, False)))

# Optimized solution
n = int(eval(input()))
a = list(map(int, input().split()))
dp = {}
dp[0,0] = 0
dp[0,1] = -float('inf')
for i in range(1, n+1):
    dp[i, 0] = max(dp[i-1, 0] + a[i-1], dp[i-1, 1] - a[i-1])
    dp[i, 1] = max(dp[i-1, 0] - a[i-1], dp[i-1, 1] + a[i-1])
print((dp[n, 0]))
# Original solution
n = int(eval(input()))
a = list(map(int, input().split()))
b = 0
ans = 0

for i in range(n):
    if a[i] < 0:
        b += 1
        a[i] = abs(a[i])
        
if (0 in a) or ((b % 2) == 0):
    pass
else:
    ans -= min(a) * 2

ans += sum(a)
print(ans)

# Optimized solution
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

n, *a = list(map(int, read().split()))
cnt = 0
for i, aa in enumerate(a):
    if aa < 0:
        a[i] = -aa
        cnt += 1
if cnt % 2 == 0:
    print((sum(a)))
else:
    print((sum(a) - min(a) * 2))
# Original solution
N = int(eval(input()))
A = list(map(int, input().split()))

dp = [[0] * 2 for i in range(N)]
dp[0][0] = A[0] + A[1]
dp[0][1] = -A[0] - A[1]

for i in range(1, N - 1):
    for j in range(2):
        if j == 0:
            dp[i][j] = max(dp[i-1][0] + A[i+1], dp[i-1][1] + A[i+1])
        else:
            dp[i][j] = max(dp[i-1][0] - 2 * A[i] - A[i+1], dp[i-1][1] + 2 * A[i] - A[i+1])

print((max(dp[N-2])))

# Optimized solution
N = int(eval(input()))
A = list(map(int, input().split()))

dp = [[0] * 2 for i in range(N+1)]

dp[0][1] = - 10 ** 18

for i in range(N):
    for j in range(2):
        if j == 0:
            dp[i+1][j] = max(dp[i][0] + A[i], dp[i][1] - A[i])
        else:
            dp[i+1][j] = max(dp[i][0] - A[i], dp[i][1] + A[i])

print((dp[N][0]))
# Original solution
import sys
sys.setrecursionlimit(10 ** 6)


def main():
    n = int(eval(input()))
    a = list(map(int, input().split()))

    def dp(i, j):
        if i == 0 and j == 0:
            return 0
        if i == 0 and j == 1:
            return -float('inf')
        if j == 0:
            return max(dp(i - 1, 0) + a[i - 1], dp(i - 1, 1) - a[i - 1])
        if j == 1:
            return max(dp(i - 1, 0) - a[i - 1], dp(i - 1, 1) + a[i - 1])

    score = dp(n, 0)

    print(score)


main()


# Optimized solution
def main():
    n = int(eval(input()))
    aa = list(map(int, input().split()))

    pos = [0] * n
    neg = [0] * n

    pos[0] = aa[0]
    neg[0] = -aa[0]

    for i, a in enumerate(aa[1:-1], start=1):
        pos[i] = max(pos[i - 1] + a, neg[i - 1] - a)
        neg[i] = max(pos[i - 1] - a, neg[i - 1] + a)

    pos[n - 1] = pos[n - 2] + aa[n - 1]
    neg[n - 1] = neg[n - 2] - aa[n - 1]

    score = max(pos[n - 1], neg[n - 1])

    print(score)


main()
# Original solution
N = int(eval(input()))
A = list(map(int, input().split()))
dp = [[0,0] for i in range(N+1)]
dp[0][1] = -10**20

for i in range(N):
    dp[i+1][0] = max(dp[i][0] + A[i], dp[i][1] - A[i])
    dp[i+1][1] = max(dp[i][0] - A[i], dp[i][1] + A[i])

print((dp[-1][0]))


# Optimized solution
N = int(eval(input()))
A = list(map(int, input().split()))
dp = [0, -10**20]
tmp = [0, 0]

for i in range(N):
    tmp[0] = max(dp[0] + A[i], dp[1] - A[i])
    tmp[1] = max(dp[0] - A[i], dp[1] + A[i])
    dp[0], dp[1] = tmp[0], tmp[1]
print((dp[0]))
# Original solution
n = int(eval(input()))
a = list(map(int, input().split()))

dp = [[0] * 2 for i in range(n + 1)]

# dp[i + 1][j] := i 番目までの最大値（j = 1 なら a[i] の符号反転）

dp[0][1] = -float("inf")
for i in range(n):
    dp[i + 1][0] = max(dp[i][0] + a[i], dp[i][1] - a[i])
    dp[i + 1][1] = max(dp[i][0] - a[i], dp[i][1] + a[i])

print((dp[n][0]))

# Optimized solution
n = int(eval(input()))
a = list(map(int, input().split()))

b = []
count = 0
for i in a:
    if i < 0:
        count += 1
    b.append(abs(i))

if count % 2:
    print((sum(b) - 2 * min(b)))
else:
    print((sum(b)))
# Original solution

n = int(eval(input()))
a = list(map(int,input().split()))
ma = []
pa = []
l =[]
t = 0
k  = 0
for i in a:
  if i < 0:
    ma.append(i)
    l.append(i*-1)
    k+=1
  elif i> 0:
    pa.append(i)
    l.append(i)
  else:
    t = 1
s = sum(l)
print((s  if t == 1 or k%2==0 else s - 2*min(l)))

# Optimized solution
#写経

n=eval(input())
a=list(map(int,input().split()))
b=[abs(x)for x in a]

#sum(x<0 for x in a)%2 ここで負の値の個数数えてるのが！
print((sum(b)-2*min(b)if sum(x<0 for x in a)%2 else sum(b)))
# Original solution
# -*- coding: utf-8 -*-
from functools import reduce

n = int(eval(input())) 
alist = list(map(int, input().split()))

def isMinus(x):
  if x<0:
    return 1
  else:
    return 0

#signature count
s = sum([isMinus(i) for i in alist]) % 2
#    print(alist)\\

if s == 0:
  ans = sum(map(abs, alist))
else:
  ans = sum(map(abs, alist)) - min(list(map(abs, alist)))*2

print(("{}".format(ans)))

# Optimized solution
n = int(eval(input()))
A = list(map(int, input().split()))
absA = [abs(a) for a in A]

if sum(True if a < 0 else False for a in A) % 2 == 0:
  ans = sum(absA)
else:
  ans = sum(absA) - 2 * min(absA)
  
print(ans)
# Original solution
n = int(eval(input()))
a_list = [int(x) for x in input().split()]

count = 0
for i in range(n):
    if a_list[i] < 0:
        a_list[i] *= -1
        count += 1

ans = sum(a_list)
if count % 2 == 1:
    a_list.sort()
    ans -= a_list[0] * 2
print(ans)

# Optimized solution
n = int(eval(input()))
a_list = [int(x) for x in input().split()]
count = 0
ans = 0
m = 10 ** 9
for i in range(n):
    if a_list[i] < 0:
        a_list[i] *= -1
        count += 1
    ans += a_list[i]
    if a_list[i] < m:
        m = a_list[i]
if count % 2 == 1:
    ans -= m * 2
print(ans)
# Original solution
N=int(eval(input()))
LA=list(map(int,input().split()))

p_m=1
for n in range(N):
    p_m*=LA[n]
    
LA_absolute=[i*(-1) if i<0 else i for i in LA]


if p_m>=0:
    print((sum(LA_absolute)))
else:
    print((sum(LA_absolute)-2*min(LA_absolute)))

# Optimized solution
N=int(eval(input()))
LA=list(map(int,input().split()))

Minus=0
Zero=0
L_absolute=[]

for i in LA:
    if i<0:
        L_absolute.append(i*(-1))
        Minus+=1
    elif i==0:
        Zero=1
    else:
        L_absolute.append(i)
        
L_absolute.sort()

if Zero==1:
    ans=sum(L_absolute)
elif Minus%2==1:
    ans=sum(L_absolute)-2*L_absolute[0]
else:
    ans=sum(L_absolute)
    
print(ans)
# Original solution
def main():
    N = int(eval(input()))
    A = list(map(int, input().split()))
    negatives = 0
    zeros = 0
    min_abs = 10**9 + 1
    for a in A:
        if a < 0:
            negatives += 1
        if a == zeros:
            zeros += 1
        min_abs = min(min_abs, abs(a))
    ans = sum(map(abs, A))
    if negatives % 2 == 0 or zeros != 0:
        print(ans)
        return

    ans -= 2*min_abs
    print(ans)

if __name__ == '__main__':
    main()


# Optimized solution
def main():
    N = int(eval(input()))
    A = list(map(int, input().split()))
    negatives = 0
    zeros = 0
    min_abs = 10**9 + 1
    for a in A:
        if a < 0:
            negatives += 1
        if a == 0:
            zeros += 1
        min_abs = min(min_abs, abs(a))
    ans = sum(map(abs, A))
    if negatives % 2 == 0 or zeros != 0:
        print(ans)
        return

    ans -= 2*min_abs
    print(ans)

if __name__ == '__main__':
    main()
# Original solution
import itertools

N = int(eval(input()))
A = list(map(int, input().split()))

pat = []
for i in range(N-1):
    pat.extend(itertools.combinations([i for i in range(N-1)],i+1))
B = A.copy()
B_list = [A.copy()]

for j in pat:
    for i in j:
        A[i:i+2] = [i * (-1) for i in A[i:i+2]]

    B_list.append(A)
    A = B.copy()
total = 0
for i in B_list:
    if total < sum(i):
        total = sum(i)
print(total)

# Optimized solution
N = int(eval(input()))
A = list(map(int, input().split()))

A_tmp = sorted(A, key=abs, reverse=True,)
total = 0
for i,v in enumerate(A_tmp):
    if (v <= 0) and (i < len(A_tmp)-1):
        A_tmp[i] = (-1) * A_tmp[i]
        A_tmp[i+1] = (-1) * A_tmp[i+1]
        total += A_tmp[i]
    else:
        total += v
print(total)
# Original solution
n = int(eval(input()))
a = list(map(int, input().split()))

b = [abs(i) for i in a]

if (sum([i < 0 for i in a]) + sum([i == 0 for i in a])) % 2 == 0:
    print((sum(b)))
else:
    print((sum(b) - min(b) * 2))

# Optimized solution
n = int(eval(input()))
a = list(map(int, input().split()))

b = [abs(i) for i in a]

if sum([i <= 0 for i in a]) % 2 == 0:
    print((sum(b)))
else:
    print((sum(b) - min(b) * 2))
# Original solution
N = int(eval(input()))
A = list(map(int,input().split()))
#偶数個しか正負反転できない
#小さい順に並べてマイナスは全部、境界は判定
S = sorted([[i,A[i]] for i in range(N)],key=lambda x: x[1])
judge = [False]*N
for j in range(0,N-1,2):
    if S[j][1]+S[j+1][1]<0:
        judge[S[j][0]] = True
        judge[S[j+1][0]] = True
for i in range(N-1):
    if judge[i]:
        A[i] *= -1
        A[i+1] *= -1
        judge[i+1] ^= True
print((sum(A)))

# Optimized solution
N = int(eval(input()))
A = list(map(int,input().split()))
#アルゴリズム（動的計画法）で殴る解法
INF = float("inf")
dp = [[0,0] for _ in range(N+1)]
dp[0][1] = -INF
for i in range(N):
    dp[i+1][0] = max(dp[i][0]+A[i],dp[i][1]-A[i])
    dp[i+1][1] = max(dp[i][0]-A[i],dp[i][1]+A[i])
print((dp[N][0]))
# Original solution
N=int(eval(input()))
A=list(map(int,input().split()))

n=0
sm=0
if A[0]>0:
  mn=A[0]
else:
  mn=(-1)*A[0]


for i in range(0,N):
  if A[i]<0:
    n=n+1
    AA=A.pop(i)
    A.insert(i,(-1)*AA)
    sm=sm+AA*(-1)
    if (-1)*AA<mn:
      mn=(-1)*AA
  else:
    sm=sm+A[i]
    if A[i]<mn:
      mn=A[i]

if n%2==0:
  print(sm)
else:
  print((sm-2*mn))

# Optimized solution
N=int(eval(input()))
A=list(map(int,input().split()))

n=0
sm=0
if A[0]>0:
  mn=A[0]
else:
  mn=(-1)*A[0]


for i in range(0,N):
  if A[i]<0:
    n=n+1
    sm=sm-A[i]
    if (-1)*A[i]<mn:
      mn=(-1)*A[i]
  else:
    sm=sm+A[i]
    if A[i]<mn:
      mn=A[i]
if n%2==0:
  print(sm)
else:
  print((sm-2*mn))
# Original solution
N = int(eval(input()))
A = list(map(int, input().split()))
absa = [abs(a) for a in A]
m = 0
for a in A:
    if a < 0:
        m += 1
if m % 2 == 0:
    print((sum(absa)))
else:
    print((sum(absa) - min(absa)*2))

# Optimized solution
N = int(eval(input()))
A = list(map(int, input().split()))

minus = 0
for i in range(N):
    if A[i] < 0:
        minus += 1

absed = sorted([abs(a) for a in A])
if minus % 2 == 0:
    print((sum(absed)))
else:
    print((sum(absed) - absed[0]*2))
# Original solution
N = int(eval(input()))
A = list(map(int,input().split(' ')))
sum = 0
min = 1000000000
minus = 0
for n in range(N):
    n_abs = abs(A[n])
    sum += n_abs
    if min > n_abs:
        min = n_abs
    if A[n] < 0:
        minus += 1
if minus%2 != 0:
    sum -= 2*min
print(sum)

# Optimized solution
N = int(eval(input()))
A = list(map(int,input().split(' ')))
ABS_LIST = list(map(abs,A))
ABS_SUM = sum(ABS_LIST)
if len([x for x in A if x < 0])%2 != 0:
    print((ABS_SUM-min(ABS_LIST)*2))
else:
    print(ABS_SUM)
# Original solution
N = int(eval(input()))
A = list(map(int, input().split()))
Amin = 10**9
ans = 0
counter = 0
for i in range(N):
  if(A[i] < 0):
    counter = counter + 1
  a = abs(A[i])
  ans += a
  Amin = min(a, Amin)
    
if(counter%2 == 1):
    ans -= 2*Amin
  
print(ans)


# Optimized solution
n = int(eval(input()))
A = list(map(int, input().split()))
neg_A = len(list([x for x in A if x < 0]))
A = sorted(map(abs, A))

if neg_A%2 == 0:
  print((sum(A)))
else:
  print((sum(A)-2*A[0]))
# Original solution
from functools import lru_cache
import sys

sys.setrecursionlimit(10000000)

N = int(eval(input()))
A = list(map(int, input().split()))


@lru_cache(maxsize=None)
def rec(i, reverse):
    if i == N - 1:
        return A[i] * reverse

    num_normal = rec(i + 1, 1) + (A[i] * reverse)
    num_reverse = rec(i + 1, -1) + (-1 * A[i] * reverse)

    return max(num_normal, num_reverse)


print((rec(0, 1)))


# Optimized solution
N = int(eval(input()))
A = list(map(int, input().split()))

minus_count = 0

for a in A:
    if a < 0:
        minus_count += 1

total = 0
min_num = 10 ** 9

for a in A:
    total += abs(a)
    min_num = min(min_num, abs(a))

if minus_count % 2 == 1:
    total -= 2 * min_num

print(total)
# Original solution
n = int(eval(input()))
a = list(map(int,input().split()))
ans = 0
minus = 0
q = abs(a[0])
for i in a:
    ans += abs(i)
    q = min(q,abs(i))
    if i < 0:
        minus += 1

if minus%2 == 0:
    print(ans)
else:
    print((ans - 2*q))

# Optimized solution
n = int(eval(input()))
a = list(map(int,input().split()))
minus = 0
mi = 10**10
ans = 0
for i in range(n):
    if a[i] < 0:minus+=1
    mi = min(mi,abs(a[i]))
    ans += abs(a[i])
if minus%2 == 0:
    print(ans)
else:
    print((ans-2*mi))
# Original solution
n=int(eval(input()))
a=list(map(int,input().split()))
minabs=10**9+1
minus=0
allsum=0
for i in a:
  if i<0:
    minus+=1
  minabs=min(minabs,abs(i))
  allsum+=abs(i)

if minus%2==1:
  allsum-=minabs*2
print(allsum)

# Optimized solution
# 0が一つでもあれば、すべて正の数にできる
# そうでない場合、負の数の数が奇数の場合、負の数を一つ（最も絶対値が小さい）だけ残す

import sys
readline = sys.stdin.readline

N = int(readline())
A = list(map(int,readline().split()))

ans = 0
minabs = 10 ** 9 + 1
haszero = False
minuscnt = 0
for i in range(len(A)):
  if A[i] == 0:
    haszero = True
  elif A[i] < 0:
    minuscnt += 1
  ans += abs(A[i])
  if abs(A[i]) < minabs:
    minabs = abs(A[i])
    
if haszero:
  print(ans)
elif minuscnt % 2 == 1:
  print((ans - minabs * 2))
else:
  print(ans)
# Original solution
n,*a=list(map(int,open(0).read().split()))
b=sum([i<0 for i in a])%2
a=[abs(i) for i in a]
print((sum(a) if b==0 or 0 in a else sum(a)-min(a)*2))

# Optimized solution
n,*a=list(map(int,open(0).read().split()))
b=sum(i<0 for i in a)%2
a=list(map(abs,a))
print((sum(a)-min(a)*2*b))
# Original solution
import math
import re

n = int(input())
a = input().split()

cnt = 0
minus = 0
b = []
for i in a:
    a[cnt] = int(i)
    b.append(abs(int(i)))
    if a[cnt] < 0:
        minus += 1
    cnt += 1


if minus % 2 != 0:
    mini = min(b)
else:
    mini = 0

total = 0
for i in b:
    total += abs(i)

total -= 2 * abs(mini)
print(total)

# Optimized solution
# coding:utf-8                                                                                         

import math
                                                                                                       
def main(n,a):                                                                                         
    n = int(n)                                                                                         
    a = a.split()
    cnt = 0
    minus = 0                                                                                          
    b = []                                                                                             
    for i in a:                                                                                        
        a[cnt] = int(i)                                                                                
        b.append(abs(int(i)))                                                                          
        if a[cnt] < 0:
            minus += 1                                                                                 
        cnt += 1 
    if minus % 2 != 0: 
        mini = min(b)                                                                                  
    else:       
        mini = 0 
    total = 0   
    for i in b: 
        total += abs(i)
    total -= 2 * abs(mini)                                                                             
    print(total)     
    return total
                                                                                                       
if __name__ == "__main__":                                                                             
    n = input()
    a = input()                                                                                    
    main(n,a)
# Original solution
N=int(eval(input()))
A=list(map(int,input().split()))

cnt=0
S=0
mi=10000000000
C0=False

for i in range(N):
    mi=min(mi,abs(A[i]))
    if A[i]>0:
        S+=A[i]
    elif A[i]==0:
        C0==True
    elif A[i]<0:
        cnt+=1
        S+=(-A[i])

if cnt%2==0 or C0==True:
    print(S)
else:
    print((S-mi*2))


# Optimized solution
N=int(eval(input()))
A=list(map(int,input().split()))

mi=10**9+1
S=0
cnt=0
for i in range(N):
    if A[i]<0:
        cnt+=1
    a=abs(A[i])
    S+=a
    mi=min(mi,a)

if cnt%2==0:
    ans=S
else:
    ans=S-2*mi
print(ans)
# Original solution
N = int(eval(input()))
A = [int(i) for i in input().split()]

ans = sum(A)
for i in range(N):
  if A[i] < 0:
    if i != N-1:
      A[i] = A[i] * -1
      A[i+1] = A[i+1] * -1
      ans = max(ans, sum(A))

print(ans)


# Optimized solution
N = int(eval(input()))
A = [int(i) for i in input().split()]

count = 0
for i in range(N):
  if A[i] <0:
    count = count + 1
    A[i] = A[i] * -1

if 0 in A:
  ans = sum(A)
else:
  if count%2 == 0:
    ans = sum(A)
  else:
    ans = sum(A) - 2*min(A)

print(ans)
# Original solution
import heapq
N = int(eval(input()))
A = list(map(int, input().split()))
heapq.heapify(A)
while True:
  m = heapq.heappop(A)
  n = heapq.heappop(A)
  if m + n > 0:
    heapq.heappush(A, m)
    heapq.heappush(A, n)
    break
  else:
    heapq.heappush(A, -m)
    heapq.heappush(A, -n)
print((sum(A)))

# Optimized solution
import heapq
N = int(eval(input()))
A = list(map(int, input().split()))
heapq.heapify(A)
while True:
  m = heapq.heappop(A)
  n = heapq.heappop(A)
  if m + n >= 0:
    heapq.heappush(A, m)
    heapq.heappush(A, n)
    break
  else:
    heapq.heappush(A, -m)
    heapq.heappush(A, -n)
print((sum(A)))
# Original solution
import bisect
n,m=list(map(int,input().split()))
a=list(map(int,input().split()))
b=list(map(int,input().split()))
x=10**9+7
if len(set(a))!=len(a) or len(set(b))!=len(b):
  print((0))
else:
  a.sort()
  b.sort()
  L=[]
  for i in range(n):
    l=[]
    for j in range(m):
      l.append(0)
    L.append(l)
  if m*n in a and m*n in b:
    L[a.index(m*n)][b.index(m*n)]=m*n
    ct=1
    ctn=1
    ctm=1
    for k in range(m*n-1,0,-1):
      if k in a and k in b:
        L[bisect.bisect_left(a,k)][bisect.bisect_left(b,k)]=k
        ct*=1
        ctn+=1
        ctm+=1
      elif k in a or k in b:
        if k in a:
          ct*=ctm
          ctn+=1
        if k in b:
          ct*=ctn
          ctm+=1
      else:
        ct*=(ctm*ctn-m*n+k)
      if ct==0:
        break
      ct=ct%x
    print(ct)
  else:
    print((0))

# Optimized solution
import bisect
n,m=list(map(int,input().split()))
a=list(map(int,input().split()))
b=list(map(int,input().split()))
x=10**9+7
if len(set(a))!=len(a) or len(set(b))!=len(b):
  print((0))
else:
  a.sort()
  b.sort()
  L=[]
  for i in range(n):
    l=[]
    for j in range(m):
      l.append(0)
    L.append(l)
  if m*n in a and m*n in b:
    L[a.index(m*n)][b.index(m*n)]=m*n
    ct=1
    ctn=1
    ctm=1
    A=set(a)
    B=set(b)
    for k in range(m*n-1,0,-1):
      if k in A and k in B:
        L[bisect.bisect_left(a,k)][bisect.bisect_left(b,k)]=k
        ct*=1
        ctn+=1
        ctm+=1
      elif k in A:
          ct*=ctm
          ctn+=1
      elif k in B:
          ct*=ctn
          ctm+=1
      else:
        ct*=(ctm*ctn-m*n+k)
      if ct==0:
        break
      ct=ct%x
    print(ct)
  else:
    print((0))
# Original solution
import bisect
n,m=list(map(int,input().split()))
a=list(map(int,input().split()))
b=list(map(int,input().split()))
x=10**9+7
if len(set(a))!=len(a) or len(set(b))!=len(b):
  print((0))
else:
  a.sort()
  b.sort()
  L=[]
  for i in range(n):
    l=[]
    for j in range(m):
      l.append(0)
    L.append(l)
  ct=1;ctn=0;ctm=0
  A=set(a);B=set(b)
  for k in range(m*n,0,-1):
    if k in A and k in B:
      L[bisect.bisect_left(a,k)][bisect.bisect_left(b,k)]=k
      ctn+=1;ctm+=1
    elif k in A:
        ct*=ctm
        ctn+=1
    elif k in B:
        ct*=ctn
        ctm+=1
    else:
      ct*=(ctm*ctn-m*n+k)
    if ct==0:
      break
    ct=ct%x
  print(ct)

# Optimized solution
n,m=list(map(int,input().split()))
a=list(map(int,input().split()))
b=list(map(int,input().split()))
x=10**9+7
A=set(a);B=set(b)
if len(A)!=len(a) or len(B)!=len(b):
  print((0))
else:
  L=[]
  for i in range(n):
    l=[]
    for j in range(m):
      l.append(0)
    L.append(l)
  ct=1;ctn=0;ctm=0
  for k in range(m*n,0,-1):
    if k in A and k in B:
      L[n-ctn-1][m-ctm-1]=k
      ctn+=1;ctm+=1
    elif k in A:
        ct*=ctm
        ctn+=1
    elif k in B:
        ct*=ctn
        ctm+=1
    else:
      ct*=(ctm*ctn-m*n+k)
    if ct==0:
      break
    ct=ct%x
  print(ct)