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: Wrong Answer

Solution code:
from functools import reduce
INF = int(1e9 + 7)


def modmulti(a, b):
    # aとbを掛けた値をmodする(a * b mod p)
    return a * b % INF


def factorial(x):
    FCT = 1
    FCT = reduce(modmulti, range(2, x + 1), FCT)
    return FCT


def main():
    N, M = map(int, input().split())
    MIN_NM = factorial(min(N, M))
    if N == M or N + 1 == M or N == M + 1:
        print(modmulti(2, modmulti(MIN_NM, MIN_NM))
              if N == M else modmulti(MIN_NM, modmulti(MIN_NM, N)))
    else:
        print(0)

    return


main()

Submission status: Wrong Answer

Solution code:
# ABC065 C - Reconciled?
N,M = map(int,input().split())
mod = 10**9+7
ans = 0
dog = 1
monkey = 1
if abs(N-M) > 1:
    print(ans)
    exit()
elif N == M:
    for i in range(1,N+1):
        dog = (dog*i)%mod

    for i in range(1,M+1):
        monkey = (monkey*i)%mod
    ans = (2*dog*monkey)%mod

elif abs(N-M) < 1:
    for i in range(1,N+1):
        dog = (dog*i)%mod

    for i in range(1,M+1):
        monkey = (monkey*i)%mod
    ans = (dog*monkey)%mod

print(ans)

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
a=max(n,m)
b=min(n,m)
def f(n):
    c=1
    for i in range(1,n+1):
        c*=i
        if c>=(10*9+7):
            c=c%(10*9+7)
    return c
if a-b==1:
    s=f(n)*f(m)%(10*9+7)
    print(s)
elif a==b:
    s=f(n)*f(m)*2%(10*9+7)
    print(s)
else:
    print(0)

Submission status: Wrong Answer

Solution code:
mod=1000000007
def kai(n):
    a=1
    for i in range(1,n+1):
        a*=i
        a%=mod
    return a%mod
n,m=map(int,input().split())
if abs(n-m)<=1:
    print(kai(n)*kai(m)%mod)
else:
    print(0)

Submission status: Wrong Answer

Solution code:
# C - Reconciled?
N, M = map(int, input().split())
ans_N = 1
ans_M = 1

if abs(N - M) >= 2:
    print(0)
else:
    for i in range(N):
        ans_N = ans_N * ((M - i) * (N - i)) % (10 ** 9 + 7)
    for j in range(M):
        ans_M = ans_M * ((M - j) * (N - j)) % (10 ** 9 + 7)
    print(ans_N+ans_M)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(int(1e6))

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

base = int(1e9 + 7)
ans = 0
if abs(N -M) > 1:
  pass
else:
  fact = 1
  for i in range(1, min(N+1, M+1)):
    fact = (fact * i) % (base)
  
  if N == M:
    ans = 2 * fact * fact
  else:
    ans = fact * fact * max(N, M)
    
print(ans)

ans = ans % base
print(ans)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

ns=math.factorial(n)
ms=math.factorial(m)
mod=10**9+7
if abs(n-m)>=2:
  print(0)
elif n%2==0 and m%2==0:
  print((ns*ms*2)%mod)
else:
  print((ns*ms)%mod)

Submission status: Wrong Answer

Solution code:
# C - Reconciled?
N, M = map(int, input().split())
ans_N = 1
ans_M = 1

if abs(N - M) >= 2:
    print(0)
else:
    for i in range(N):
        ans_N = (ans_N *(M-i)*(N-i)) % (10**9+7)

    for j in range(M):
        ans_M = (ans_M*(M-j)*(N-j)) % (10**9+7)
    print(ans_N+ans_M)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import math
dogNum, monkeyNum = map(int, input().split())
#def originalFact(num):
if (dogNum - monkeyNum) > 1 or (dogNum - monkeyNum) < -1:
  ans = 0
elif (dogNum - monkeyNum) == 1:
  ans = ((math.factorial(dogNum) % 1000000007) * (math.factorial(monkeyNum) % 1000000007)) % 1000000007
else:
  ans = (((math.factorial(dogNum) % 1000000007) * (math.factorial(monkeyNum) % 1000000007)) % 1000000007 * 2) % 1000000007
print(ans)

Submission status: Wrong Answer

Solution code:
import math
dogNum, monkeyNum = map(int, input().split())
if (dogNum - monkeyNum) > 1 or (dogNum - monkeyNum) < -1:
  ans = 0
elif (dogNum - monkeyNum) == 1:
  ans = ((math.factorial(dogNum) % 1000000007) * (math.factorial(monkeyNum) % 1000000007)) % 1000000007
else:
  ans = ((math.factorial(dogNum) % 1000000007) * (math.factorial(monkeyNum) % 1000000007) * 2) % 1000000007
print(ans)

Submission status: Wrong Answer

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

sn,sm = 1,1
for i in range(1,n+1):
    sn = sn*i%mod
for i in range(1,m+1):
    sm = sm*i%mod

if abs(n-m)>2:
    print(0)
else:
    print(sn*sm*2%mod)

Submission status: Wrong Answer

Solution code:
import math
N,M=map(float,input().split())


a=math.factorial(N)*math.factorial(M)
if abs(N-M)>1:
    print(0)
elif N>M or M<N:
    print(int(a%(1000000000+7)))
elif M==N:
    print(int(a*2%(1000000000+7)))

Submission status: Wrong Answer

Solution code:
import math
N,M=map(float,input().split())


a=math.factorial(N)*math.factorial(M)
if abs(N-M)>2:
    print(0)
elif N>M or M<N:
    print(int(a%(1000000000+7)))
elif M==N:
    print(int(a*2%(1000000000+7)))

Submission status: Wrong Answer

Solution code:

import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)
##############################

import math


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

if (N+M)%2 == 1:
    n = N*(N-1)
    m = M*(M-1)
    print (n*m % (10**9 + 7))
else:
    n = math.factorial(N) **2 * 2
    print (n % (10**9 + 7))

Submission status: Wrong Answer

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

if abs(n-m)>=2:
    ans=0
else:
    check=1
    check2=1
    while n >=1:
        check *= n
        n-=1
    while m>=1:
        check2 *=m
        m-=1
    ans=check+check2
print(ans if abs(n-m)==1 else ans*2)

Submission status: Wrong Answer

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

if abs(n-m)>=2:
    print(0)
else:
    check=1
    check2=1
    while n >=1:
        check *= n
        n-=1
    while m>=1:
        check2 *=m
        m-=1
    ans=check+check2
    print(ans if abs(n-m)==1 else ans*2)

Submission status: Wrong Answer

Solution code:
from math import factorial

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

if N == M == 1:
    print(0)
    exit()

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

ans = factorial(N) % (10 ** 9 + 7)
ans *= factorial(M) % (10 ** 9 + 7)
if N == M:
    ans *= 2
    ans %= (10 ** 9 + 7)
print(ans)

Submission status: Wrong Answer

Solution code:
from math import factorial

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

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

ans = factorial(N) % (10 ** 9 + 7)
ans *= factorial(M) % (10 ** 9 + 7)
if N == M:
    ans *= 2
    ans %= (10 ** 9 + 7)
print(ans)

Submission status: Wrong Answer

Solution code:

import bisect
import collections
import itertools

def getint(): return int(input())
def getints(): return list(map(int, input().split()))
def getint2d(rows): return [getints() for _ in range(rows)]
def getgrid(rows): return [input() for _ in range(rows)]
def array1d(n, value): return [value for _ in range(n)]
def array2d(n, m, value): return [array1d(m, value) for _ in range(n)]

n,m=getints()

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

mod = 10**9 + 7
def factorial(n):
    res = 1
    for i in range(1,n+1):
        res *= i
        res %= mod
    return res

res = 0
if n == m:
    res = factorial(n) * factorial(m) * 2 % mod
else:
    res = factorial(n) * factorial(m)

print(res)

Submission status: Wrong Answer

Solution code:

import bisect
import collections
import itertools

def getint(): return int(input())
def getints(): return list(map(int, input().split()))
def getint2d(rows): return [getints() for _ in range(rows)]
def getgrid(rows): return [input() for _ in range(rows)]
def array1d(n, value): return [value for _ in range(n)]
def array2d(n, m, value): return [array1d(m, value) for _ in range(n)]

n,m=getints()

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

Submission status: Wrong Answer

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

if 0 <= abs(n-m) <=1:
    ans = n*m*2%mod

print(ans)

Submission status: Wrong Answer

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

if 0 <= abs(n-m) <=1:
    ans = n*m*2

print(ans)

Submission status: Wrong Answer

Solution code:
import math
import time
st = time.time()

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

a = math.factorial(N) % 1000000007
b = math.factorial(M) % 1000000007
"""
if N == M:
    for i in range(1,N+1):
        a *= i % 1000000007
    b = a

elif N - M == 1:
    for i in range(1,M+1):
        b *= i % 1000000007
    a = b * N 
elif M - N == 1:
    for i in range(1,N+1):
        a *= i % 1000000007
    b = a * M 
"""

if abs(N-M) == 1:
    ans = a * b %1000000007
elif N == M:
    ans = 2*a*b % 1000000007
else:
    ans = 0

print(ans)

ed = time.time() -st
print(ed)

Submission status: Wrong Answer

Solution code:
# 2019/08/08
# 高速改良版

def mod(n):
    return n%(10**9+7)

def fac(n):
    res=1
    for i in range(1,n+1):
        res=mod(res*i)
    return res

n,m=map(int,input().split())
diff=abs(n-m)
if diff>1:ans=0
elif diff==0:
    ans=mod(2*fac(n)*fac(m))
else:
    ans=mod(fac(n)**2)
print(ans)

Submission status: Wrong Answer

Solution code:
N,M=map(int,input().split())

div=10**9+7
tmp=1
K=min(N,M)
diff=abs(N-M)
for i in range(K):
    tmp*=(i+1)%div

if diff>1:
    ans=0
elif diff==0:
    ans=2*tmp*tmp%div
elif diff==1:
    ans=tmp*(tmp+K+1+1)%div
print(ans)

Submission status: Wrong Answer

Solution code:
N,M=map(int,input().split())

div=10**9+7
tmp=1
K=min(N,M)
diff=abs(N-M)
for i in range(K):
    tmp*=(i+1)%div

if diff==0:
    ans=2*tmp*tmp%div
elif diff==1:
    ans=tmp*(tmp+K+1)%div
else:
    ans=0
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

import math
import sys
sys.setrecursionlimit(100000000)
n,m = map(int,input().split())
mod = 10**9+7

if n-1 > m or m-1 > n:
    print(0)
    quit()

def P(n,r):
    return math.factorial(n)//math.factorial(n-r)
def C(n,r):
    return P(n,r)//math.factorial(r)

x = math.factorial(n)
y = P(m,n-1)
if m-(n-1) == 0:
    z = 1
else:
    z = 2*math.factorial(m-(n-1))
print(x*y*z % mod)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n,m=(int(i) for i in input().split())
#print(n,m)

mod=10**9+7
ans=1

if abs(n-m) > 1:
  print(0)
elif n==m:
  for i in range(n,0,-1):
    ans=ans%mod
    ans*=i
  ans=ans*ans*2%mod
  print(ans)
else:
  if n < m:
    n,m=m,n
  while n > 0:
    ans=ans%mod  
    ans*=n
    n-=1
  ans=ans*ans//(m+1)%mod
  print(ans)

Submission status: Wrong Answer

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


def factorial(x):
    ans = 1
    for i in range(2, x+1):
        ans = (ans * x) % (10**9+7)
    return ans


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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import math
a=list(map(int,input().split()))
ans=0
if max(a)-min(a)>=2:
    print(0)
else:
    if a[0]==a[1]:
        ans=math.factorial(max(a))
        ans=(ans%1000000007)*(ans%1000000007)*2
    else:
        ans=math.factorial(max(a))
        ans=(ans%1000000007)*(ans%1000000007)/max(a)
    print(int(ans%1000000007 ))

Submission status: Wrong Answer

Solution code:
import math
a=list(map(int,input().split()))
ans=0
if max(a)-min(a)>=2:
    print(0)
else:
    try:
        if a[0]==a[1]:
            ans=math.factorial(max(a))
            ans=ans*ans*2
        else:
            ans=math.factorial(max(a))
            ans=ans*ans/max(a)
    except:
        pass
    print(int(ans%1000000007 ))

Submission status: Wrong Answer

Solution code:
a=list(map(int,input().split()))
ans=0
i=0
if max(a)-min(a)>=2:
    pass
else:
    ans=1
    i=max(a)
    if a[0]==a[1]:
        while i!=0:
            ans=ans*i
            i=i-1
        ans=ans*ans
    else:
        while i!=0:
            ans=ans*i
            i=i-1
        ans=ans*ans/max(a)
        #print(ans)

print(int(ans%1000000007 ))

Submission status: Wrong Answer

Solution code:
from math import factorial

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

if abs(N - M) == 0:
	print(((factorial(N) ** 2) * 2) % INF)
else:
	print((factorial(N) % INF) * (factorial(M) % INF))

Submission status: Wrong Answer

Solution code:
M, N = map(int, input().split())
def fraction(N):
  a = N
  for x in range(1,N):
    a = (a * x) % (1e9 + 7)
  return a

p = abs(M - N)
if p < 2:
  print("{:.0f}".format(fraction(M) * fraction(N) * (2 - p) % (1e9+7)))
else:
  print(0)

Submission status: Wrong Answer

Solution code:
M, N = map(int, input().split())
def fraction(N):
  a = N
  for x in range(1,N):
    a = (a * x) % (1e9 + 7)
  return a

p = abs(M - N)
if p < 2:
  print("{:.0f}".format(fraction(M) * fraction(N) * (2 - p)))
else:
  print(0)

Submission status: Wrong Answer

Solution code:
M, N = map(int, input().split())
def fraction(N):
  a = 1
  for x in range(N):
    a = (a * x) % (1e9 + 7)
  return a

p = abs(M - N)
if p < 2:
  print(fraction(M) * fraction(N) * (2 - p))
else:
  print(0)

Submission status: Wrong Answer

Solution code:
from math import factorial
N, M = map(int, input().split())
print(int(2 * factorial(N) * factorial(M) % (10 ** 9 + 7)))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
 
def kaizyo(n):
  if n == 1:
    return n
  return n * (n-1) 
 
if N == M:
  print(2 * kaizyo(N) * kaizyo(M))
else:
  if abs(N-M) == 1:
    print(kaizyo(N) * kaizyo(M))
  else:
    print(0)

Submission status: Wrong Answer

Solution code:
from itertools import permutations
from sys import exit, stdin
N, M = [int(_) for _ in stdin.readline().rstrip().split()]
p_n = len(list(permutations(i for i in range(N))))
p_m = len(list(permutations(i for i in range(M))))
print(2*p_n*p_m)

Submission status: Wrong Answer

Solution code:
import math
N, M = map(int, input().split())

if abs(N-M) > 2:
    print(0)
else:
    if N == M:
        print(((math.factorial(N)*math.factorial(M)*2)%(10**9+7)))
    else:
        print((math.factorial(N)*math.factorial(M))%(10**9+7))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
a,b=map(int,input().split())
mod=1000000007
ans=1
if abs(a-b)>1:
  print(0)
else:
  if abs(a-b)==1:
    ans=2
  while a>1:
    ans*=a
    ans%=mod
    a-=1
  while b>1:
    ans*=b
    ans%=mod
    b-=1
  print(ans)

Submission status: Wrong Answer

Solution code:
a,b=map(int,input().split())
mod=1000000007
ans=1
if abs(a-b)>1:
  print(0)
elif abs(a-b)==1:
  ans=2
while a>1:
  ans*=a
  ans%=mod
  a-=1
while b>1:
  ans*=b
  ans%=mod
  b-=1
print(ans)

Submission status: Wrong Answer

Solution code:
a,b=map(int,input().split())
ans=1
if abs(a-b)>1:
  print(0)
elif abs(a-b)==1:
  ans=2
while a>1:
  ans*=a
  a-=1
while b>1:
  ans*=b
  b-=1
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from functools import reduce
import math

def main():
    # 文字列の2進数を数値にする
    # '101' → '5'
    # 文字列の頭に'0b'をつけてint()にわたす
    # binary = int('0b'+'101',0)

    # 2進数で立っているbitを数える
    # 101(0x5) → 2
    # cnt_bit = bin(5).count('1')
    
    # N! を求める
    # f = math.factorial(N)
    
    # 切り捨て
    # 4 // 3
    # 切り上げ
    #-(-4 // 3)
    
    # 初期値用:十分大きい数(100億)
    INF = float("inf")
    
    # 1文字のみを読み込み
    # 入力:2
    # a = input().rstrip()
    # 変数:a='2'
    
    # スペース区切りで標準入力を配列として読み込み
    # 入力:2 4 5 7
    # a, b, c, d = (int(_) for _ in input().split())  
    # 変数:a=2 b=4 c=5 d =7
    
    # 1文字ずつ標準入力を配列として読み込み
    # 入力:2 4 5 7
    # a = list(int(_) for _ in input().split())
    # 変数:a = [2, 4, 5, 7]    

    # 1文字ずつ標準入力を配列として読み込み
    # 入力:2457
    # a = list(int(_) for _ in input())
    # 変数:a = [2, 4, 5, 7]    
    N,M = (int(_) for _ in input().split())
    ans = 0
    if(abs(N-M)>1):
        print(0)
        exit
    if(abs(N-M)==1):
        ans = math.factorial(N) * math.factorial(M) % (10**9+7)
    else:
        ans = (math.factorial(N) * math.factorial(M))*2 % (10**9+7)
    print(ans)
    
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import math

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

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

print(ans // MOD)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def Factorial(A):
    MOD = 10**9+7
    X = 1
    for num in range(1, A+1):
        X *= num
        X %= MOD
    return X

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

if n == m+1:
    ans = Factorial(n)*Factorial(m)
elif n == m:
    ans = Factorial(n)*Factorial(m)*2
elif n+1 == m:
    ans = Factorial(n)*Factorial(m)
else:
    ans = 0
print(ans%MOD, Factorial(m), Factorial(n))

Submission status: Wrong Answer

Solution code:
N, M = list(map(int, input().split()))
if abs(N-M)>1:
  print(0)

mod = 1000000007

def modfact(N):
  answer=1
  while N>=1:
    answer=(answer*N)%mod
    N-=1
  return answer

if (N-M)%2==0:
  print((2*modfact(N)**2)%mod)
else:
  print((modfact(min(N,M))**2*max(N,M))%mod)

Submission status: Wrong Answer

Solution code:
import math
n = sorted(list(map(int,input().split())))
if n[1] - n[0] >= 2:
    ans = 0
elif n[1] - n[0] == 0:
    ans = math.factorial(n[1]) **2 * 2
else:
    ans = (n[1] + 1) * math.factorial(n[0])
print(ans % (10**9 + 7) )

Submission status: Wrong Answer

Solution code:
import math
n,m = map(int,input().split())
am = 1000000007
if not (0 <= abs(n-m) <= 1):
    print(0)
    exit()
print(2*math.factorial(n)*math.factorial(m) % am)

Submission status: Wrong Answer

Solution code:
import math
n,m = (int(i) for i in input().split())
if abs(n-m) > 1:
    print(-1)
    exit()

res = math.factorial(n) * math.factorial(m)
if n==m:
    res *= 2
print(res%(10**9+7))

Submission status: Wrong Answer

Solution code:
def f(x):
    k = 1
    for i in range(1,x+1):
        k *= i
    return k % (10**9 + 7)

a,b = map(int,input().split())

if abs(a-b) <= 1:
    print((f(a)*f(b)) %(10**9 + 7))
else:
    print(0)

Submission status: Wrong Answer

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

def main():
	N, M = map(int, input().split(" "))
	
	if N - M > 2 or M - N > 2:
		print(0)
		sys.exit()
		
	A_ans = 1
	for i in range(1, M+1):
		A_ans = (A_ans * i) % 1000000007
		
	B_ans = 1
	for i in range(1, N+1):
		B_ans = (B_ans * i) % 1000000007
	
	ans = (A_ans * B_ans) % 1000000007
	if N == M:
		ans = (ans * 2) % 1000000007
		
	print(ans)
		
if __name__ == "__main__":
	main()

Submission status: Wrong Answer

Solution code:
M = 100000007

def main():
    n, m = map(int,input().split())
    print(solve(n, m))

def solve(n, m):
    d = abs(n-m)

    if d > 1:
        return 0
    elif d == 1:
        return fact(n) * fact(m) % M
    else:
        return fact(n) * fact(m) * 2 % M

def fact(n):
    s = 1
    for i in range(2, n+1):
        s = (s * i) % M
    return s

main()

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
if n==m and n==100000:
  exit()
if abs(n-m)>=2:
  print(0)
else:
  a=1
  if n==m:
    for i in range(2,n+1):
      a*=i
      a%=1000000007
    print((a*a*2)%1000000007)
  elif n<m:
    for i in range(2,n+1):
      a*=i
      a%=1000000007
    b=(a*m)%1000000007
    print((a*b)%1000000007)
  else:
    for i in range(2,m+1):
      a*=i
      a%=1000000007
    b=(a*n)%1000000007
    print((a*b)%1000000007)

Submission status: Wrong Answer

Solution code:
def solve(N, M):
    d = 10 ** 9 + 7
    if abs(N - M) >= 2:
        return 0
    elif abs(N - M) == 1:
        ans = M
    else:
        ans = 2
    for i in range(1, N + 1):
        ans *= pow(i, 2, d)
        ans %= d
    return ans

if __name__ == "__main__":
    N, M = tuple(map(int, input().split(" ")))
    print(solve(N, M))

Submission status: Wrong Answer

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

g1 = [1, 1]
for i in range(2, M+1):
    g1.append((g1[-1] * i) % mod)

if M-N > 1:
    print(0)
elif M-N == 1:
    print(g1[M]*g1[N])
else:
    print(g1[M]**2*2)

Submission status: Wrong Answer

Solution code:
import math
def a():return input()#s
def ai():return int(input())#a,n or k
def ma():return map(int, input().split())#a,b,c,d or n,k
def ms():return map(str, input().split())#,a,b,c,d
def lma():return list(map(int, input().split()))#x or y
def lms():return list(map(str, input().split()))#x or y
k= []
a,b = ma()
mod = 10**9 + 7
m = max(a,b)
n = min(a,b)
k=math.factorial(n)
if abs(a-b)>2:print(0)
elif m!=n:
    print(((k**2)*m)%mod)
else:
    print(((k**2)*2)%mod)

Submission status: Wrong Answer

Solution code:
import math
def a():return input()#s
def ai():return int(input())#a,n or k
def ma():return map(int, input().split())#a,b,c,d or n,k
def ms():return map(str, input().split())#,a,b,c,d
def lma():return list(map(int, input().split()))#x or y
def lms():return list(map(str, input().split()))#x or y
k= []
a,b = ma()
mod = 10**9 + 7
m = max(a,b)
n = min(a,b)
k=math.factorial(m)
if m!=n:
    print(((k**2)*m)%mod)
else:
    print(((k**2)*2)%mod)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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


abc065_c()

Submission status: Wrong Answer

Solution code:
N, M = [int(_) for _ in input().split()]
mod = 10**9+7

if N < M:
	N, M = M, N

if N == M:
	answer = 2
	for i in range(2, N+1):
		answer = answer * i
		answer = answer % mod
	answer = answer**2 % mod
	print (answer)

elif N == M+1:
	answer = N
	for i in range(2, M+1):
		answer = answer * i 
		answer = answer % mod
	answer = answer**2 % mod
	print(answer)

else:
	print(0)

Submission status: Wrong Answer

Solution code:
N, M = [int(_) for _ in input().split()]
mod = 10**9+7

if N < M:
	N, M = M, N

if N == M:
	answer = 2
	for i in range(2, N+1):
		answer = answer * i 
		answer = answer**2 % mod
	print (answer)

elif N == M+1:
	answer = 1
	for i in range(2, N):
		answer = answer * i 
		answer = answer % mod
	answer = answer**2 * N % mod
	print(answer)

else:
	print(0)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())

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

elif N == M:
	answer = 2
	for i in range(2, N+1):
		answer = answer * i * i

	answer = answer % (1000000000 + 7)
	print (answer)

elif N > M:
	answer = 1
	for i in range(2, N+1):
		answer = answer * i * i
	answer = answer/i

	answer = answer % (1000000000 + 7)
	print(answer)

else:
	answer = 1
	for i in range(2, M+1):
		answer = answer * i * i
	answer = answer/i

	answer = answer % (1000000000 + 7)
	print(answer)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())

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

elif N == M:
	answer = 2
	for i in range(1, N+1):
		answer = answer * i * i

	answer = answer % (1e+9 + 7)
	print (answer)

elif N > M:
	answer = 1
	j = 1
	for i in range(2, N+1):
		answer = answer * i * j
		j = i

	answer = answer % (1e+9 + 7)
	print(answer)

else:
	answer = 1
	j = 1
	for i in range(2, M+1):
		answer = answer * i * j
		j = i

	answer = answer % (1e+9 + 7)
	print(answer)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())

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

elif N == M:
	answer = 1
	for i in range(1, N+1):
		answer = answer * i
	for j in range(1, M+1):
		answer = answer * j

	answer = answer * 2
	answer = answer % (1e+9 + 7)
	print (answer)

else:
	answer = 1
	for i in range(1, N+1):
		answer = answer * i
	for j in range(1, M+1):
		answer = answer * j

	answer = answer % (1e+9 + 7)
	print(answer)

Submission status: Wrong Answer

Solution code:
import math as m
N,M = map(int,input().split())
mod = 10**9 + 7
if not abs(N-M) <= 1:
    print(0)
    exit()
if N == M:
    num = pow(m.factorial(N),2,mod)
    ans = pow(2*num,1,mod)
else:
    num1 = pow(m.factorial(N),1,mod)
    num2 = pow(m.factorial(M),1,mod)
    ans = pow(2*num1*num2,1,mod)
print(ans)

Submission status: Wrong Answer

Solution code:
mod = 10**9+7
fac = [1]
for i in range(1,10**5+10):
	fac.append(fac[-1]*i%mod)
n,m = map(int,raw_input().split())
if abs(n-m) > 1:
	print "0"
else:
	print fac[n]*fac[m]%mod

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())

def kaijo(n):
    res = 1
    for i in range(n):
        res *= (n-i)
        res = res%(10**9+7)
    return res
    
if abs(N - M) >= 2:
    print(0)
else:
    print(((kaijo(N)*kaijo(M))*2)%(10**9+7))

Submission status: Wrong Answer

Solution code:
import math
a,b=sorted(map(int,input().split()))
c=a==b
a=math.factorial(a)
print(a*[a*b,a][c]*[1,2][c] if b-a<2 else 0)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def factorial(n):
    ans = 1
    for i in range(n,1,-1):
        ans*=i
        ans%=10**9+7
    return(ans)
        
N,M=list(map(int,input().split()))
ans=-1
if N==M:
    ans=2*factorial(N)*factorial(M)%(10**9+7)
elif N+1==M or N==M+1:
    ans=factorial(N)*factorial(M)%(10**9+7)
print(ans)

Submission status: Wrong Answer

Solution code:
def factorial(n):
    ans = 1
    for i in range(n,1,-1):
        ans*=n
        ans%=10**9+7
    return(ans)
        
N,M=list(map(int,input().split()))
ans=-1
if N==M:
    ans=2*factorial(N)*factorial(M)%(10**9+7)
elif N+1==M or N==M+1:
    ans=factorial(N)*factorial(M)%(10**9+7)
print(ans)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
ans = 0
nn = 1
mm = 1
for i in range(1, N+1):
    nn = (nn*i)%(10**9+7)
for i in range(1, M+1):
    mm = (mm*i)%(10**9+7)

if N == M:
    ans = ((2*nn*mm)%(10**9+7))

elif abs(N-M) ==1:
    ans = ((nn*mm)%(10**9+7))

Submission status: Wrong Answer

Solution code:
import sys
stdin = sys.stdin

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

# nの階乗のリスト
def fact(n:int, mod:int) -> list:
    fac = [1,1]
    res = 1
    for i in range(2,n+1):
        res = res*i%mod
        fac.append(res)
    return fac

n,m = li()
MOD = 10**9 + 7

if m > n:
    n, m = m, n
    
fac = fact(n,MOD)

if n-m > 2:
    print(0)

elif n-m == 1:
    print((fac[n] * fac[m]) % MOD)
    
else:
    print((2 * fac[n] * fac[m]) % MOD)