#include #include #include #include #include #include #include #define MODE_VALUE 1000000007 int main(){ int segment[1200]; //int array[1201][1201][1201]; long long int step[1200]; int n; scanf("%d",&n); int *m = malloc(sizeof(int) * n); for(int m_i = 0; m_i < n; m_i++){ scanf("%d",&m[m_i]); } int max = m[0]; int max_component = 0; int now_component = 1; int idx_segment = 0; int num_segment; for(int m_i = 1; m_i < n; m_i++){ if (m[m_i] < m[m_i-1]){ segment[idx_segment] = now_component; idx_segment++; now_component = 1; }else { now_component++; } } segment[idx_segment] = now_component; idx_segment++; num_segment = idx_segment; max_component = segment[0]; /* for (int idx_segment = 0; idx_segment < num_segment; idx_segment++){ if (max_component < segment[idx_segment]){ max_component = segment[idx_segment]; } } */ int **p_array; p_array = malloc((max_component+1)*sizeof(int *)); for (int i = 1; i <= max_component;i++){ p_array[i] = malloc((i+1)*sizeof(int)); for (int j = 1; j <= i;j++){ if (j == 1){ p_array[i][j] = i; }else { p_array[i][j] = (i-j+1)*p_array[i][j-1]; } } } step[0] = 1; for (int i = 1; i <= max_component; i++){ step[i] = step[i-1]*i; if (step[i] >= MODE_VALUE){ step[i] = (step[i] % MODE_VALUE); } } //printf("%d\n",num); if ( num_segment == n || n == 1){ printf("1\n"); return 0; } //for (int i = 1; i <= max_component; i++){ // printf("%lld\n", step[i]); //} //printf("%d\n",max_component); int ***array; array = malloc((max_component+1)*sizeof(int **)); for (int i = 1; i <= max_component;i++){ array[i] = malloc(i*sizeof(int *)); for (int j = 1; j <= i ;j++){ array[i][j] = calloc(j,4); } } long long int _temp = 0; array[1][1][1] = 1; for (int i = 2; i <= max_component; i++){ for (int j = 1; j <= i; j++){ for (int k = 1; k <= j; k++){ // printf("%d %d %d\t", i, j, k); int max_value = (j > (i-j)) ? (i-j) : j; if (i-j < k && k != i){ _temp = 0; }else if (j == k && i == j){ _temp = 1; }else { _temp = 0; for (int p = k ; p <= max_value; p++){ _temp += p_array[j][p]*array[i-j][p][k]; // printf("'%d:%d %d %d'\t", p_array[j][p], i-j, p, k); } } array[i][j][k] = _temp; // printf("! %d\n",array[i][j][k]); //array[i][j][k] = 1; } } } int **segment_array; segment_array = malloc(num_segment*sizeof(int *)); for (int i = 0 ; i < num_segment; i++){ segment_array[i] = calloc((max_component+1),sizeof(int)); } int i = num_segment - 1; for (int j = 1; j <= max_component;j++){ for (int k = 1; k <= j; k++){ segment_array[i][j] += array[segment[i]][j][k]; } } for (int i = num_segment-2; i >= 0; i--){ for (int j = 1; j <= max_component;j++){ for (int k = 1; k <= j; k++){ for (int q = 1; q <= k; q++){ segment_array[i][j] += array[segment[i]][j][k]*p_array[k][q]*segment_array[i-1][q]; } } } } //printf("%d\n",array[4][4][4]); int sum = 0; for (int i = 1; i <= max_component;i++){ sum += segment_array[0][i]; } printf("%d\n",sum); // your code goes here return 0; }