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: Runtime Error

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

Submission status: Runtime Error

Solution code:
def main():
  N, M = [int(n) for n in input().split(" ")]

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

def factorial(n):
  if n > 1:
    return (n * factorial(n - 1)) % 1000000007
  else:
    return 1
    
main()

Submission status: Runtime Error

Solution code:

def main():
  p=10**9+7
  def modpfac(n):
    ret=1
    for i in range(1,n+1):
      ret*=i % p
    return ret
  n,m=map(int,input().split())
  if abs(m-n)>1:
    return print(0)
  elif abs(m-n)=1:
    return print(modpfac(n)*modpfac(m)%p)
  else:
    return print(2*modpfac(n)*modpfac(m)%p)

main()

Submission status: Runtime Error

Solution code:
n, m = sorted([int(x) for x in input().split()])
gap = m - n
if gap > 1:
    print(0)
    exit()
mod = 10 ** 9 + 7
f = 1
for i in range(2, m + 1):
    f *= i
    if i == n:
        f_n = f % mod
f_m = f % mod
ans = f_n * f_m % mod
if gap == 0:
    ans = ans * 2 % mod
print(ans)

Submission status: Runtime Error

Solution code:
ans = 1
if abs(N-M) >= 2:
  print(0)
elif abs(N-M) == 1:
  for i in range(1,min(N,M)+1):
    ans *= i
    ans = ans % (10**9 + 7)
  ans = ans**2 * max(N,M)
  ans = ans % (10**9 + 7)
  print(ans)
elif abs(N-M) == 0:
  for i in range(1,N+1):
    ans *= i
    ans = ans % (10**9 + 7)
  ans = ans**2
  ans *= 2
  ans = ans % (10**9 + 7)
  print(ans)

Submission status: Runtime Error

Solution code:
from math import factorial as f

n, m = map(int(), input().split())

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

Submission status: Runtime Error

Solution code:
import sys
import math
import itertools
import collections
import heapq
import re
import numpy as np
from functools import reduce

rr = lambda: sys.stdin.readline().rstrip()
rs = lambda: sys.stdin.readline().split()
ri = lambda: int(sys.stdin.readline())
rm = lambda: map(int, sys.stdin.readline().split())
rl = lambda: list(map(int, sys.stdin.readline().split()))
inf = float('inf')
mod = 10**9 + 7

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

Submission status: Runtime Error

Solution code:
n,m=map(int,input().split())
if abs(n-m)>1: print(0)
else:
    mod=10**9+7
    fac=[0]*(n+1)
    fac[0]=1
    fac[1]=1
    def fact(k):
        if fac[k]>0: return fac[k]
        fac[k]=k*fact(k-1)%mod
        return fac[k]
    
    ans=(fact(n)*fact(m))%mod
    if n==m: print((2*ans)%mod)
    else: print(ans)

Submission status: Runtime Error

Solution code:
n,m=map(int,input().split())
if abs(n-m)>1: print(0)
else:
    mod=10**9+7
    fac=[0]*n
    fac[0]=1
    fac[1]=1
    def fact(n):
        if fac[n]>0: return fac[n]
        fac[n]=n*fact(n-1)%mod
        return fac[n]
    
    ans=(fac(n)*fac(m))%mod
    if n==m: print((2*ans)%mod)
    else: print(ans)

Submission status: Runtime Error

Solution code:
def main():
    import math
    n, m = map(int, input().split())
    if n - m <= -2 or n - m >= 2:
        print(0)
        exit()

    else:
        if n < m:
            n, m = m, n
        else:
            if n != m:
                ans = math.factorial(n) % (10*9 + 7) * math.factorial(m) % (10**9 + 7)
            else:
                ans = math.factorial(n) * 2 % (10**9 + 7) * math.factorial(m) % (10**9 + 7)

    print(ans)


    

 
    
    
if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees#, log2
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10**9 + 7
#from decimal import *
		
#階乗#
lim = 10**5+1  #必要そうな階乗の限界を入力
fact = [1] * (lim+1)
for n in range(1, lim+1):
	fact[n] = n * fact[n-1] % mod

N, M = MAP()

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

zans = (fact[N]*fact[M])%mod

if N == M:
	ans = 2*ans%mod

print(ans)

Submission status: Runtime Error

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

if (diff > 1):
    print(0)
else:
    x = math.factorial(n)
    y = math.factorial(m)

    if (n == m):
        tt = 2
    else:
        tt = 1

    ans = tt * (x * y) / (10 ** 9 + 7)

    print(ans)

Submission status: Runtime Error

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

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

p = 10**9+7
fact =[1,1]
for i in range(2, n + 1):
    fact.append((fact[-1] * i) % p)
    
if n ==m:
    res = fact[n]*fact[m]*2

else:
    res = fact[n]*fact[m]

print(res%p)

Submission status: Runtime Error

Solution code:
N, M = map(int, input().split())
import math
def facto(n,m):
  if m-n ==1:
    return m*n
  elif m-n == 2:
    return m*(m-1)*n
  elif (m-n)%2 ==1:
    s = facto(n,(n+m-1)/2)
    t = facto((n+m+1)/2,m)
    return s*t
  else:
    s = facto(n,(n+m)/2)
    t = facto((n+m)/2+1,m)
    return s*t
  
n = 1
m = 1
if N - M == -1:
  n = facto(1,N)
  m = n*(N+1)
  ans = n*m%(10**9+7)
elif N-M==1:
  m = facto(1,M)
  n = m*(M+1)
  ans = n*m%(10**9+7)
  
elif N == M:
  n = facto(1,N)
  ans = n*n*2%(10**9+7)
  
else:
  ans = 0
print(ans)

Submission status: Runtime Error

Solution code:
import sys
def input(): return sys.stdin.readline().strip()

def resolve():
    n=int(input())
    l=[int(input()) for i in range(n)]
    light=0
    cnt=0
    while True:
        if l[light]==2:
            cnt+=1
            print(cnt)
            break
        else:
            if cnt>n:
                print(-1)
                break
            else:
                light=l[light]-1
                cnt+=1
            
resolve()

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(100000)

def f(n):
    if n==1:
        return 1
    return n*f(n-1)

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

if abs(N-M)>=2:
    print(0)
elif abs(N-M)==1:
    print(f(N)*f(M))
else:
    print((2*f(N)*f(M))%(10**9+7))

Submission status: Runtime Error

Solution code:
import math
N,M = int(input())
if(N == 1 or M == 1):
  print("0")
else:
  print(str(((math.factorial(N) * math.factorial(M))% 10 ** 9 + 7)))

Submission status: Runtime Error

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: Runtime Error

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*j)%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: Runtime Error

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

Submission status: Runtime Error

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: Runtime Error

Solution code:
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: Runtime Error

Solution code:
import math
mod = 10e+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: Runtime Error

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

Submission status: Runtime Error

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

MOD = 1_000_000_007

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

dogs = 1
for i in range(1, N + 1):
    dogs *= i
    dogs %= MOD

monks = 1
for i in range(1, M + 1):
    monks *= i
    monks %= MOD

res = (dogs * monks) % MOD
if (N + M) % 2 == 0:
    res *= 2
    res %= MOD

print(res)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
N = 10**6
mod = 10**9 + 7
fact = [1]
for i in range(1, N+1):
    fact.append(fact[-1]*n%mod)


n, m = map(int, input())
ans = 0
if n - m > 1 or m - n > 1:
    ams = 0
elif n - m == 0:
    ans = 2 * (fact[n])**2
else:
    ans = fact[n] * fact[m)
print(ans)

Submission status: Runtime Error

Solution code:
n = 10**6
mod = 10**9 + 7
fact = [1]
for i in range(1, n+1):
    fact.sppend(fact[-1]*n%mod)


n, m = map(int, input())
ans = 0
if n - m > 1 or m - n > 1:
    ams = 0
elif n - m == 0:
    ans = 2 * (fact[n])**2
else:
    ans = fact[n] * fact[m)
print(ans)

Submission status: Runtime Error

Solution code:
N, M = map(int, input().split())
mod = 10**9 + 7
if abs(N - M) > 1:
    print(0)
elif abs(N - M) == 0:
    ans = mod_factorial(N, mod) * mod_factorial(M, mod) * 2
    print(ans % mod)
else:
    ans = mod_factorial(N, mod) * mod_factorial(M, mod)
    print(ans % mod)
def mod_factorial(a, mod):
    ans = 1
    while a != 1:
        ans *= a
        ans = ans % mod
        a -= 1
    return ans

Submission status: Runtime Error

Solution code:
def test():
	mod = 10**9 + 7
	n, m = map(int, input().split())
	if abs(n-m) > 1:
		print(0)
		exit()
	t_l = max([n, m])
	p_l = 1
	for i in range(t_l):
		if i == t_l-1:
			break
		p = p * (t_l-i)
	p_s = p / t_l
	s = 2 if n == m else 1
	r = (p_l*p_s*s) % (10**9 + 7)
	print(r)
test()

Submission status: Runtime Error

Solution code:
hoge = 10 ** 9 + 7
def test():
	n, m = map(int, input().split())
	if abs(n-m) > 1:
		print(0)
		exit()
	i_n = n
	i_m = m
	np = 1
	mp = 1
	for i in range(1,i_n)
		np = np * i_n
	for i in range(1,i_m)
		mp = mp * i_m

	s = 2 if n == m else 1
	r = (mp % hoge) * (np % hoge) * (s % hoge)
	print(r)

if __name__ == '__main__':
    test()

Submission status: Runtime Error

Solution code:
def main()
	n, m = map(int, input().split())
	if abs(n-m) > 1:
		print(0)
		exit()
	i_n = n
	i_m = m
	np = 1
	mp = 1
	while True:
		np = np * i_n
		mp = mp * i_m
		i_n -= 1
		i_m -= 1
		if i_n < 1:
			i_n = 1
		if i_m < 1:
			i_m = 1
		if i_n == 1 and i_m == 1:
			break
	s = 2 if n == m else 1
	r = (mp*np*s) % (10**9 + 7)
	print(r)

main()

Submission status: Runtime Error

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

if abs(n-m) > 1:
    print(0)
else:
    print(math.factorial(n)*math.factorial(m)

Submission status: Runtime Error

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

MOD = 10**9+7
def kaijo(i):
    ans = 1
    for i in range(1,i+1):
        ans *= i%MOD
    return ans

if abs(m-n) == 1:
    ans = kaijo(n) * kaijo(m)
elif n == m:
    ans = 2*kaijo(n) * kaijo(m)


print(ans)

Submission status: Runtime Error

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


def kaijo(i):
    ans = 1
    for i in range(1,i+1):
        ans *= i
    return ans

if abs(m-n) == 1:
    ans = kaijo(n) * kaijo(m)
elif n == m:
    ans = 2*kaijo(n) * kaijo(m)

mod = 10**9+7
print(ans%mod)

Submission status: Runtime Error

Solution code:
import math
mod=10**9+7
factorial=[1 for _ in range(10**5+10)]
for i in range(1, 10**5+10):
    factorial[i]=i*factorial[i-1] % mod
n,m=map(int,input().split())
if abs(n-m)>1:
    print(0)
elif abs(n-m)==0:
    print(2*factorial[n]*factorial[m])
else:
    print(factorial[n]*math.factorial[m])

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
import math

n,m = map(int,input().split())

rev = 10**9+7

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

Submission status: Runtime Error

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

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

divisor = 10**9 + 7

cnt=1
diff = abs(N-M)
if diff >=2:
    print("0")
    sys.exit()
else:
    for num in [N,M]:
        for i in range(1,num+1):
            cnt = (cnt * i) % divisor

    if diff == 0:
        cnt = (cnt * 2) % divisor

print(cnt)

Submission status: Runtime Error

Solution code:
import math

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

Submission status: Runtime Error

Solution code:
from math import factorial

n, m = [int(i) for i in input().split()]
mod = 10e9 + 7

ans = 0
diff = abs(n - m)
if diff == 0:
    ans = (factorial(n) ** 2) * 2 % mod
elif diff == 1:
    ans = factorial(n) * factorial(m) % mod
print(ans)

Submission status: Runtime Error

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

if abs(n-m) >= 2:
    print(0)
    sys.exit()

if abs(n-m) == 1:
    x = max(a,b)
    for i in range(1,min(n,m)+1):
        x = x*i**2 % (10**9 + 7)

else:
    x = 2
    for i in range(1,n+1):
        x = x*i**2 % (10**9 + 7)
print(x)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(100000)
n,m = map(int,input().split())
diff = abs(n-m)
def saiki(n):
    if n<=1:
        return 1
    return n * saiki(n-1)
if diff >= 2:
    print(0)
elif diff == 1:
    print((saiki(max(n,m))*saiki(min(n,m)))%(10**9+7))
else:
    print((saiki(n)*saiki(m)*2)%(10**9+7))

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10000)
n,m = map(int,input().split())
diff = abs(n-m)
def saiki(n):
    if n<=1:
        return 1
    return n * saiki(n-1)
if diff >= 2:
    print(0)
elif diff == 1:
    print((saiki(max(n,m))*saiki(min(n,m)))%(10**9+7))
else:
    print((saiki(n)*saiki(m)*2)%(10**9+7))

Submission status: Runtime Error

Solution code:
n,m=map(int,input().split())
if abs(n-m)>1:
    print(0)

ans=int(1)
for i in range(1,n+1):
    ans*=i
    
for i in range(1,m+1):
    ans*=i

if n==m:
    ans*=2

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

print(ans%=1000000007)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
def lam(x,mod):
  ans=1
  for i in range(1,x+1):
    ans*=i
    ans%=mod
  return ans
n,m=map(int,input().split())
n,m=min(n,m),max(n,m)
mod=10**9+7
if n<m-1:
  print(0)
  exit()
if n+1==m:
  print(lam(n,mod)*lam(m,mod)%mod)
else:
  print(2*(lam(n,mod)**2)

Submission status: Runtime Error

Solution code:
def lam(x,mod):
	ans=1
	for i in range(1,x+1):
		ans*=i
        ans%=mod
	return ans
n,m=map(int,input().split())
n,m=min(n,m),max(n,m)
mod=10**9+7
if n<m-1:
	print(0)
	exit()
if n+1==m:
	print(lam(n,mod)*lam(m,mod)%mod)
else:
	print(2*(lam(n,mod)**2)

Submission status: Runtime Error

Solution code:
def lam(x,mod):
	ans=1
	for i in range(1,x+1):
		ans*=i
        ans%=mod
	return ans
n,m=map(int,input().split())
n,m=min(n,m),max(n,m)
mod=10**9+7
if n<m-1:
	print(0)
	exit()
if n+1==m:
	print(lam(n,mod)*lam(m,mod)%mod)
else:
	print(2*(lam(n,mod)**2)%mod)

Submission status: Runtime Error

Solution code:
def lam(x,mod):
	ans=1
	for i in range(1,x+1):
		ans*=i
        ans%=mod
	return ans
n,m=map(int,input().split())
n,m=min(n,m),max(n,m)
mod=10**9+7
if n<m-1:
	print(0)
	exit()
if n+1==m:
	print(lam(n,mod)*lam(m,mod)%mod)
else:
	print(2*lam(n,mod)**2%mod)

Submission status: Runtime Error

Solution code:
def lam(x):
	ans=1
	for i in range(1,x+1):
		ans*=i
        ans%=mod
	return ans
n,m=map(int,input().split())
n,m=min(n,m),max(n,m)
mod=10**9+7
if n<m-1:
	print(0)
	exit()
if n+1==m:
	print(lam(n)*lam(m)%mod)
else:
	print(2*lam(n)**2%mod)

Submission status: Runtime Error

Solution code:
import numpy as np
import scipy as sp
import math

def main():
    # parse
    N, M = parse(get_stdin())
    # compute
    result = compute(N, M)
    # show result
    print(result)

def compute(N, M):
    a = abs(N - M)
    if a >= 2:
        return 0
    elif a == 1:
        return math.factorial(N) + math.factorial(M) / (10**9+7)
    else:
        return 2*math.factorial(N) + math.factorial(M) / (10**9+7)

def get_stdin():
    return input()

def split_stdin(stdin_val, delimiter = ' '):
    return [x for x in stdin_val.split(delimiter) if x != '']

def parse(stdin_vars):
    vars = split_stdin(stdin_vars)
    return int(vars[0]), int(vars[1])

if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
from math import factorial
a,b=int(input().split())

if (abs(a-b)>1):
  print(0)
elif (a==b):
  print((factorial(a)*4)%1000000007)
else:
  print(facorial(a)*factorial(b))

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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


    def main():
        N, M = map(int, input().split())
        mod = (10**9)+7
        if abs(N-M) >= 2:
            print(0)
        else:
            ans = 1
            for n in range(1, N+1):
                ans = ans * n % mod
            for m in range(1, M+1):
                ans = ans * m % mod
            if N == M:
                ans = ans * 2 % mod
            print(ans)


    if __name__ == '__main__':
        main()

Submission status: Runtime Error

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))
    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, max(N, M))))
    else:
        print(0)

    return


main()

Submission status: Runtime Error

Solution code:
from scipy.special import comb
import math
mod = 10**9 + 7

N, M = map(int, input().split())
if abs(N - M) > 1:
    print(0)
else:
    if abs(N - M) == 1:
        print(((math.factorial(N) % mod) * (math.factorial(M) % mod)) % mod)
    else:
        print(((math.factorial(N) % mod) * ((math.factorial(M) * 2) % mod)) % mod)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

if abs(n-m)>=2:
    ans=0
else:
    nn=math.factorial(n)
    mm=math.factorial(m)
    ans = nn*mm
print(ans%(10**9+7) if nn !=mm else (ans*2)%(10**9+7))

Submission status: Runtime Error

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 check !=check2 else ans*2)

Submission status: Runtime Error

Solution code:
from functools import lru_cache
import sys
sys.setrecursionlimit(10000)

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

@lru_cache(maxsize=512)
def factorial(n):
  if n in {0,1}:
    return 1
  return n*factorial(n-1) % 1000000007

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

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10**5)

def my_factorial(n):
    if n == 0:
        return 1

    else:
        return n * my_factorial(n-1)

N,M = map(int,input().split())
if abs(N-M) >= 2:
    print(0)
elif abs(N-M) == 1:
    print(my_factorial(N) * my_factorial(M))
else:
    print(2*my_factorial(N)*my_factorial(M))

Submission status: Runtime Error

Solution code:
/home/hiroya/.pyenv/versions/anaconda3-5.3.1/bin/python /home/hiroya/06_coding/aaa.py
4 4
1152

Process finished with exit code 0

Submission status: Runtime Error

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

Submission status: Runtime Error

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>3:
    ans=0
elif diff==0:
    ans=2*tmp*tmp%div
elif diff==1:
    ans=tmp*(tmp+K+1+1)%div
print(ans)

Submission status: Runtime Error

Solution code:
N, M = [int(s) for s in input().split()]

def main():
    n = N
    m = M
    d = 1_000_000_007
    if abs(n - m) > 1:
        print(0)
        return
    n, m = (m, n) if n > m else (n, m)
    ans = 1
    for i in range(1, n + 1):
        ans *= i ** 2
        ans = ans % d
    last_n = 2 if n == m else m
    ans = ans * last_n % d
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
from math import factorial as f
print(max(0,2-abs(n-m))*f(n)*f(m) % (10**9+7))

Submission status: Runtime Error

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/max(a))%1000000007)
    print(int(ans%1000000007 ))

Submission status: Runtime Error

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*ans*2
    else:
        ans=math.factorial(max(a))
        ans=ans*ans/max(a)
        #print(ans)

    print(int(ans%1000000007 ))

Submission status: Runtime Error

Solution code:
import math
a=list(map(int,input().split()))
ans=0
i=0
if max(a)-min(a)>=2:
    pass
else:
    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)
        #print(ans)

print(int(ans%1000000007 ))

Submission status: Runtime Error

Solution code:
def fct(n):
	v = 1
	for i in range(1, n+1):
		v = (v * i) % 1000000007
	return v
N, M = map(int, input().split())
ans = 0
if N == M:
	v = fct(N)
	ans = (v*v*2)%1000000007
elif abs(N-M) == 1:
	ans = (fct(N)*fct(M))%1000000007
print ans

Submission status: Runtime Error

Solution code:
m,d=map(int,input().split())
def fac(n):
  a=1
  if n==1:
    return a
  else:
    a=n*fac(n-1)
    return a%(10**9+7)
if abs(m-d)>=2:
  print(0)
elif abs(m-d)==1:
  print((fac(m)*fac(d))%(10**9+7))
else:
  print(2*(fac(m)**2)%(10**9+7))

Submission status: Runtime Error

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


def n_factorial_mod(n, mod):
    ret = 1
    while n != 1:
        ret = ret * n % mod
        n -= 1
    return ret


if abs(n - m) > 1:
    print(0)
elif abs(n - m) == 1:
    print(n_factorial_mod(n, mod) * n_factorial_mod(m, mod) % mod)
else:
    print(2 * n_factorial_mod(n, mod) * n_factorial_mod(m, mod) % mo
d)

Submission status: Runtime Error

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: Runtime Error

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*i)%(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: Runtime Error

Solution code:
N, M = int(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: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
def f(n):
  if n==1:
    return 1
  else:
    return f(n-1)*n

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

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit
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: Runtime Error

Solution code:
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: Runtime Error

Solution code:
mo=10**9+7
def fact(x):
  if x==1:
    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: Runtime Error

Solution code:
mo=10**9+7
def fact(x):
  if x==1:
    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((fact(m)*fact(n)*2)%mo)
else:
  print(fact(m)*fact(n)%mo)

Submission status: Runtime Error

Solution code:
mo=10**9+7
def fact(x):
  if x==1:
    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((fact(m)*fact(n)*2)%mo)
else:
  print(fact(m)*fact(n))

Submission status: Runtime Error

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: Runtime Error

Solution code:
import sys
import collections
sys.setrecursionlimit(100000)
ns = lambda: sys.stdin.readline().rstrip()
ni = lambda: int(ns())
nm = lambda: map(int, sys.stdin.readline().split())
nl = lambda: list(nm())
nsl = lambda: map(str, sys.stdin.readline().split())

n, m = nm()
memo = [-1 for i in range(max(n, m) + 1)]
mod = 10 ** 9 + 7

def fact(k):
    if memo[k] != -1:
        return memo[k]
    elif k == 1:
        memo[1] = 1
        return memo[1]
    else:
        memo[k] = k * fact(k - 1)
        return memo[k]

if abs(n - m) >= 2:
    print(0)
else:
    nn = fact(n) % mod
    mm = fact(m) % mod
    if n == m:
        print(2 * nn * mm % mod)
    else:
        print(nn * mm % mod)

Submission status: Runtime Error

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