#include #include #define MOD 998244353 int main() { int N; if (scanf("%d", &N) != 1) return 0; char *A = (char *)malloc(N + 1); scanf("%s", A); int *pos0 = (int *)malloc(N * sizeof(int)); int *pos1 = (int *)malloc(N * sizeof(int)); int count0 = 0, count1 = 0; for (int i = 0; i < N; i++) { if (A[i] == '0') pos0[count0++] = i; else pos1[count1++] = i; } // Optimized: Only store 2 rows at a time // dp[2][count1 + 1] int *dp[2]; dp[0] = (int *)calloc(count1 + 1, sizeof(int)); dp[1] = (int *)calloc(count1 + 1, sizeof(int)); dp[0][0] = 1; for (int i = 0; i <= count0; i++) { int curr = i % 2; int next = (i + 1) % 2; // Clear the 'next' row before we use it if (i < count0) { for (int j = 0; j <= count1; j++) dp[next][j] = 0; } for (int j = 0; j <= count1; j++) { if (dp[curr][j] == 0) continue; int current_idx = i + j; // Try placing '0' (moves to the NEXT row) if (i < count0 && pos0[i] <= current_idx) { dp[next][j] = (dp[next][j] + dp[curr][j]) % MOD; } // Try placing '1' (stays in the CURRENT row, next column) if (j < count1 && pos1[j] >= current_idx) { dp[curr][j + 1] = (dp[curr][j + 1] + dp[curr][j]) % MOD; } } } // The answer is in the row where i == count0 printf("%d\n", dp[count0 % 2][count1]); free(dp[0]); free(dp[1]); free(pos0); free(pos1); free(A); return 0; }