1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
| #include <algorithm> #include <cassert> #include <cmath> #include <cstdio> #include <cstring> #include <deque> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <utility> #include <vector> #define rep(i, l, r) for (int i = (l); i <= (r); ++i) #define per(i, l, r) for (int i = (l); i >= (r); --i) using std::cerr; using std::cin; using std::cout; using std::endl; using std::make_pair; using std::pair; typedef pair<int, int> pii; typedef long long ll; typedef unsigned int ui;
const int p = 998244353;
const int N = 201000;
#define mod(x) (x) %= 998244353
inline int mul(int x, int y) { return ((ll)x * y) % p; } inline int add(int x, int y) { return ((ll)x + y) % p; }
int a[N], b[N], cnta, cntb;
int k, f[N][2];
int ksm(int a, int n) { int ret = 1; while (n) { if (n & 1) ret = mul(ret, a); a = mul(a, a); n >>= 1; } return ret; }
int solve(int a[], int n) { if (n == 0) return 1; int l = 0, r = n - 1; int ret; for (; a[l] == -1 && l < n; l++) ; if (l == n) return mul(k, ksm(k - 1, n - 1)); for (; a[r] == -1; --r) ; ret = mul(ksm(k - 1, l), ksm(k - 1, n - r - 1)); int lst = l; for (l = lst + 1; l <= r; ++l) { if (a[l] == -1) continue; ret = mul(ret, f[l - lst - 1][a[lst] == a[l]]); lst = l; } return ret; }
int main() { std::ios::sync_with_stdio(false); cout.tie(0); int n; cin >> n >> k;
f[0][0] = 1, f[0][1] = 0; rep(i, 1, n) { f[i][1] = mul(f[i - 1][0], k - 1); f[i][0] = add(mul(k - 2, f[i - 1][0]), f[i - 1][1]); }
rep(i, 1, n) { if (i & 1) cin >> a[cnta++]; else cin >> b[cntb++]; }
cout << mul(solve(a, cnta), solve(b, cntb)); return 0; }
|