Problem:


Score : 300 points

Problem Statement

Snuke has N dogs and M monkeys. He wants them to line up in a row.

As a Japanese saying goes, these dogs and monkeys are on bad terms. ("ken'en no naka", literally "the relationship of dogs and monkeys", means a relationship of mutual hatred.) Snuke is trying to reconsile them, by arranging the animals so that there are neither two adjacent dogs nor two adjacent monkeys.

How many such arrangements there are? Find the count modulo 10^9+7 (since animals cannot understand numbers larger than that).
Here, dogs and monkeys are both distinguishable. Also, two arrangements that result from reversing each other are distinguished.

Constraints

1 ≤ N,M ≤ 10^5

Input

Input is given from Standard Input in the following format:

N M

Output

Print the number of possible arrangements, modulo 10^9+7.

Sample Input 1

2 2

Sample Output 1

8

We will denote the dogs by A and B, and the monkeys by C and D. There are eight possible arrangements: ACBD, ADBC, BCAD, BDAC, CADB, CBDA, DACB and DBCA.

Sample Input 2

3 2

Sample Output 2

12

Sample Input 3

1 8

Sample Output 3

0

Sample Input 4

100000 100000

Sample Output 4

530123477



Sample input:
2 2


Sample output:
8

Submission status: Accepted

Solution code:
def pre_c(n, mod):
    f = [0] * (n + 1)
    #g = [0] * (n + 1)
    for i in range(n + 1):
        if i == 0:
            f[i] = 1
            #g[i] = 1
        else:
            f[i] = (f[i - 1] * i) % mod
            #g[i] = pow(f[i], mod - 2, mod)

    return f
mod = 10 ** 9 + 7
n, m = map(int,input().split())
big = max(n, m)
small = n + m - big
if big - small >= 2:
    print(0)
    exit()
flag = 1 if small != big else 2
F = pre_c(big + 1, mod)
rec = F[big] * F[small] * flag
print(rec % mod)

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7

def main():
    N, M = map(int, input().split())
    mod = 10 ** 9 + 7

    table = [1]
    tmp = 1
    for i in range(1, N + M + 1):
        tmp *= i
        tmp %= mod
        table.append(tmp)

    if abs(N - M) > 1:
        print(0)
    elif abs(N - M) == 1:
        print(table[N] * table[M] % mod)
    else:
        print(2 * table[N] * table[M] % mod)



if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

N, M = map(int, input().split())
mod = 10 ** 9 + 7

table = [1]
tmp = 1
for i in range(1, N + M + 1):
    tmp *= i
    tmp %= mod
    table.append(tmp)

if abs(N - M) > 1:
    print(0)
elif abs(N - M) == 1:
    print(table[N] * table[M] % mod)
else:
    print(2 * table[N] * table[M] % mod)

Submission status: Accepted

Solution code:
import math
n,m = [int(x) for x in input().split()]

mod = 10**9 + 7

if n == m:
    ans = (math.factorial(n) * math.factorial(m) * 2) % mod
elif abs(n-m) > 1:
    ans = 0
else:
    ans = (math.factorial(n) * math.factorial(m)) % mod

print(ans)

Submission status: Accepted

Solution code:
import math
n, m = map(int, input().split())

N = math.factorial(n)
M = math.factorial(m)
INF = 10**9 + 7

if abs(n-m) > 1:
    print(0)
elif abs(n-m) == 1:
    print(N*M%INF)
else:
    print(2*N*M%INF)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**7)
mo=10**9+7
def fact(x):
  if x==0:
    return 1
  else:
    return x*fact(x-1)%mo
n,m=map(int,input().split())
if abs(n-m)>1:
  print(0)
elif m==n:
  print((2*fact(m)*fact(n))%mo)
else:
  print((fact(m)*fact(n))%mo)

Submission status: Accepted

Solution code:
n , m = map(int,input().split())
mod = 10**9 + 7
if abs(n-m) >= 2:
    print(0)
elif abs(n-m) <= 1:
    p = 1
    q = 1
    t = 1
    for i in range(n):
        p *= (i+1)
        p %= mod
    for i in range(m):
        q *= (i+1)
        q %= mod
    if abs(n-m) == 0:
        t = 2
    print(t*p*q%mod)

Submission status: Accepted

Solution code:
from math import factorial,sqrt,ceil #,gcd

N,M = map(int,input().split())
MOD = 10**9 + 7

if abs(N-M)>=2:
    print(0)
    exit()

if N==M:
    ans = factorial(N) % MOD
    ans = ans * factorial(M) % MOD
    ans = ans * 2 % MOD  
    print(ans)
    exit()
elif N>M:
    ans = factorial(N) % MOD * factorial(M) % MOD
    print(ans)
    exit()
elif M>N:
    ans = factorial(M) % MOD * factorial(N) % MOD
    print(ans)
    exit()

Submission status: Accepted

Solution code:
MOD = 10**9 + 7
max = 10**5 + 1
facts = [1 for i in range(max)]
invs = [1 for i in range(max)]

def inv(x):
    return pow(x,MOD-2,MOD)

for i in range(1,max):
    facts[i] = facts[i-1]*i%MOD
    invs[i] = inv(facts[i])

def comb(n,r):
    return facts[n]*invs[r] * invs[n-r] % MOD

n,m = map(int,input().split())
if m > n:
    n,m = m,n
if n-m >= 2:
    print(0)
elif n-m == 1:
    print(facts[m]*facts[n] % MOD)
else:
    print(facts[m]*facts[n]*2 % MOD)

Submission status: Accepted

Solution code:
import sys
import re
import math
import collections
import decimal
import bisect
import itertools
import fractions
import functools
import copy

# import heapq
# from collections import deque
# import decimal

sys.setrecursionlimit(10000001)
INF = sys.maxsize
MOD = 10 ** 9 + 7

ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))


# ===CODE===

def main():
    n, m = ns()

    tmp = abs(n - m)
    if tmp > 1:
        print(0)
        exit(0)

    def for_factorial(n):
        val = 1
        for i in range(2, n + 1):
            val *= i
            val %= MOD
        return val

    n_fact = for_factorial(n)
    m_fact = for_factorial(m)

    if tmp == 1:
        ans = n_fact * m_fact % MOD
    else:
        ans = 2 * n_fact * m_fact % MOD
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def fact(n):
    res = 1
    for i in range(2,n+1):
        res = (res*i)%1000000007
    return res

N,M = map(int,input().split())

if abs(N-M) >= 2:
    print(0)
elif N == M:
    print(2*fact(N)*fact(M)%1000000007)
else:
    print(fact(N)*fact(M)%1000000007)

Submission status: Accepted

Solution code:
def fact(n):
    mod = int(1e9)+7
    res = 1
    for i in range(1,n+1):
        res = (res*i)%mod
    return res

N,M = map(int,input().split())

if abs(N-M) >= 2:
    print(0)
    exit()

mod = int(1e9)+7

if N == M:
    print(2*fact(N)*fact(M)%mod)
else:
    print(fact(N)*fact(M)%mod)

Submission status: Accepted

Solution code:
import math
def fact(n):
    mod = int(1e9)+7
    return math.factorial(n)%mod

N,M = map(int,input().split())

if abs(N-M) >= 2:
    print(0)
    exit()

mod = int(1e9)+7

if N == M:
    print(2*fact(N)*fact(M)%mod)
else:
    print(fact(N)*fact(M)%mod)

Submission status: Accepted

Solution code:
import math
def fact(n):
    return math.factorial(n)

N,M = map(int,input().split())

if abs(N-M) >= 2:
    print(0)
    exit()

mod = int(1e9)+7

if N == M:
    print(2*fact(N)*fact(M)%mod)
else:
    print(fact(N)*fact(M)%mod)

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
MOD=10**9+7
s=1
for i in range(2,N+1):
  s=(s*i)%MOD
t=1
for i in range(2,M+1):
  t=(t*i)%MOD
if abs(N-M)>1:
  print(0)
  exit()
if abs(N-M)==1:
  print((s*t)%MOD)
else:
  print((2*s*t)%MOD)

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

from math import factorial

n, m = map(int, readline().split())
mod = 10 ** 9 + 7
ans = factorial(n) * factorial(m)
if abs(n - m) > 1:
    ans = 0
elif n == m:
    ans *= 2
print(ans % mod)

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
if m>n:
    n,m = m,n
mod = 10**9+7
ans = 1
if n>m+1:
    print(0)
    exit()
if n==m:
    ans = 2
for i in range(1,n+1):
    ans = (ans*i)%mod
for i in range(1,m+1):
    ans = (ans*i)%mod
print(ans)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
if abs(N - M) > 1:
    print(0)
    quit()
MOD = 10 ** 9 + 7
fact = [1] * (max(N, M) + 1)
f = 1
for i in range(1, max(N, M) + 1):
    f = f * i % MOD
    fact[i] = f
print(((N == M) + 1) * fact[N] * fact[M] % MOD)

Submission status: Accepted

Solution code:
A,B = map(int,input().split())
ans = 1
if A-B == 0:
    for i in range(1,A+1):
        ans = (ans * i) % (10**9+7)
    for i in range(1,B+1):
        ans = (ans * i) % (10**9+7)
    print(ans*2%1000000007)
elif abs(A-B) == 1:
    for i in range(1,A+1):
        ans = (ans * i) % (10**9+7)
    for i in range(1,B+1):
        ans = (ans * i) % (10**9+7)
    print(ans%1000000007)
else:
    print(0)

Submission status: Accepted

Solution code:
import sys

N, M = [int(n) for n in input().split()]

def factorial_mod(n):
    result = 1
    for i in range(1,n+1):
        result = (result * i) % (10**9 + 7)
    return result

if abs(N - M) > 1:
    print(0)
    sys.exit(0)

N_fact = factorial_mod(N)
M_fact = factorial_mod(M)

if N == M:
    print((2*N_fact*M_fact) % (10**9 + 7))
else:
    print((N_fact*M_fact) % (10**9 + 7))

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
mod = 10**9+7
if abs(n-m)>1:
    print(0)
else:
    ans = 1
    for i in range(2, max(n, m)):
        ans *= i
        ans %= mod
    ans = pow(ans, 2, mod)
    ans *= 2*max(n, m)**2 if n==m else max(n, m)
    print(ans%mod)

Submission status: Accepted

Solution code:
n, m = map(int, input().split())

if abs(n-m)>1:
    ans = 0
else:
    if m < n:
        n, m = m, n

    total_n = 1
    for i in range(1,n+1):
        total_n *= i
        if total_n >= 1000000007:
            total_n = total_n % 1000000007

    if n != m:
        ans = (total_n * total_n * m) % 1000000007
    else:
        ans = (total_n * total_n * 2) % 1000000007

print(int(ans))

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
mod=10**9+7
import sys
sys.setrecursionlimit(100002)
def kaijou(x):
  if x==1:
    return 1
  else:
    return ((x%mod)*kaijou(x-1))%mod

if abs(m-n)==1:
  n2=kaijou(n)
  m2=kaijou(m)
  print((n2*m2)%mod)
elif abs(m-n)==0:
  n2=kaijou(n)
  m2=kaijou(m)
  print(2*(n2*m2)%mod)
else:
  print(0)

Submission status: Accepted

Solution code:
n, m = map(int,input().split())
mod = 10**9+7
ans = 1
if abs(n-m) > 1:
  print(0)
  exit()
elif n==m:
  for i in range(1,n+1):
    ans *= i
    ans %= mod
  ans = pow(ans,2,mod)
  ans *= 2
  ans %= mod
else:
  for i in range(1,n+1):
    ans *= i
    ans %= mod
  for i in range(1,m+1):
    ans *= i
    ans %= mod
print(ans)

Submission status: Accepted

Solution code:
import math
n, m = map(int, input().split())

mod = 10**9+7
ans = 0
if abs(n-m) > 1:
    ans = 0
elif abs(n-m) == 1:
    ans = math.factorial(n)*math.factorial(m)
elif abs(n-m) == 0:
    ans = 2 * (math.factorial(n)*math.factorial(m))

print(ans % mod)

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
mod = 1000000007
fact = [1]
for i in range(max(n, m)):
  fact.append(fact[-1] * (i + 1) % mod)
if n == m:
  print(2 * fact[n] * fact[m] % mod)
elif abs(n - m) == 1:
  print(fact[n] * fact[m] % mod)
else:
  print(0)

Submission status: Accepted

Solution code:
from math import factorial
n, m = map(int, input().split())
if abs(n - m) == 1:
    print((factorial(n) * factorial(m)) % 1000000007)
elif abs(n - m) == 0:
    print(((factorial(n) * factorial(m)) * 2) % 1000000007)
else:
    print(0)

Submission status: Accepted

Solution code:
import math


def combinations_count(n):
    return math.factorial(n)


def main():
    mod = 10**9 + 7
    n,m = list(map(int, input().split()))
    if abs(n - m) > 1:
        print(0)
        exit()
    elif n == m:
        ans = (combinations_count(n) * combinations_count(m)) * 2
    else:
        ans = (combinations_count(n)) * (combinations_count(m))
    print(ans % mod)



if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from math import factorial
n, m = map(int, input().split())
MOD = 1000000007
if abs(n - m) == 1:
    ans = max(n, m)
elif n == m:
    ans = 2
else:
    ans = 0
ans *= ((factorial(min(n, m)) % MOD)) ** 2
ans %= MOD
print(ans)

Submission status: Accepted

Solution code:
import sys
import itertools
sys.setrecursionlimit(1000000000)
from heapq import heapify,heappop,heappush,heappushpop
import math
import collections
MOD = 10**9 + 7

n,m = map(int,input().split())
if abs(n-m)>1:
    print(0)
else:
    if n == m:
        ans = (2*math.factorial(n%MOD)*math.factorial(m%MOD))%MOD
        print(ans)
    else:
        ans = (math.factorial(n%MOD)*math.factorial(m%MOD))%MOD
        print(ans)

Submission status: Accepted

Solution code:
import sys
n, m = map(int, input().split())
r = 10**9 + 7

if 1 < abs(n - m):
    print(0)
    sys.exit()
elif abs(n - m) == 1:
    ans = 1
else:
    ans = 2
    
for i in range(2, n + 1):
    ans *= i
    ans %= r

for i in range(2, m + 1):
    ans *= i
    ans %= r

print(ans)

Submission status: Accepted

Solution code:
MOD=10**9+7
N,M=map(int,input().split())

def fact_mod(x):
  ret=1
  for i in range(2,x+1):
    ret*=i
    ret%=MOD
  return ret

N,M=max(N,M),min(N,M)
if N-M>=2:
  print(0)
elif N==M:
  fact=fact_mod(N)
  print(2*fact**2%MOD)
else:
  fact1=fact_mod(M)
  fact2=fact_mod(N)
  print(fact1*fact2%MOD)

Submission status: Accepted

Solution code:
import math
mod = 10**9+7
n, m = map(int, input().split())
ans = 0
if n == m:
	ans = math.factorial(n)*math.factorial(m)*2
elif abs(n-m) >= 2:
	ans = 0
else:
	b = max(n,m)
	ans = math.factorial(b)*math.factorial(b-1)
print (ans%mod)

Submission status: Accepted

Solution code:
import math
a,b = map(int,(input().split()))
MOD = 10**9 +7
if abs(a-b) > 1:
    print(0)
elif abs(a-b) == 1:
    print(math.factorial(a)*math.factorial(b) %MOD)
else:
    print(((math.factorial(a)**2)*2 %MOD))

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
mod = 10**9+7
ans = 1
if abs(n-m)== 1:
    for i in range(1, n+1):
        ans = (ans * i) % mod
    for j in range(1, m+1):
        ans = (ans * j) % mod
    print(ans)
elif n == m:
    for i in range(1, n+1):
        ans = (ans * i) % mod
    print(ans**2 * 2 % mod)
else:
    print(0)

Submission status: Accepted

Solution code:
n , m = map(int, input().split())
N=1000000007
def fac(a):
    ans = 1
    while a>1:
        ans=ans*a%N
        a-=1
    return ans

if abs(n-m)==1:
    ans = fac(n)*fac(m)%N
elif n == m:
    ans = fac(n)*fac(m)*2%N
else:
    ans = 0
print(ans)

Submission status: Accepted

Solution code:
import math 
n,m = map(int,input().split())
nm_abs = abs(n-m)
n_lad = math.factorial(n)
m_lad = math.factorial(m)
if nm_abs >= 2:
  print(0)
elif nm_abs == 1:
  print((n_lad * m_lad) % (10**9+7))
else:
  print((n_lad * m_lad*2) % (10**9+7))

Submission status: Accepted

Solution code:
import math

N, M = map(int, input().split())

MOD = 10**9 + 7

if abs(N-M) > 1:
    ans = 0
elif abs(N-M) == 1:
    ans = math.factorial(N) * math.factorial(M)
else:
    ans = math.factorial(N) * math.factorial(M) * 2

print(ans % MOD)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys

def main():
    N, M = map(int, input().split())
    if abs(N-M) > 1:
        print(0)
        exit()

    p = 10**9 + 7
    ans = 1
    if N == M:
        ans *=2
    
    for i in range(N):
        ans = ans * (i+1) % p
    for j in range(M):
        ans = ans * (j+1) % p
    
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
mod = 10**9 + 7
import math
def perm(n, r):
    return (math.factorial(n) // math.factorial(n - r))%mod

if abs(n-m) >= 2:
    print(0)
else:
    mx = max(n,m)
    mn = min(n,m)
    p = math.factorial(mn)%mod
    k = (mn-1)*math.factorial(mx)%mod if mn == mx else 0
    print(p*(perm(mn+1,mx) - k)%mod)

Submission status: Accepted

Solution code:
from math import factorial
n,m = map(int, input().split())
MOD = 10 ** 9 + 7
if abs(n-m) > 1: print(0)
elif n==m:print((factorial(n) ** 2 * 2)%MOD)
else: print((factorial(n)*factorial(m))%MOD)

Submission status: Accepted

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

def main():
    N,M=map(int,input().split())
    ans=0
    
    if abs(N-M)>=2:
        print(0)
    else:
        if N==M:
            ans+=(math.factorial(N)*math.factorial(M))*2
            ans%=mod
            print(ans)
            
        else:
            ans+=(math.factorial(N)*math.factorial(M))
            ans%=mod
            print(ans)
        
if __name__=="__main__":
    main()

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
MOD = int(1e9) + 7


def factorial_cor(n):
    fact = 1
    for integer in range(1, n + 1):
        fact = (fact * integer) % MOD
    return fact


if abs(N - M) >= 2:
    print(0)
else:
    if abs(N - M) == 1:
        print((factorial_cor(N) * factorial_cor(M))%MOD)
    else:
        print(((factorial_cor(N) * factorial_cor(M))*2)%MOD)

Submission status: Accepted

Solution code:
from math import factorial
N,M=map(int,input().split())
ans=0
mod=10**9+7
if abs(N-M)>1:
    print(0)
    exit()
elif N == M:
    ans=2*(factorial(N)*factorial(M))%mod
    print(ans)
else:
    ans=(factorial(N)*factorial(M))%mod
    print(ans)

Submission status: Accepted

Solution code:
from math import factorial

def resolve():
	n, m = map(int, input().split())
	mod = 10**9+7
	if abs(n - m) > 1:
		print(0)
		return
	a = factorial(m) % mod * factorial(n) % mod
	if (n + m) % 2 == 0:
		a = a * 2 % mod
	print(a)
resolve()

Submission status: Accepted

Solution code:
from math import factorial
n,m = map(int,input().split())
if abs(n-m) >= 2:
  print(0)
elif abs(n-m) == 1:
  print(((factorial(n))*(factorial(m)))%(10**9+7))
else:
  print((2*(factorial(n))**2) %(10**9+7))

Submission status: Accepted

Solution code:
from math import factorial
n,m = map(int,input().split())
if abs(n-m) >= 2:
  print(0)
elif abs(n-m) == 1:
  print(((factorial(n)%(10**9+7))*(factorial(m)%(10**9+7)))%(10**9+7))
else:
  print((2*(factorial(n) % (10**9+7))**2) %(10**9+7))

Submission status: Accepted

Solution code:
import math
mod = 1000000007

n,m = map(int,input().split())
if abs(n-m) >= 2:
    num_dm = 0
elif abs(n-m) == 1:
    num_dm = 1
else:
    num_dm = 2
d_comb_num = math.factorial(n) % mod
m_comb_num = math.factorial(m) % mod
ans = num_dm * d_comb_num * m_comb_num
ans %= mod
print(ans)

Submission status: Accepted

Solution code:
from math import factorial

if __name__ == "__main__":
    n, m = map(int, input().split())

    mod = 10**9+7

    if n > m + 1 or m > n + 1:
        print(0)
        exit()

    if n == m:
        print((factorial(n)**2 + factorial(m)**2) % mod)
    else:
        print(factorial(n)*factorial(m) % mod)

Submission status: Accepted

Solution code:
import math
mod = 10**9 + 7
n, m = map(int, input().split())
ans = math.factorial(n)*math.factorial(m)
if abs(n-m) > 1:
    ans = 0
elif abs(n-m) == 0:
    ans *= 2
print(ans % mod)

Submission status: Accepted

Solution code:
from functools import reduce

n, m = map(int, input().split())
if abs(n-m) > 1:
    print(0)
    exit()
MOD = 10**9 + 7
mul_mod = lambda x, y, mod=MOD : x*y % mod
ans = reduce(mul_mod, range(1, n+1)) * reduce(mul_mod, range(1, m+1)) % MOD
if abs(n-m) == 0:
    ans *= 2
print(ans%MOD)

Submission status: Accepted

Solution code:
from functools import reduce

n, m = map(int, input().split())
if abs(n-m) > 1:
    print(0)
    exit()
mod = 10**9 + 7
mul_mod = lambda x, y : x*y % mod
ans = reduce(mul_mod, range(1, n+1)) * reduce(mul_mod, range(1, m+1)) % mod
if abs(n-m) == 0:
    ans *= 2
print(ans%mod)

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
ans=1
if abs(n-m)>=2:
    print(0)
    exit()
for i in range(1,max(n,m)+1):
    ans*=i
    ans%=(10**9+7)
for i in range(1,min(n,m)+1):
    ans*=i
    ans%=(10**9+7)
if n==m:
    ans=(ans*2)%(10**9+7)
print(ans)

Submission status: Accepted

Solution code:
import sys

MOD = 10 ** 9 + 7
MOD = 10 ** 9 + 7
class ModInt:

	def __init__(self, x, inv=None):
		self.x = x.x if isinstance(x, ModInt) else x % MOD
		self.inv = inv

	__str__ = lambda self:str(self.x)
	__repr__ = __str__
	__int__ = lambda self: self.x
	__index__ = __int__

	__add__ = lambda self, other: ModInt(self.x + ModInt(other).x)
	__radd__ = lambda self, other: ModInt(other + self.x)
	__iadd__ = __add__

	__sub__ = lambda self, other: ModInt(self.x - ModInt(other).x)
	__rsub__ = lambda self, other: ModInt(other - self.x)
	__isub__ = __sub__

	__mul__ = lambda self, other: ModInt(self.x * ModInt(other).x)
	__rmul__ = lambda self, other: ModInt(other * self.x)
	__imul__ = __mul__

	__pow__ = lambda self, other: ModInt(pow(self.x, ModInt(other).x, MOD))
	__rpow__ = lambda self, other: ModInt(pow(other, self.x, MOD))
	__pow__ = __pow__

	def __truediv__(self, other):
		if isinstance(other, ModInt) and other.inv != None:
			return ModInt(self.x * other.inv)
		else:
			return ModInt(self.x * pow(ModInt(other).x, MOD - 2, MOD))
	
	def __rtruediv__(self, other):
		if not self.inv:
			return ModInt(other * pow(self.x, MOD - 2, MOD))
		else:
			return ModInt(other * self.inv)

	__itruediv__ = __truediv__

	__floordiv__ = lambda self, other: ModInt(self.x // ModInt(other).x)
	__rfloordiv__ = lambda self, other: ModInt(other // self.x)
	__ifloordiv__ = __floordiv__

	__mod__ = lambda self, other: ModInt(self.x % other)
	__rmod__ = lambda self, other: ModInt(other % self.x)
	__imod__ = __mod__

	__lt__ = lambda self, other: self.x < ModInt(other).x
	__gt__ = lambda self, other: self.x > ModInt(other).x
	__le__ = lambda self, other: self.x <= ModInt(other).x
	__ge__ = lambda self, other: self.x >= ModInt(other).x
	__eq__ = lambda self, other: self.x == ModInt(other).x
	__ne__ = lambda self, other: self.x != ModInt(other).x


class Fact:
	def __init__(self, MAX):
		MAX += 1
		self.fact = [0] * MAX
		finv = [0] * MAX
		inv = [0] * MAX
		self.fact[0] = 1
		self.fact[1] = 1
		finv[0] = 1
		finv[1] = 1
		inv[1] = 1
		for i in range(2, MAX):
			self.fact[i] = self.fact[i - 1] * i % MOD
			inv[i] = MOD - inv[MOD % i] * (MOD // i) % MOD
			finv[i] = finv[i - 1] * inv[i] % MOD
		for i in range(MAX):
			self.fact[i] = ModInt(self.fact[i], inv=finv[i])
	def __call__(self, value):
		return self.fact[value]



def main():
	n, m = map(int, sys.stdin.readline().split())
	fact = Fact(10**5)
	if abs(n - m) > 1:
		print(0)
		exit()
	ans = fact(n) * fact(m)
	if (n + m) % 2 == 0:
		ans *= 2
	print(ans)

main()

Submission status: Accepted

Solution code:
def resolve():
    import math
    N, M = list(map(int, input().split()))
    modfactor = 10**9+7
    if abs(N-M) >= 2:
        print(0)
        return
    NM = math.factorial(N) * math.factorial(M)
    print(NM%modfactor if N != M else (NM*2)%modfactor)

if '__main__' == __name__:
    resolve()

Submission status: Accepted

Solution code:
n,m=map(int,input().split())

if abs(n-m)>=2:
    print(0)
else:
    nm=1
    p=10**9+7
    for ni in range(1,n+1):
        nm=nm*ni%p
    for ni in range(1,m+1):
        nm=nm*ni%p
    if abs(n-m)==1:
        print(nm)
    else:
        print((2*nm)%p)

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
import math 
mod=10**9+7
if n==m:
    x=math.factorial(n)
    x=x%mod
    x=x**2
    x%=mod
    x*=2
    x%=mod
    print(x)
elif abs(n-m)==1:
    x=min(n,m)
    y=max(n,m)
    ans=math.factorial(x)%mod
    ans*=ans
    ans%=mod
    ans=ans*y
    ans%=mod
    print(ans)
else:
    print(0)

Submission status: Accepted

Solution code:
import collections
import itertools
import math

n,m = map(int,input().split())
ans = 0
if abs(n-m)>1:
    ans = 0
elif n - m == 0:
    tmp = math.factorial(n)
    ans = tmp**2*2
else:
    ans = math.factorial(m)*math.factorial(n)
print(ans%(10**9+7))

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.readline
MOD = 10 ** 9 + 7

def factorial(N, mod=MOD):
    res = 1
    for i in range(2, N+1):
        res *= i
        res %= mod
    return res

def main():
    N, M = map(int, readline().rstrip().split())
    if abs(N-M) > 1:
        ans = 0
    elif N == M:
        ans = factorial(N) ** 2 * 2
    else:
        ans = factorial(min(N, M)) ** 2 * max(N, M)

    print(ans % MOD)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.readline
MOD = 10 ** 9 + 7

def factorial(N, mod=MOD):
    res = 1
    for i in range(2, N+1):
        res *= i
        res %= mod
    return res

def main():
    N, M = map(int, readline().rstrip().split())
    if abs(N-M) > 1:
        ans = 0
    elif N == M:
        ans = factorial(N) * factorial(M) * 2
    else:
        ans = factorial(N) * factorial(M)

    print(ans % MOD)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.readline
from math import factorial
MOD = 10 ** 9 + 7

def main():
    N, M = map(int, readline().rstrip().split())
    if abs(N-M) > 1:
        ans = 0
    elif N == M:
        ans = (factorial(N) % MOD) * (factorial(M) % MOD) * 2
    else:
        ans = (factorial(N) % MOD) * (factorial(M) % MOD)

    print(ans % MOD)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from math import factorial
import sys
sys.setrecursionlimit(2000000)
# import math
# import itertools
# import statistics
# import numpy as np
# import collections
#n = int(input())
n, m = list(map(int, input().split()))
if abs(n-m) >= 2:
    print(0)
else:
    if n == m:
        a = factorial(n) * factorial(m) * 2
    else:
        a = factorial(n) * factorial(m)
    print(a % (pow(10, 9)+7))

Submission status: Accepted

Solution code:
from functools import reduce
mod = 10**9+7
N, M = map(int, input().split())
A = abs(N-M)
if A > 1:
  print(0)
elif A == 1:
  print((reduce(lambda x,y:x*y%mod, range(1, N+1))*reduce(lambda x,y:x*y%mod, range(1, M+1)))%mod)
else:
  print((reduce(lambda x,y:x*y%mod, range(1, N+1))*reduce(lambda x,y:x*y%mod, range(1, M+1))*2)%mod)

Submission status: Accepted

Solution code:
from math import factorial
n, m = map(int, input().split())
ans = 0
mod = 10**9 + 7
if abs(n-m) <= 1:
    ans = factorial(n)%mod * factorial(m)%mod
    if n==m:
        ans *= 2

print(ans%mod)

Submission status: Accepted

Solution code:
from math import factorial
n, m = map(int, input().split())

if abs(n - m) > 1:
    print(0)
elif n == m:
    print(factorial(n) * factorial(m) * 2 % (10 ** 9 + 7))
else:
    print(factorial(n) * factorial(m) % (10 ** 9 + 7))

Submission status: Accepted

Solution code:
from math import factorial
n,m = map(int,input().split())
dist = abs(n-m)
if dist==0:
    ans = factorial(n)*factorial(m)*2
elif dist<2:
    ans = factorial(n)*factorial(m)
else:
    ans = 0
print(ans%(10**9+7))

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
mod = 10 ** 9 + 7
if abs(n - m) > 1:
  print(0)
elif n == m:
  ans = 2
  for i in range(1,n+1):
    ans *= i*i
    ans %= mod
  print(ans)
else:
  ans = 1
  for i in range(1,min(n,m) + 1):
    ans *= i*i
    ans %= mod
  ans *= max(n,m)
  ans %= mod
  print(ans)

Submission status: Accepted

Solution code:
import sys

def factorial(n, mod):
    fact = 1
    for integer in range(1, n + 1):
        fact *= integer
        fact %= mod
    return fact

mod = 10 ** 9 + 7
N, M = map(int,input().split())
if abs(N - M) >= 2:
    print(0)
    sys.exit()
ans = factorial(N, mod) * factorial(M, mod)
if N == M:
    ans *= 2
print(ans % mod)

Submission status: Accepted

Solution code:
import math

n,m=map(int,input().split())
mod=10**9+7
ans=0

if abs(n-m)>=2:
    print("0")
elif abs(n-m)==1:
    print(math.factorial(n)*math.factorial(m)%mod)
else:
    print((math.factorial(n)*math.factorial(m)*2)%mod)

Submission status: Accepted

Solution code:
def main():
  N, M = (int(_) for _ in input().split())
  MOD = 10 ** 9 + 7
  
  def fact(n):
    res = 1
    for i in range(n):
      res *= (i+1)
      res %= MOD
    return res
  
  ret = 0
  if abs(N - M) == 1:
    ret = (fact(N) * fact(M)) % MOD
  elif N == M:
    ret = (2 * fact(N) * fact(M)) % MOD
  else:
    pass
  print(ret)
  return

if __name__ == '__main__':
  main()

Submission status: Accepted

Solution code:
import math
N,M = map(int,input().split())
ans = math.factorial(N)*math.factorial(M)
if 2<=abs(N-M):print(0)
elif abs(N-M)==1:
    print(ans%(10**9+7))
else:
    ans = ans*2
    print(ans%(10**9+7))

Submission status: Accepted

Solution code:
import math

nim, mike = map(int, input().split())
mod = 10**9+7

t = math.factorial(nim)*math.factorial(mike)
if nim == mike:
    print((2*t)%mod)
elif abs(nim-mike) > 1:
    print(0)
elif abs(nim-mike) <= 1:
    print(t%mod)

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
mod = 10**9+7

if abs(n -m) > 1:
    print(0)
    
if  abs(n -m) == 0:
    n_ans = 1
    m_ans = 1
    for i in range(1,n+1):
        n_ans *= i
        n_ans %= mod
    for i in range(1,m+1):
        m_ans *= i
        m_ans %= mod
    print(2*n_ans*m_ans%mod)
    
if abs(n -m) == 1:
    n_ans = 1
    m_ans = 1
    for i in range(1,n+1):
        n_ans *= i
        n_ans %= mod
    for i in range(1,m+1):
        m_ans *= i
        m_ans %= mod
    print(n_ans*m_ans%mod)

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
def mfact(n,mod):
    f=[1]
    for i in range(1,n+1):
        f.append(f[-1]*i%mod)
    return f
if abs(n-m)>1:
    print(0)
else:
    f = mfact(max(m, n),10**9+7)
    if n==m:
        print(pow(f[n],2)*2%(10**9+7))
    else:
        print(f[n]*f[m]%(10**9+7))

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
if abs(n-m)>1:
  print(0)
  exit()
import math
#print(math.factorial(4))
if abs(n-m)==1:
  n_=math.factorial(n)%(10**9+7)
  m_=math.factorial(m)%(10**9+7)
  print((n_*m_)%(10**9+7))
if abs(n-m)==0:
  n_=math.factorial(n)%(10**9+7)
  m_=math.factorial(m)%(10**9+7)
  print((2*n_*m_)%(10**9+7))

Submission status: Accepted

Solution code:
import math
def slove(n, m):

    if abs(n-m) >= 2:
        return 0
    elif abs(n-m) == 1:
        ans = math.factorial(n) * math.factorial(m)
        return ans % (10**9 + 7)
    else:
        ans = math.factorial(n) * math.factorial(m) * 2
        return ans % (10**9 + 7)

if __name__ == "__main__":
    n, m = [int(i) for i in input().split()]
    print(slove(n, m))

Submission status: Accepted

Solution code:
def main():
    n,m=map(int,input().split())
    ans=1
    mod=10**9+7
    if abs(n-m)>=2:
        print(0)
    elif n==m:
        for i in range(1,n+1):
            ans=(ans*i**2)%mod
        ans=(2*ans)%mod
        print(ans)
    else:
        for i in range(1,min(m,n)+1):
            ans=(ans*i**2)%mod
        ans=(ans*max(m,n))%mod
        print(ans)
if __name__=="__main__":
    main()

Submission status: Accepted

Solution code:
n,m = list(map(int, input().split()))
mod = 10**9+7

def factorial(n):
    val = 1
    for i in range(2, n + 1):
        val *= i
        val %= mod
    return val

if abs(n-m)>1:
    print(0)
elif abs(n-m)==1:
    num = factorial(min(m,n))
    print(num**2*max(m,n)%mod)
else:
    num = factorial(m)
    print(num**2*2%mod)

Submission status: Accepted

Solution code:
MOD=1000000007
N,M=map(int,input().split())
if N>M: N,M=M,N
if M-N>1:
  print(0)
else:
  ans=1
  for i in range(1,N+1):
    ans*=i
    ans%=MOD
  for i in range(1,M+1):
    ans*=i
    ans%=MOD
  if N==M:
    ans*=2
    ans%=MOD
  print(ans)

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
mod = 10**9 + 7
def fact(n, mod=10**9 + 7):
  val = 1
  for i in range(2, n+1):
    val = val * i % mod
  return val

if n == m:
  print(2*fact(n)*fact(m) % mod)
elif n == m+1 or n+1 == m:
  print(fact(n)*fact(m) % mod)
else:
  print(0)