#include #include #include #include int *M; int n; int i; #define MAX(a,b) \ ({ __typeof__ (a) _a = (a); \ __typeof__ (b) _b = (b); \ _a > _b ? _a : _b; }) #define MIN(a,b) \ ({ __typeof__ (a) _a = (a); \ __typeof__ (b) _b = (b); \ _a < _b ? _a : _b; }) bool issorted(int b /* begin */) { if (i == 1) return true; int lim = MIN(b + i - 1, n); for (int j = b; j < lim; ++j) { if (!(M[j] < M[j+1])) return false; } return true; } unsigned long long int factorial [] = { 1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800, 39916800, 479001600, 6227020800, 87178291200, 1307674368000, 20922789888000, 355687428096000, 6402373705728000, 121645100408832000, 2432902008176640000, // 51090942171709440000, // 1124000727777607680000, // 25852016738884976640000, // 620448401733239439360000, // 15511210043330985984000000 }; // look at if subsequences of i elements are sorted // if they are all sorted, then return non-zero value unsigned long count() { // I think elements are guaranteed to be different // but I am not sure int b; for (b = i; b < n; b += i) { if (!issorted(b)) return 0; } if (!issorted(b-i)) return 0; int k = (int)(n/i); unsigned long sum = pow(factorial[i], k) / i * factorial[i] / factorial[i - (n % i)]; return sum; } int main() { unsigned long modulo = pow(10, 9) + 7; scanf("%d", &n); M = malloc(sizeof(int)*n); for (int i = 0; i < n; ++i) { int m_i; scanf("%d", &m_i); M[i] = m_i; } unsigned long sum = 0; for (i = 1; i <= MIN(n, 15); ++i) { if (!issorted(0)) // if the beginning isn't sorted, cannot be sorted ever after! break; sum += count(); sum %= modulo; } printf("%ld\n", sum); return 0; }