Problem:
Score : 2000 points

Problem Statement

Let us consider a grid of squares with N rows and N columns.
Arbok has cut out some part of the grid so that, for each i = 1, 2, \ldots, N, the bottommost h_i squares are remaining in the i-th column from the left.
Now, he wants to place rooks into some of the remaining squares.

A rook is a chess piece that occupies one square and can move horizontally or vertically, through any number of unoccupied squares.
A rook can not move through squares that have been cut out by Arbok.

Let's say that a square is covered if it either contains a rook, or a rook can be moved to this square in one move.

Find the number of ways to place rooks into some of the remaining squares so that every remaining square is covered, modulo 998244353.

Constraints

1 \leq N \leq 400

1 \leq h_i \leq N

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
h_1 h_2 ... h_N

Output

Print the number of ways to place rooks into some of the remaining squares so that every remaining square is covered, modulo 998244353.

Sample Input 1

2
2 2

Sample Output 1

11

Any placement with at least two rooks is fine. There are 11 such placements.

Sample Input 2

3
2 1 2

Sample Output 2

17

The following 17 rook placements satisfy the conditions (R denotes a rook, * denotes an empty square):

R * * R * * R R R R R R
**R R** R*R R** *R* **R

R * R * * R * R * * R R
R*R *RR RR* R*R RRR RR*

R R R R R * * R R R
R*R *RR RRR RRR RRR

Sample Input 3

4
1 2 4 1

Sample Output 3

201

Sample Input 4

10
4 7 4 8 4 6 8 2 3 6

Sample Output 4

263244071

Solution in C++14 (GCC 5.4.1) (judge verdict: Accepted)

#include <bits/stdc++.h>
#define MOD 998244353
using namespace std;
inline int add(int a,int b)
{a+=b;return a>=MOD?a-MOD:a;}
inline int sub(int a,int b)
{a-=b;return a<0?a+MOD:a;}
inline int mul(int a,int b)
{return 1LL*a*b%MOD;}
inline void Add(int &a,int b)
{a+=b;a>=MOD?a-=MOD:1;}
const int Q=404;
int C[Q][Q],f[Q][Q][Q][2],h[Q],bok[Q][Q];
int iid[Q][Q],maxn=0,owo[Q][2],p2[Q];
void G(int l,int r,int lim)
{
    int id=iid[l][r];
    if(!id)id=iid[l][r]=++maxn;
    if(bok[id][lim])return;
    bok[id][lim]=1;
    for(int ii=l;ii<=r;ii++)
        if(h[ii]<lim){
            vector<pair<int,int>> rem;
            int now=r-l+1;
            for(int jj=l,k;jj<=r;jj=k)
                if(h[jj]>=lim){
                    for(k=jj+1;k<=r&&h[k]>=lim;k++);
                    G(jj,k-1,lim),rem.push_back(make_pair(jj,k-1));
                    now-=k-jj;
                }
                else k=jj+1;
            f[id][lim][now][0]=f[id][lim][0][1]=1;
            for(pair<int,int> o:rem){
                int nw=iid[o.first][o.second],len=o.second-o.first+1;
                for(int i=0;i<=now;i++)
                    for(int j=0;j<2;j++)
                        owo[i][j]=f[id][lim][i][j],f[id][lim][i][j]=0;
                for(int i=0;i<=now;i++)
                    for(int j=0;j<=len;j++)
                        for(int k=0;k<2;k++)
                            Add(f[id][lim][i+j][k],mul(owo[i][k],f[nw][lim][j][k]));
                now+=len;
            }
            return;
        }
    G(l,r,lim+1);
    int len=r-l+1;
    for(int i=0;i<=len;i++)
        for(int j=i;j<=len;j++) 
            Add(f[id][lim][j][0],mul(mul(p2[i]-(i==j),C[len-i][len-j]),f[id][lim+1][i][0])),
            Add(f[id][lim][j][1],mul(mul(p2[i]-(i==j),C[len-i][len-j]),f[id][lim+1][i][1]));
    for(int i=0;i<=len;i++)
        Add(f[id][lim][i][0],f[id][lim+1][i][1]),Add(f[id][lim][i][1],f[id][lim+1][i][1]);
}
int main()
{
    for(int i=0;i<Q;i++){
        C[i][0]=1;
        for(int j=1;j<=i;j++)
            C[i][j]=add(C[i-1][j-1],C[i-1][j]);
    }
    p2[0]=1;
    for(int i=1;i<Q;i++)
        p2[i]=add(p2[i-1],p2[i-1]);
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%d",&h[i]);
    G(1,n,1);
    printf("%d\n",f[iid[1][n]][1][n][0]);
    return 0;
}