#!/bin/python import sys import operator def ncr(n, r): r = min(r, n-r) if r == 0: return 1 numer = reduce(operator.mul, xrange(n, n-r, -1)) denom = reduce(operator.mul, xrange(1, r+1)) return numer//denom def npr(n, r): return factorials[n]/factorials[n-r] def rec(main, k = None, is_first=False): first = False if k == None: if is_first == True: first = True k = main else: if k <= 0: return 1 summ = 0 for i in xrange(k, 0, -1): product = 1 if first == False: product *= npr(main, i) b = k b -= i product *= rec(i, b) summ += product return summ M = 1000000007 divisors = [[] for i in xrange(1201)] factorials = [[] for i in xrange(1201)] factorials[0] = 1 for number in xrange(1, 1201): factorials[number] = (factorials[number-1] * number) % M for i in xrange(1, number+1): if number % i == 0: divisors[number].append(i) n = int(raw_input().strip()) m = map(int, raw_input().strip().split(' ')) # your code goes here chunks = [] chunk = [] for num in m: if len(chunk) == 0 or num > chunk[-1]: chunk.append(num) else: chunks.append(chunk) chunk = [num] if len(chunk) > 0: chunks.append(chunk) total = 0 if len(chunks) == 1: print rec(len(chunks[0]), is_first=True) else: ks = divisors[len(chunks[0])] # list of all divisors for k in ks: problem = False ans = 1 for i in xrange(len(chunks)): l = len(chunks[i]) # Last group if i == len(chunks) - 1: permutations = rec(k, l) ans *= permutations elif l % k != 0: problem = True break else: if i != 0: for j in xrange(l/k): permutations = npr(k,k) #factorials[k] ans *= permutations if problem == False: total = (total + (ans % M)) % M print total