#include #include #include using namespace std; #define cin fin ifstream fin("permutation.txt"); #define NELEMENT_MAX 1024 int nelement; int E[NELEMENT_MAX]; long long M[NELEMENT_MAX][NELEMENT_MAX+1]; int main() { while ( cin >> nelement, nelement ) { for ( int i = 0; i < nelement; i++ ) cin >> E[i]; if ( nelement == 1 ) { cout << 1 << endl; continue; } for ( int i = 0; i <= nelement; i++ ) M[nelement-1][i] = 0; M[nelement-1][0] = 1; for ( int curr = nelement-2; curr >= 0; curr-- ) { const int nrem = nelement - curr; const bool up = (E[curr] < E[curr+1]); for ( int i = 0; i <= nelement; i++ ) M[curr][i] = 0; if ( up ) for ( int ith = nrem-1; ith >= 0; ith-- ) M[curr][ith] = M[curr][ith+1] + M[curr+1][ith]; else for ( int ith = 0; ith < nrem; ith++ ) if ( ith > 0 ) M[curr][ith] = M[curr][ith-1] + M[curr+1][ith-1]; for ( int i = 0; i <= nelement; i++ ) assert( M[curr][i] >= 0 ); } long long sum = 0; for ( int i = 0; i < nelement; i++ ) sum += M[0][i]; cout << sum << endl; } return 0; }