#!/bin/python3 import math import sys from time import time mod = 10**9 + 7 def divfactorial(a,b): return factorialList[a]*invFactList[b]%mod def bininsert(nums, newnum): toret = [] if newnum < nums[0]: toret.append(newnum) toret.extend(nums) return toret if newnum > nums[len(nums)-1]: toret.extend(nums) toret.append(newnum) return toret L = 0 R = len(nums)-1 while R - L > 1: m = int(math.floor((R+L)/2)) if newnum > nums[m]: L = m else: R = m toret.extend(nums[:R]) toret.append(newnum) toret.extend(nums[R:]) return toret start = time() n = int(input().strip()) factorialList = [1] for i in range(1,n+1): factorialList.append(factorialList[i-1]*i%mod) invFactList = [] for i in range(n+1): invFactList.append(pow(factorialList[i],mod-2,mod)) m = list(map(int, input().strip().split(' '))) #m.sort() #print(*m) dp = [[0 for length in range(n+1)] for currArrays in range(n+1)] for currArrays in range(n+1): dp[0][currArrays] = 1 dp[0][0] = 1 for length in range(1,n+1): dp[length][1] = 1 #print(str(time()- start) ) #scan the beginning? firsts =[] firsts.append(m[0]) index = 1 while(index < n and firsts == m[:index]): firsts = bininsert(firsts,m[index]) index+=1 permUpper = index for length in range(1, n+1): upper = permUpper+1 if length < n: upper = min(length+1, permUpper + 1, n - length + 1) subs = [] subs.append(m[n-length]) for currArrays in range(2, upper): #print(subs) subs.append(m[n-length+currArrays-1]) #subs = m[n-length:n-length+currArrays] #subs.sort() if subs[currArrays-1] < subs[currArrays-2]: #dp[length][currArrays] = 0 break else: prod = 0 for newArrays in range(min(currArrays+1,length-currArrays+1)): if dp[length-currArrays][newArrays] > 0: prod += divfactorial(currArrays,currArrays-newArrays)*dp[length-currArrays][newArrays] prod = prod%mod dp[length][currArrays] = prod%mod #print(str(time()- start) ) #print(dp) tot = 0 for i in range(n+1): tot += dp[n][i] print(int((tot)%mod)) #print(str(time()-start)) # your code goes here