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:
N, M = map(int, input().split())

mod = 10 ** 9 + 7
ans = 1
if abs(N - M) >= 2:
    print(0)

else:
    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:
class ModWorld:
    def __init__(self, mod):
        self.mod = mod

    def prepare_factorial(self, N):
        self.f = [1] # f[n] := n!
        p = 1
        for i in range(1,N+1):
            p = (p*i) % self.mod
            self.f.append(p)

    def modpow(self, x, k):
        x %= self.mod
        k %= self.mod-1 # a^(p-1) = 1
        retval = 1
        while k:
            if k&1: retval *= x
            k>>=1
            x = x*x % self.mod
        return retval % self.mod

    def modcomb(self, n, k):
        return self.f[n] * self.modpow(self.f[n-k], -1) * self.modpow(self.f[k], -1) % self.mod

mw = ModWorld(mod=10**9+7)
mw.prepare_factorial(N=10**5)

N, M = list(map(int, input().split()))
d = 2 if N==M else 1 if abs(N-M)==1 else 0
print(d * mw.f[N] * mw.f[M] % mw.mod)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys, math, itertools, heapq, collections, bisect, string
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
inf = float('inf')
mod = 10**9+7
ans = 0 ;count = 0 ;pro = 1

n,m=map(int,input().split())
def factorial(n):
    result=1
    for ni in range(n,0,-1):
        result=result*ni%mod
    return result
    
if abs(n-m)>=2:
    print(0)
    exit()
if abs(n-m)==1:
    print(factorial(n)*factorial(m)%mod)
else:
    print(factorial(n)*factorial(m)*2%mod)

Submission status: Accepted

Solution code:
def c_reconciled(MOD=10**9 + 7):
    from math import factorial
    N, M = [int(i) for i in input().split()]

    if N == M:
        return (factorial(N) * factorial(M) * 2) % MOD
    elif abs(N - M) == 1:
        return (factorial(N) * factorial(M)) % MOD
    return 0

print(c_reconciled())

Submission status: Accepted

Solution code:
import math

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

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

print(ans)

Submission status: Accepted

Solution code:
mod = 10**9+7
def kai(n):
  i = n
  r = 1
  while i > 1:
    r = (r*i) % mod
    i -= 1
  return r
m,n = map(int,input().split())
m,n = max(m,n),min(m,n)
if m > n+1:
  print(0)
elif m == n+1:
  print((kai(n)**2*m)%mod)
else:
  print((2*kai(n)**2)%mod)

Submission status: Accepted

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

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

if n==m:
      print((math.factorial(n)*math.factorial(m)*2)%mod)
      exit()

print((math.factorial(n)*math.factorial(m))%mod)

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

def main():
    md = 10 ** 9 + 7
    n, m = MI()
    fac = [1, 1]
    for i in range(2, n + 5):
        fac.append(fac[-1] * i % md)
    if abs(n - m) > 1:
        print(0)
        exit()
    if n < m: n, m = m, n
    if n == m:
        ans = 2 * fac[n] ** 2 % md
    else:
        ans = fac[n] * fac[m] % md
    print(ans)

main()

Submission status: Accepted

Solution code:
import sys
import math

sys.setrecursionlimit(10**6)
if sys.platform in (['ios','darwin','win32']):
	sys.stdin=open('Untitled.txt')
input = sys.stdin.readline
def INT(): return int(input())
def MAP(): return [int(s) for s in input().split()]

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

N, M = MAP()

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

dog = permutations_count(N, N) % (10**9 + 7)
monkey = permutations_count(M, M) % (10**9 + 7)

ans = dog*monkey % (10**9 + 7) if N!=M else dog*monkey*2 % (10**9 + 7)

print(ans)

Submission status: Accepted

Solution code:
import sys
def f(a):
    b=1
    for i in range(a):
        b*=i+1
        b%=1000000007
    return b
a=sorted(list(map(int,input().split())))
if a[1]-a[0]>=2:
    print(0)
    sys.exit()
c=2
if a[1]>a[0]:
    c=1
print((f(a[1])*f(a[0])*c)%1000000007)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
p = 10**9 + 7
if abs(N-M) > 1:
  print(0)
  exit()

fct = [1] * (max(N, M) + 1)
for i in range(2, max(N, M)+1):
  fct[i] = fct[i-1] * i % p

print(fct[N] * fct[M] * ((N == M) + 1) % p)

Submission status: Accepted

Solution code:
import sys
MOD = 1e9 + 7
N, M = map(int, input().split())
ans = 1
if abs(N - M) > 1:
    print(0)
    sys.exit()
for n in range(2, N + 1):
    ans *= n
    ans %= MOD
for m in range(2, M + 1):
    ans *= m
    ans %= MOD
if N == M:
    ans *= 2
    ans %= MOD
print(int(ans))

Submission status: Accepted

Solution code:
from math import factorial

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

M = 10 ** 9 + 7

if abs(n - m) > 1:
    print(0)
else:
    ans = factorial(m) * factorial(n) % M
    if (m + n) % 2 == 0:
        ans = ans * 2 % M
    print(ans)

Submission status: Accepted

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

def is_even(n):
    if(n%2==0):
        return True
    else:
        return False
if(is_even(n) and is_even(m) or not is_even(n) and not is_even(m)):
    print(math.factorial(n)*math.factorial(m)*2%(10**9+7))
    exit()
else:
    print(math.factorial(n)*math.factorial(m)%(10**9+7))
    exit()
# if(is_even(n) and is_even(m)):
#     print(math.factorial(n)*math.factorial(m)*2)
#     exit()
# elif(is_even(n) and not is_even(m)):
#     print(math.factorial(n)*math.factorial(m))
#     exit()
# elif(not is_even(n) and is_even(m)):
#     print(math.factorial(m)*math.factorial(n))
#     exit()
# elif(not is_even(n) and not is_even(m)):
#     print(math.factorial(m)*math.factorial(n)*2)
#     exit()

Submission status: Accepted

Solution code:
import math

n, m = map(int, input().split())
a = math.factorial(n)
b = math.factorial(m)
if n == m:
    print(a * b * 2 % 1000000007)

elif abs(n - m) == 1:
    print(a * b % 1000000007)
else:
    print(0)

Submission status: Accepted

Solution code:
import math

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

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

n_cnt = 1
m_cnt = 1
if abs(n-m) == 1:
    for i in range(1, n+1):
        n_cnt *= i
        n_cnt %= MOD
    for i in range(1, m+1):
        m_cnt *= i
        m_cnt %= MOD
    print(n_cnt*m_cnt%MOD)
    exit()

n_cnt = 1
m_cnt = 1
if n == m:
    for i in range(1, n+1):
        n_cnt *= i
        n_cnt %= MOD
    for i in range(1, m+1):
        m_cnt *= i
        m_cnt %= MOD
    print(2*n_cnt*m_cnt%MOD)

Submission status: Accepted

Solution code:
N , M = map(int,input().split())
MOD = 10 ** 9 + 7
if abs(N - M) > 1:
    print("0")
    exit()
large = max(N , M)
small = min(N , M)
N_fuct = 1
M_fuct = 1
for i in range(2,large + 1):
    N_fuct *= i
    N_fuct %= MOD
for i in range (2,small + 1):
    M_fuct *= i
    M_fuct %= MOD
ans = N_fuct * M_fuct
if large == small:
    ans *= 2
    print(ans % MOD)
else:
    print(ans % MOD)

Submission status: Accepted

Solution code:
import math
MOD = 10**9+7
n,m = map(int,input().split())
#大きい順に並べる
if n < m:
  n,m = m,n

# mがn-1より小さいとどこかで犬・サル同士が隣り合う
if n-1 > m:
  print(0)
  exit()
  
if n == m:
  ans = (math.factorial(n)%MOD) * (math.factorial(m)%MOD)*2
  print(ans%MOD)
  
elif n-1 == m:
  ans = (math.factorial(n)%MOD) * (math.factorial(m)%MOD)
  print(ans%MOD)

Submission status: Accepted

Solution code:
import math
MOD = 10**9+7
n,m = map(int,input().split())
#大きい順に並べる
if n < m:
  n,m = m,n

# mがn-1より小さいとどこかで犬・サル同士が隣り合う
if n-1 > m:
  print(0)
  exit()
  
if n == m:
  ans = math.factorial(n)* math.factorial(m)*2
  print(ans%MOD)
  
elif n-1 == m:
  ans = math.factorial(n)* math.factorial(m)
  print(ans%MOD)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
n, m = [int(i) for i in sys.stdin.readline().split()]
MOD = 10**9+7

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

if n == m:
    print(2 * factorial(n) ** 2 % MOD)
elif abs(n - m) == 1:
    print(factorial(n) * factorial(m) % MOD)
else:
    print(0)

Submission status: Accepted

Solution code:
def main():
    nn,mm=map(int,input().split())
    n=max(nn,mm)
    m=min(nn,mm)
    temp=10**9 +7
    if n-m>=2:
        print(0)
        return
    fact=[0,1]
    for i in range(2,n+1):
        fact.append(fact[i-1]*i%temp)
    ans=fact[n]*fact[m]%temp
    if n-m==0:
        ans=ans*2%temp
    print(ans)

if __name__=='__main__':
    main()

Submission status: Accepted

Solution code:
# coding: utf-8
N, M = map(int, input().split())
if abs(N - M) > 1:
    print(0)
    exit()

mod = 10**9 + 7
ans = 1
for i in range(N):
    ans *= (N - i)
    ans = ans % mod
    
for i in range(M):
    ans *= (M - i)
    ans = ans % mod
print(ans * 2 % mod if (N+M)%2 == 0 else ans % mod)

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
mod = 10**9+7
def fact(x):
    a = 1
    for i in range(1,x+1):
        a = (a*i)%mod
    return a
if n >m+1 or m>n+1:
    print(0)
  

elif n == m+1:
    p = (fact(m)**2)%mod
    print(p*(m+1)%mod)

elif m == n+1:
    p = (fact(n)**2)%mod
    print(p*(n+1)%mod)
elif m == n:
    p = fact(n)**2%mod
    print(p*2%mod)

Submission status: Accepted

Solution code:
#
# 　　  ⋀_⋀　 
#　　  (･ω･)  
# .／ Ｕ ∽ Ｕ＼
#  │＊　合　＊│
#  │＊　格　＊│ 
#  │＊　祈　＊│ 
#  │＊　願　＊│ 
#  │＊　　　＊│ 
#      ￣
#
import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,ceil,sqrt,factorial,log #log2ないｙｐ
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
inf=float('inf')
mod = 10**9+7
def pprint(*A): 
    for a in A:     print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
class ModInt:
    def __init__(self, x):
        self.x = x % mod
    
    def __str__(self):
        return str(self.x)
    
    __repr__ = __str__

    def __add__(self, other):
        if isinstance(other, ModInt):
            return ModInt(self.x + other.x)
        else:
            return ModInt(self.x + other)

    __radd__ = __add__
        
    def __sub__(self, other):
        if isinstance(other, ModInt):
            return ModInt(self.x - other.x)
        else:
            return ModInt(self.x - other)

    def __rsub__(self, other):
        if isinstance(other, ModInt):
            return ModInt(other.x - self.x)
        else:
            return ModInt(other - self.x)

    def __mul__(self, other):
        if isinstance(other, ModInt):
            return ModInt(self.x * other.x)
        else:
            return ModInt(self.x * other)

    __rmul__ = __mul__

    def __truediv__(self, other):
        if isinstance(other, ModInt):
            return ModInt(self.x * pow(other.x, mod-2,mod))
        else:
            return ModInt(self.x * pow(other, mod - 2, mod))
            
    def __rtruediv(self, other):
        if isinstance(other, self):
            return ModInt(other * pow(self.x, mod - 2, mod))
        else:
            return ModInt(other.x * pow(self.x, mod - 2, mod))
            

    def __pow__(self, other):
        if isinstance(other, ModInt):
            return ModInt(pow(self.x, other.x, mod))
        else:
            return ModInt(pow(self.x, other, mod))
            

    def __rpow__(self, other):
        if isinstance(other, ModInt):
            return ModInt(pow(other.x, self.x, mod))
        else:
            return ModInt(pow(other, self.x, mod))
def main():
    N,M=MI()
    if abs(N-M)>1:
        print(0)
    else:
        ans = ModInt(1)
        for i in range(1,N+1):
            ans *= i
        for j in range(1,M+1):
            ans *= j
        if abs(N-M)==0:
            ans*=2
        print(ans)
if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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: Accepted

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

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

Submission status: Accepted

Solution code:
from math import factorial

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

ans = factorial(n) * factorial(m)
d = abs(n-m)  
if d == 0 :
  ans = 2*ans
elif d == 1:  
  pass
else:
  ans = 0

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from sys import stdin
from collections import deque
from math import factorial
mod = 10**9 + 7
a,b = [int(x) for x in stdin.readline().rstrip().split()]

if abs(a-b) >= 2:
    print(0)
    exit()

if a == b:
    print(2*(factorial(a)%mod)**2%mod)
else:
    mi = min(a,b)
    print(factorial(mi+1)%mod*(factorial(mi)%mod)%mod)

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

def fact(n):
  if n == 1:
    return 1
  return n*fact(n-1)%p

if abs(n-m) >1:
  print(0)
elif abs(n-m) == 1:
  print(fact(n)*fact(m)%p)
else:
  print(2*fact(n)*fact(m)%p)

Submission status: Accepted

Solution code:
import math

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

diff = abs(a - b)
if diff >= 2:
  print(0)
  exit(0)
  
n = (10 ** 9) + 7
f = math.factorial(a) * math.factorial(b)
if diff == 1:
  print(f % n)
  exit(0)
  
print((2 * f) % n)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
mod = 10**9 + 7
N, M = map(int,input().split())
if 1 < abs(N-M):
    print(0)
    exit()
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
if N == M:
    print((N_ans*M_ans*2)%mod)
else:
    print((N_ans*M_ans)%mod)

Submission status: Accepted

Solution code:
from math import factorial

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

Submission status: Accepted

Solution code:
N, M = sorted([int(a) for a in input().split()])
fa = 1
P = 10 ** 9 + 7
for i in range(1, N+1):
    fa = fa * i % P

if N == M:
    print(fa*fa*2%P)
elif M - N == 1:
    print(fa*fa*M%P)
else:
    print(0)

Submission status: Accepted

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: Accepted

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

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd

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()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N, M = MAP()

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

n,m = map(int,input().split())
large = n if n >= m else m
ans = math.factorial(large)
if abs(n-m)>1:
    ans = 0
elif n == m:
    ans *= (math.factorial(n)*2)
else:
    ans *= math.factorial(large-1)
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:
  ans = 0
elif N == M:
  ans = 2
  for i in range(1, N + 1):
    ans = (ans * i * i) % MOD
else:
  A = max(N, M)
  ans = 1
  for i in range(1, A):
    ans = (ans * i * i) % MOD
  ans = ans * A
print(int(ans % MOD))

Submission status: Accepted

Solution code:
import math

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

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

Submission status: Accepted

Solution code:
import math

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

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

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

Submission status: Accepted

Solution code:
def examC(mod):
    N, M = LI()
    fac = [1]*max(N,M)
    for i in range(1,max(N,M)):
        fac[i] = (i+1)*fac[i-1]%mod
    if abs(N-M)==1:
        ans = fac[N-1]*fac[M-1]%mod
    elif abs(N-M)==0:
        ans = fac[N-1]*fac[M-1]*2%mod
    else:
        ans = 0
    print(ans)

import sys
import copy
import bisect
import heapq
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
mod = 10**9 + 7
inf = float('inf')

examC(mod)

Submission status: Accepted

Solution code:
from sys import stdin,setrecursionlimit
import sys
from math import factorial
setrecursionlimit(10**6)
input=stdin.readline

a,b=map(int,input().split())
if abs(a-b)>1:
    print(0)
else:
    mod=10**9+7
    fact=[0]*(max(a,b)+1)
    fact[0],fact[1]=1,1
    for i in range(2,max(a,b)+1):
        fact[i]=(i*fact[i-1])%mod
    ans=(fact[a]*fact[b])%mod
    print(ans if abs(a-b)==1 else (ans*2)%mod)

Submission status: Accepted

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

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

if N > M:
    N, M = M, N
tmp = 1

MOD = 10 ** 9 + 7
for i in range(1, N + 1):
    tmp = (tmp * i) % MOD

if N == M:
    ans = ((tmp ** 2) * 2) % MOD
else:
    ans = (tmp * (tmp * M)) % MOD
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

if abs(n - m) > 1:
    res = 0
else:
    a = 1
    for x in range(1, n+1):
        a = (a * x) % mod
    b = 1
    for x in range(1, m+1):
        b = (b * x) % mod
    res = (a * b) % mod
    if n == m:
        res = (res * 2) % mod

print(res)

Submission status: Accepted

Solution code:
import math

animals = list(map(int, input().split(' ')))
dog = max(animals)
monkey = min(animals)
ans = 0



if dog - monkey == 0:
    ans = 2 * (math.factorial(dog) * math.factorial(monkey))
elif dog - monkey == 1:
    ans = math.factorial(dog) * math.factorial(monkey)
else:
    ans = 0
    
print(ans % (10 ** 9 + 7))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    MOD = 10**9+7
    N, M = (int(i) for i in input().split())
    if abs(N - M) > 1:
        print(0)
    elif N == M:
        from math import factorial
        print(factorial(N) * factorial(M) * 2 % MOD)
    else:
        from math import factorial
        print(factorial(N) * factorial(M) % MOD)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

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



if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from math import factorial

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

Submission status: Accepted

Solution code:
import math

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

Submission status: Accepted

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

N, M = [int(x) for x in input().split()]
if abs(N - M) >= 2: ans = 0
else: ans = (factorial(N) % mod * factorial(M) % mod) % mod
if N == M: ans *= 2
print(ans % mod)

Submission status: Accepted

Solution code:
import sys
from functools import reduce
import copy
import math
from pprint import pprint
import collections
import bisect


sys.setrecursionlimit(4100000)


def inputs(num_of_input):
    ins = [input() for i in range(num_of_input)]
    return ins


def int_inputs(num_of_input):
    ins = [int(input()) for i in range(num_of_input)]
    return ins


def solve(inputs):
    SSSS = 10 ** 9 + 7
    [A, B] = string_to_int(inputs[0])
    max_len = max([A, B])
    min_len = min([A, B])
    diff_len = abs(A-B)
    if diff_len >= 2:
        return 0

    pattern = 0
    for i in reversed(range(1, min_len + 1)):
        if i == min_len:
            if diff_len == 1:
                pattern = max_len * min_len
                pattern *= min_len
            else:
                pattern = min_len + min_len
                pattern *= min_len
        else:
            pattern *= i * i
        pattern = pattern % SSSS

    return pattern % SSSS


def string_to_int(string):
    return list(map(int, string.split()))


if __name__ == "__main__":
    ret = solve(inputs(1))
    print(ret)

Submission status: Accepted

Solution code:
# encoding:utf-8
import copy
import random
import bisect #bisect_left　これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math
 
mod = 10**9+7
 
N,M = map(int,input().split())
 
facterint = [1]
for i in range(1,max(N,M)+1):
    facterint.append(facterint[-1]*i%mod)
 
 
if abs(N-M) > 1:
    print(0)
    exit()
 
if N==M:
    ans = 2*facterint[N]*facterint[M]%mod
else:
    ans = facterint[N]*facterint[M]%mod
 
print(ans)

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
MOD=10**9+7
import math
Nb=math.factorial(N)%MOD
Mb=math.factorial(M)%MOD

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

Submission status: Accepted

Solution code:
import math
import sys


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

nn = max(N, M)
mm = min(N, M)

if mm != nn and mm + 1 < nn:
    print(0)
    sys.exit()

if N == M:
    pN = math.factorial(N) % C
    pM = math.factorial(M) % C
    p = pN * pM * (2 % C)
    print(p % C)

else:
    pN = math.factorial(N) % C
    pM = math.factorial(M) % C
    print((pN * pM) % C)

Submission status: Accepted

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

devided_number = 7 + 10**9

if abs(N-M)>=2:
    print(0)
elif abs(N-M)==1:
    #数が多い方が先頭になる
    print(math.factorial(N)*math.factorial(M)%devided_number)
else:
    #どっちでも先頭に持ってこれる
    print(math.factorial(N)*math.factorial(M)*2%devided_number)