#include <stdio.h>
#include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <algorithm>

using ll = long long int;
const int INF = (1<<30);
const ll INFLL = (1ll<<60);
const ll MOD = 998244353ll;

#define l_ength size

void mul_mod(ll& a, ll b){
	a *= b;
	a %= MOD;
}

void add_mod(ll& a, ll b){
	a = (a<MOD)?a:(a-MOD);
	b = (b<MOD)?b:(b-MOD);
	a += b;
	a = (a<MOD)?a:(a-MOD);
}

ll dp[310][310][310][2];
std::string s;

int main(void){
	int n,m,i,j,k,l;
	ll ans=0ll;
	std::cin >> s;
	std::cin >> l;
	n = s.l_ength();
	l = std::min(l,n);
	std::reverse(s.begin(),s.end());
	dp[0][0][0][0] = 1ll;
	for(i=0; i<n; ++i){
		if(s[i]=='0'){
			for(j=0; j<=l; ++j){
				for(k=0; k<=j; ++k){
					add_mod(dp[i+1][j][k][0],dp[i][j][k][0]);
					if(k){
						add_mod(dp[i+1][j][k][0],dp[i+1][j][k-1][0]);
					}
				}
			}
			for(j=0; j<=l; ++j){
				for(k=0; k<=j; ++k){
					add_mod(dp[i+1][j][k][0],dp[i][j][k][1]);
				}
			}
		}else{
			for(j=0; j<=l; ++j){
				for(k=0; k<=j; ++k){
					add_mod(dp[i+1][j][k][0],dp[i][j][k][0]);
					if(j+1<=l){
						add_mod(dp[i+1][j+1][k][1],dp[i][j][k][0]);
						add_mod(dp[i+1][j+1][k][1],dp[i][j][k][1]);
					}
				}
			}
		}
		/*
		for(j=0; j<=l; ++j){
			for(k=0; k<=j; ++k){
				printf("%d %d %d: %lld, %lld,;\n",i,j,k,dp[i+1][j][k][0],dp[i+1][j][k][1]);
			}
		}
		printf("\n");
		*/
	}
	for(j=0; j<=l; ++j){
		for(k=0; k<=j; ++k){
			add_mod(ans,dp[n][j][k][0]);
		}
	}
	std::cout << ans << std::endl;
	return 0;
}

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using ld = long double;
using vl = vector<ll>;
using vvl = vector<vl>;
using vb = vector<bool>;
using P = pair<ll, ll>;
template <typename T> using pq = priority_queue<T>;
template <typename T> using minpq = priority_queue<T, vector<T>, greater<T>>;
template <typename T, typename K> using ump = unordered_map<T, K>;
const ll dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
const ll mod = 1000000007;
const ll inf = ll(1e9);
const ll e5 = ll(1e5);
const ll ll_inf = ll(1e9) * ll(1e9);

#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repr(i, n) for (ll i = ll(n - 1); i >= 0; i--)
#define each(i, mp) for (auto &i : mp)
#define eb emplace_back
#define F first
#define S second
#define all(obj) (obj).begin(), (obj).end()

template <class T> ostream &operator<<(ostream &out, const vector<T> &list) {
    ll n = list.size();
    rep(i, n) out << list[i] << ' ';
    return out;
}

template <class T> istream &operator>>(istream &in, vector<T> &list) {
    ll n = list.size();
    rep(i, n) in >> list[i];
    return in;
}

template <class T> ostream &operator<<(ostream &out, const vector<vector<T>> &list) {
    ll n = list.size();
    rep(i, n) out << list[i] << '\n';
    return out;
}

/* ------------- ANSWER ------------- */
/* ---------------------------------- */

void solve() {
    ll n;
    cin >> n;
    vl a(n);
    vl cnt(n);
    cin >> a;

    ll ans = 1;
    rep(i, n) {
        if (a[i] == 0) ans *= (3 - cnt[a[i]]);
        else ans *= (cnt[a[i] - 1] - cnt[a[i]]);
        ans %= mod;
        cnt[a[i]]++;
    }
    cout << ans << '\n';
}

int main() {
#ifdef MY_DEBUG
    while (true) {
#endif
        solve();
#ifdef MY_DEBUG
    }
#endif
    return 0;
}