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 = 1000000007

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

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

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

if abs(N - M) > 1:
    print(0)
else:
    ans = fact[N] * fact[M] % MOD
    if N == M:
        ans *= 2
        ans %= MOD
    print(ans)

Submission status: Accepted

Solution code:
mod = 10 ** 9 + 7
fact = [i for i in range(10**5+1)]
fact[0] = 1
for i in range(10 ** 5):
  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: Accepted

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

Submission status: Accepted

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

if abs(n-m) > 1:
    ans = 0
else:
    if abs(n-m) == 0:
        a = math.factorial(n)
        ans = a*a*2
    if abs(n-m) == 1:
        a = math.factorial(min(n, m))
        b = a * max(n, m)
        ans = a * b

print(ans%mod)

Submission status: Accepted

Solution code:
import math

def permutations(a,b):
    return math.factorial(a)//math.factorial(a-b)

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

Submission status: Accepted

Solution code:
import math

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

o = math.factorial(n)
p = math.factorial(m)

if abs(n-m) > 1:
  print(0)
elif abs(n-m) == 1:  
  print((o*p)%(int(1e9)+7))
elif n-m == 0:  
  print((2*o*p)%(int(1e9)+7))

Submission status: Accepted

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

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

if n-m  > 1 or m-n >1:
     ans=0
elif n-m == 0:
     ans = 2*fact[n]*fact[m]%mod
else:
     ans = fact[n] * fact[m] % mod
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from math import factorial
# int(input()) # 入力が1つ
n, m = map(int, input().split()) # 入力が複数
# [int(i) for i in input().split()] # 配列で数字
mod = 10 ** 9 + 7

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

Submission status: Accepted

Solution code:
import sys

import numpy as np
MOD = 10 ** 9 + 7
U = 10 ** 6
def mod_cumprod(a, p=MOD):
  l = len(a); sql = int(np.sqrt(l) + 1)
  a = np.resize(a, sql**2).reshape(sql, sql)
  for i in range(sql - 1): a[:, i+1] *= a[:, i]; a[:, i+1] %= p
  for i in range(sql - 1): a[i+1] *= a[i, -1]; a[i+1] %= p
  return np.ravel(a)[:l]

def make_fac_ifac(n=U, p=MOD):
  fac = np.arange(n + 1); fac[0] = 1
  fac = mod_cumprod(fac)
  ifac = np.arange(n + 1, 0, -1); ifac[0] = pow(int(fac[-1]), p-2, p)
  ifac = mod_cumprod(ifac)[n::-1]
  return fac, ifac

fac, ifac = make_fac_ifac()

def mod_choose(n, r, p=MOD):
  if r > n or r < 0: return 0
  return fac[n] * ifac[r] % p * ifac[n-r] % p

def make_choose_n_table(n=10 ** 9, r=U, p=MOD):
  table = [None] * (r + 1)
  table = np.arange(n + 1, n - r, -1); table[0] = 1
  table[1:] = mod_cumprod(table[1:]) * ifac[1:r+1] % p
  return table

mod_choose_n = make_choose_n_table()

n, m = map(int, sys.stdin.readline().split())

def main():
  d = abs(n - m)
  if d >= 2: res = 0
  elif d == 1: res = fac[n] * fac[m] % MOD
  else: res = fac[n] * fac[m] % MOD * 2 % MOD
  print(res)
  
if __name__ == '__main__':
  main()

Submission status: Accepted

Solution code:
import sys

MOD = 10 ** 9 + 7
U = 10 ** 6
def make_fac_ifac(n=U, p=MOD):
  fac = [None] * (n + 1)
  fac[0] = 1
  for i in range(n): fac[i+1] = fac[i] * (i + 1) % p
  ifac = [None] * (n + 1)
  ifac[n] = pow(fac[n], p - 2, p)
  for i in range(n, 0, -1): ifac[i-1] = ifac[i] * i % p
  return fac, ifac

fac, ifac = make_fac_ifac()

def mod_choose(n, r, p=MOD):
  if r > n or r < 0: return 0
  return fac[n] * ifac[r] % p * ifac[n-r] % p

def make_choose_n_table(n=10 ** 9, r=U, p=MOD):
  table = [None] * (r + 1)
  table[0] = 1
  j = 1
  for i in range(n, n - r, -1): 
    table[j] = table[j-1] * i % p
    j += 1
  for i in range(1, r + 1): table[i] = table[i] * ifac[i] % p
  return table
    
mod_choose_n = make_choose_n_table()

n, m = map(int, sys.stdin.readline().split())

def main():
  d = abs(n - m)
  if d >= 2: res = 0
  elif d == 1: res = fac[n] * fac[m] % MOD
  else: res = fac[n] * fac[m] % MOD * 2 % MOD
  print(res)
  
if __name__ == '__main__':
  main()

Submission status: Accepted

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

def f(x,mod):
    k = 1
    res = 1
    while k<x:
        k+=1
        res*=k
        res %= mod
    return res

if abs(N-M)>1:print(0)
elif abs(N-M)==1:
    a = f(min(N,M),mod)
    print(pow(a,2,mod)*max(N,M)%mod)
else:
    a = f(N,mod)
    print(2*pow(a,2,mod)%mod)

Submission status: Accepted

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

Submission status: Accepted

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

if abs(N-M) == 1:
  print((math.factorial(N)*math.factorial(M)) % p)
elif N-M == 0:
  print((math.factorial(N)*math.factorial(M)*2) % p)
else:
  print(0)

Submission status: Accepted

Solution code:
from math import factorial

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


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

Submission status: Accepted

Solution code:
def fac(a):
    result = 1
    for i in range(1, a+1):
        result = result * i % (10**9+7)
    return result

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#
# Written by NoKnowledgeGG @YlePhan
# ('ω')
#
#import math
#mod = 10**9+7
#import itertools
#import fractions
#import numpy as np
#mod = 10**4 + 7
"""def kiri(n,m):
  r_ = n / m
  if (r_ - (n // m)) > 0:
    return (n//m) + 1
  else:
    return (n//m)"""

""" n! mod m 階乗
mod = 1e9 + 7
N = 10000000
fac = [0] * N
def ini():
  fac[0] = 1 % mod
  for i in range(1,N):
    fac[i] = fac[i-1] * i % mod"""

"""mod = 1e9+7
N = 10000000
pw = [0] * N
def ini(c):
  pw[0] = 1 % mod
  for i in range(1,N):
    pw[i] = pw[i-1] * c % mod"""

"""
def YEILD():
  yield 'one'
  yield 'two'
  yield 'three'
generator = YEILD()
print(next(generator))
print(next(generator))
print(next(generator))
"""
"""def gcd_(a,b):
  if b == 0:#結局はc,0の最大公約数はcなのに
    return a
  return gcd_(a,a % b) # a = p * b + q"""
"""def extgcd(a,b,x,y):
  d = a
  if b!=0:
    d = extgcd(b,a%b,y,x)
    y -= (a//b) * x
    print(x,y)
  else:
    x = 1
    y = 0
  return d"""


def readInts():
  return list(map(int,input().split()))
mod = 10**9 + 7
def main():
  from math import factorial as f
  n,m = sorted(readInts())
  
  sa = m - n
  if sa >= 2:
    ans = 0
  else:
    ans = f(m) * f(n)
    if sa == 0:
      ans *= 2
  print(ans % mod)
if __name__ == '__main__':
  main()

Submission status: Accepted

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

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

Submission status: Accepted

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

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

if n - m > 1 or m - n > 1:
    ans = 0
elif n - m == 0:
    ans = 2 * fact[n] * fact[m] % mod
else:
    ans = fact[n] * fact[m] % mod
    
print(ans)

Submission status: Accepted

Solution code:
import sys
import heapq
import re
from itertools import permutations
from bisect import bisect_left, bisect_right
from collections import Counter, deque
from fractions import gcd
from math import factorial, sqrt
from functools import lru_cache, reduce
INF = 1 << 60
mod = 1000000007
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline

# UnionFind
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

# ダイクストラ
def dijkstra_heap(s, edge, n):
    #始点sから各頂点への最短距離
    d = [10**20] * n
    used = [True] * n #True:未確定
    d[s] = 0
    used[s] = False
    edgelist = []
    for a,b in edge[s]:
        heapq.heappush(edgelist,a*(10**6)+b)
    while len(edgelist):
        minedge = heapq.heappop(edgelist)
        #まだ使われてない頂点の中から最小の距離のものを探す
        if not used[minedge%(10**6)]:
            continue
        v = minedge%(10**6)
        d[v] = minedge//(10**6)
        used[v] = False
        for e in edge[v]:
            if used[e[1]]:
                heapq.heappush(edgelist,(e[0]+d[v])*(10**6)+e[1])
    return d

# 素因数分解
def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])

    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])

    return arr

# 2数の最小公倍数
def lcm(x, y):
    return (x * y) // gcd(x, y)

# リストの要素の最小公倍数
def lcm_list(numbers):
    return reduce(lcm, numbers, 1)

# リストの要素の最大公約数
def gcd_list(numbers):
    return reduce(gcd, numbers)

# ここから書き始める
def my_factorial(n):
    ret = 1
    i = 1
    while i <= n:
        ret *= i
        i += 1
        ret %= mod
    return ret


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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def resolve():
    '''
    code here

    '''
    import math

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

    deff = N - M

    if deff == -1:
        N, M = M, N

    if deff == 0:
        res = (math.factorial(N)**2 * 2) % (10**9+7)

    elif abs(deff) == 1:
        res = (math.factorial(N) **2 // N ) % (10**9+7)

    else:
        res = 0
    
    print(res)

if __name__ == "__main__":
    resolve()

Submission status: Accepted

Solution code:
def resolve():
    '''
    code here

    '''
    import math

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

    deff = N - M

    if deff == -1:
        N, M = M, N

    if deff == 0:
        temp = math.factorial(N)
        res = (temp**2 * 2) % (10**9+7)

    elif abs(deff) == 1:
        res = (math.factorial(N) **2 // N ) % (10**9+7)

    else:
        res = 0
    
    print(res)

if __name__ == "__main__":
    resolve()

Submission status: Accepted

Solution code:
def resolve():
    '''
    code here

    '''
    import math

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

    deff = N - M

    if deff == 0:
        temp = math.factorial(N)
        res = (temp**2 * 2) % (10**9+7)

    elif abs(deff) == 1:
        res = (math.factorial(N) * math.factorial(M)) % (10**9+7)

    else:
        res = 0
    
    print(res)

if __name__ == "__main__":
    resolve()

Submission status: Accepted

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

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

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

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
S = input().split(" ")
N = int(S[0])
M = int(S[1])


def C(m, n):
    t = m
    s1 = 1
    while t > (m - n):
        s1 = s1 * t
        t = t - 1

    s2 = 1

    t = n
    while t >= 1:
        s2 = s2 * t
        t = t - 1

    return int(s1 / s2)


def A(m, n):
    s = 1
    t = m

    while t > m - n:
        s = (s * t) % 1000000007
        t = t - 1

    return s


def calculate(maxValue, minValue):
    if maxValue > minValue + 1:
        return 0

    if maxValue == minValue:
        S = A(maxValue,maxValue)
        return  2 * S * S

    return A(maxValue,maxValue) * A(minValue,minValue)

result = calculate(max(N,M),min(N,M))

print(result % 1000000007)

Submission status: Accepted

Solution code:
MOD = 10 ** 9 + 7


def prepare(n):
    global MOD
    modFacts = [0] * (n + 1)
    modFacts[0] = 1
    for i in range(n):
        modFacts[i + 1] = (modFacts[i] * (i + 1)) % MOD

    invs = [1] * (n + 1)
    invs[n] = pow(modFacts[n], MOD - 2, MOD)
    for i in range(n, 1, -1):
        invs[i - 1] = (invs[i] * i) % MOD

    return modFacts, invs


N, M = map(int, input().split())
modFacts, invs = prepare(N + M)

A, B = (N, M) if N >= M else (M, N)

if A - 1 > B:
    cnt = 0
else:
    varA = modFacts[A] % MOD
    varB = modFacts[B] % MOD
    if A == B:
        varB *= 2
    cnt = (varA * varB) % MOD
print(cnt)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
def mod_factorial(a, mod):
    ans = 1
    while a != 1:
        ans *= a
        ans = ans % mod
        a -= 1
    return ans

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)

Submission status: Accepted

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

if tmp >= 2:
    ans = 0
elif tmp == 1:
    ans = math.factorial(n) * math.factorial(m)
else:
    ans = 2 * math.factorial(n) * math.factorial(m)

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

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
N = 10 ** 5  # N は必要分だけ用意する
fact = [1, 1]  # fact[n] = (n! mod p)
for i in range(2, N + 1):
    fact.append((fact[-1] * i) % (10**9+7))
result = 0
if abs(n-m) < 2:
    if n == m:
        result = 2*(fact[n]**2)
    else:
        result = fact[n]*fact[m]
print(result%(10**9+7))

Submission status: Accepted

Solution code:
import sys
from io import StringIO
import unittest

import math

def resolve():
    n, m = map(int, input().split())
    div = 10**9 + 7

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

class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)
    def test_入力例_1(self):
        input = """2 2"""
        output = """8"""
        self.assertIO(input, output)
    def test_入力例_2(self):
        input = """3 2"""
        output = """12"""
        self.assertIO(input, output)
    def test_入力例_3(self):
        input = """1 8"""
        output = """0"""
        self.assertIO(input, output)
    def test_入力例_4(self):
        input = """100000 100000"""
        output = """530123477"""
        self.assertIO(input, output)

if __name__ == "__main__":
    # unittest.main()
    resolve()

Submission status: Accepted

Solution code:
# ==================== mod 10**9+7    
mod=10**9+7

def mul(a, b):
    return ((a % mod) * (b % mod)) % mod


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

if n==1 and m==1:
    print(2)
elif n==2 and m==1:
    print(2)
elif n==1 and m==2:
    print(2)
elif n==2 and m==2:
    print(8)
elif n+2<=m:
    print(0)
elif n+1==m:
    k=1
    for i in range(1,n):
        k=mul(k,i+1)
    for i in range(1,m):
        k=mul(k,i+1)
    print(k)
elif n==m:
    k=1
    for i in range(1,n):
        k=mul(k,i+1)
    for i in range(1,m):
        k=mul(k,i+1)
    k=mul(k,2)
    print(k)
elif n==m+1:
    k=1
    for i in range(1,n):
        k=mul(k,i+1)
    for i in range(1,m):
        k=mul(k,i+1)
    print(k)
elif n>=m+2:
    print(0)

Submission status: Accepted

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

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

if N == M:
    print((2* factorial(N) ** 2) % MOD)
elif N == M + 1:
    print((factorial(M)**2*N) % MOD)
elif M == N + 1:
    print((factorial(N)**2*M) % MOD)
else:
    print(0)

Submission status: Accepted

Solution code:
def test():
	mod = 10**9 + 7
	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%mod) * (i_n%mod)
		mp = (mp%mod) * (i_m%mod)
		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)
test()

Submission status: Accepted

Solution code:
import math

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

Submission status: Accepted

Solution code:
large_p = 10**9 + 7

def main():
    n, m = map(int, input().split())
    if (abs(n-m)) >= 2:
        print(0)
    else:
        
        np = 1
        for i in range(n,0,-1):
            np = (np * i) % large_p
        mp = 1
        for i in range(m,0,-1):
            mp = (mp * i) % large_p

        if abs(n-m) == 1:
            r = (np * mp) % large_p
            print(r)
        else:
            r = ((np * mp) * 2) % large_p
            print(r)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from math import factorial
large_p = 10**9 + 7

def main():
    n, m = map(int, input().split())
    if (abs(n-m)) >= 2:
        print(0)
    else:
        np = factorial(n)
        mp = factorial(m)

        if abs(n-m) == 1:
            r = (np * mp) % large_p
            print(r)
        else:
            r = ((np * mp) * 2) % large_p
            print(r)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def resolve():
    T,M=map(int,input().split())

    MOD = 10 ** 9 + 7
    N = 10 ** 5

    fact = [1] * (N + 1)
    rfact = [1] * (N + 1)
    r = 1
    for i in range(1, N + 1):
        fact[i] = r = r * i % MOD
    rfact[N] = r = pow(fact[N], MOD - 2, MOD)
    for i in range(N, 0, -1):
        rfact[i - 1] = r = r * i % MOD

    # nPk (mod MOD) を求める
    def perm(n, k):
        return fact[n] * rfact[n - k] % MOD

    if T==M:
        ans=perm(T,T)*perm(M,M)*2%MOD
    elif abs(T-M)==1:
        ans=perm(T,T)*perm(M,M)%MOD
    else:
        ans=0
    print(ans)





resolve()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-


def factorial_mod(n, mod):
    ret = 1
    for i in range(1, n + 1):
        ret *= i
        ret %= mod
    return ret


def main():
    N, M = map(int, input().split())

    mod = 10 ** 9 + 7
    factorial = factorial_mod(N, mod) * factorial_mod(M, mod)

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


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

stdin = sys.stdin
inf = 1 << 60
mod = 1000000007

ni      = lambda: int(ns())
nin     = lambda y: [ni() for _ in range(y)]
na      = lambda: list(map(int, stdin.readline().split()))
nan     = lambda y: [na() for _ in range(y)]
nf      = lambda: float(ns())
nfn     = lambda y: [nf() for _ in range(y)]
nfa     = lambda: list(map(float, stdin.readline().split()))
nfan    = lambda y: [nfa() for _ in range(y)]
ns      = lambda: stdin.readline().rstrip()
nsn     = lambda y: [ns() for _ in range(y)]
ncl     = lambda y: [list(ns()) for _ in range(y)]
nas     = lambda: stdin.readline().split()

def modfact(n, m):
    res = 1
    for i in range(2, n + 1):
        res = res * i % m
    return res

n, m = na()
a = abs(n - m)
if a > 1:
    print(0)
elif a == 1:
    print(modfact(n, mod) * modfact(m, mod) % mod)
else:
    print(modfact(n, mod) * modfact(m, mod) * 2 % mod)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, M = list(map(int, input().split()))
s = 10**9+7
a = 1

if abs(N-M)>=2:
    print(0)
else:
    if abs(N-M):
        for i in range(1, min(N,M)+1):
            a = (a * i) % s
        b = (a * max(N,M)) % s
        print((a * b) % s)
    else:
        for i in range(1, N+1):
            a = (a * i) % s
        c = (a * a) % s
        print((c * 2) % s)

Submission status: Accepted

Solution code:
import math

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

if N == M:
    ans = math.factorial(N)*math.factorial(M)*2

elif abs(N-M) == 1:
    ans = math.factorial(N)*math.factorial(M)

else:
    ans = 0

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

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**6)
from math import floor,ceil,sqrt,factorial,log
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
from operator import itemgetter
from fractions import gcd
mod = 10 ** 9 + 7
inf = float('inf')
ninf = -float('inf')
 
#整数input
def ii(): return int(sys.stdin.readline().rstrip()) #int(input())
def mii(): return map(int,sys.stdin.readline().rstrip().split())
def limii(): return list(mii()) #list(map(int,input().split()))
def lin(n:int): return [ii() for _ in range(n)]
def llint(n: int): return [limii() for _ in range(n)]
#文字列input
def ss(): return sys.stdin.readline().rstrip() #input()
def mss(): return sys.stdin.readline().rstrip().split()
def limss(): return list(mss()) #list(input().split())
def lst(n:int): return [ss() for _ in range(n)]
def llstr(n: int): return [limss() for _ in range(n)]

#本当に貪欲法か？ DP法では？？
#本当に貪欲法か？ DP法では？？
#本当に貪欲法か？ DP法では？？

def modFact(n, p): 
    if n >= p: 
        return 0    
  
    result = 1
    for i in range(1, n + 1): 
        result = (result * i) % p  
  
    return result 

def mul(a, b):
    return ((a % mod) * (b % mod)) % mod

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

n,m=max(n,m),min(n,m)

if n!=m:
    p = modFact(n,mod)
    q = modFact(m,mod)
    print(mul(p,q))

else:
    p = modFact(n,mod)
    q = modFact(m,mod)
    w = mul(p,q)
    print((2*w)%mod)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
mod = 10 ** 9 + 7
def solve():
    N, M = map(int, input().split())
    if abs(N-M) >= 2:
        print("0")
    elif N == M:
        print(factorial(N) % mod * factorial(M) % mod * 2 % mod)
    else:
        print(factorial(N) % mod * factorial(M) % mod)


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

if __name__ == '__main__':
    solve()

Submission status: Accepted

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

elif abs(n-m)==1:

    print((a*b)%(10**9+7))
else:
    print((a*b*2)%(10**9+7))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
MOD=int(1e9)+7
if abs(N-M)>1:
    print(0)
else:
    fn=1;fm=1;
    for i in range(2,N+1):
        fn=(fn*i)%MOD
    for i in range(2,M+1):
        fm=(fm*i)%MOD
    if abs(N-M)==0:
        print((fn*fm*2)%MOD)
    else:
        print((fn*fm)%MOD)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from math import factorial    #階乗計算
#print(math.factorial(5))   ->   120

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

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

n = factorial(N)%mod
m = factorial(M)%mod
ans = n*m%mod

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

Submission status: Accepted

Solution code:
M=10**9+7
n,m=map(int,input().split())
if n<m: m,n=n,m
if n-m>1: print(0); exit()
a=1
for i in range(m): a=a*-~i%M
print(a*a*[2,n][n-m]%M)

Submission status: Accepted

Solution code:
M=10**9+7
n,m=map(int,input().split())
if n<m: m,n=n,m
if n-m>1: print(0); exit()
a=1
for i in range(m):
  a=a*-~i%M
print(a*a*[2,n][n-m]%M)

Submission status: Accepted

Solution code:
#coding: utf-8
import math
import heapq
import bisect
import numpy as np
from collections import Counter, deque
#from scipy.misc import comb

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

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

print(ans)