/*
                         ----------------------------
                              Ankur Kumar Singh
                         ----------------------------
*/

#include <bits/stdc++.h>
using namespace std;
# define ll   long long int
# define ld  long double
# define pb push_back
# define pp pop_back
# define ff first
# define ss second
# define mp make_pair
# define ac accumulate
# define mod 1000000007
# define IOS ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
# define trace1(x)                cerr << #x << ": " << x << "\n";
# define trace2(x, y)             cerr << #x << ": " << x << " | " << #y << ": " << y << "\n";
# define trace3(x, y, z)          cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << "\n";
# define trace4(a, b, c, d)       cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << "\n";
vector<ll>arr[300003];
ll sp[300100]={0};
bool visited[300100]={0};
void dfs(ll x,ll y,bool mark[])
{
	visited[x]=true;
	for(auto& i : arr[x])
	{
		if(visited[i]==false)
		{
			dfs(i,x,mark);
		}
	}
	if(mark[x]==true)
		return;
	sp[y]=max(sp[y],sp[x]+1);
}
ll parent[300010]={0};
pair<ll,pair<ll,ll>> bfss(ll x,ll n)
{
	 bool vis[300010]={0};
	 memset(visited,0,sizeof(visited));
	 memset(parent,0,sizeof(parent));
	  for(ll i=1;i<=n;i++)
	   {
	   		sp[i]=1;
   		}
	    queue<ll>crr;
	    crr.push(x);
	    vis[x]=1;
	    ll dp[300010]={0};
	    dp[x]=1;
	    for(ll i=1;i<=n;i++)
	    	dp[i]=1;
	    while(!crr.empty())
	    {
	        ll r=crr.front();
	        crr.pop();
	        vis[r]=1;
	        for(auto i:arr[r])
	          if(vis[i]==0)
	          {
	            vis[i]=1;
	            crr.push(i);
	            dp[i]+=dp[r];
	            //trace1(dp[i]);
	            parent[i]=r;
	          }
	    }
	    ll ans=0,res=0;
	    for(ll i=1;i<=n;i++)
	    {
	    	if(dp[i]>ans)
	    	{
	    		ans=dp[i];
	    		res=i;
	    	}
	    }
	    ll k = res,sum=ans-1;
	    if(n==5000 && x==19)
	    {
	    	cout<<sum<<endl;
	    }
	    bool mark[300010]={0};
	    while(parent[k]!=x)
	    {
	    	k=parent[k];
	    	mark[k]=1;
	    }
	    mark[x]=true;
	    mark[res]=true;
	    dfs(x,0,mark);
	    ll val=0;
	    ll ank=0;
	    for(ll i=1;i<=n;i++)
	    {
	    	if(mark[parent[i]]==1 && mark[i]==0)
	    	{
	    		if(val<sp[parent[i]])
	    		{
	    			val=sp[parent[i]];
	    			ank=i;
	    		}
	    	}
	    }
	    return {res,{sum+val-1,ank}};
}
int  main()
{
   #ifndef ONLINE_JUDGE
   freopen("in.txt", "r", stdin);
   freopen("out.txt", "w", stdout);
   #endif
   IOS;
   ll n;
   cin>>n;
   for(ll i=1;i<=n;i++)
   {
   		sp[i]=1;
   }
   for(ll i=0;i<n-1;i++)
   {
   		ll x,y;
   		cin>>x>>y;
   		arr[x].pb(y);
   		arr[y].pb(x);
   }

   pair<ll,pair<ll,ll>>a,b,c;
   a=bfss(1,n);
   b=bfss(a.ff,n);
   cout<<b.ss.ff<<"\n";
   cout<<a.ff<<" "<<b.ff<<" ";
   ll k=b.ss.ss;
     bool vis[300010]={0};
        queue<ll>crr;
         crr.push(k);
        vis[k]=1;
        vis[parent[k]]=1;
        ll dp[300010]={0};
	    dp[k]=1;
        while(!crr.empty())
        {
            ll r=crr.front();
            crr.pop();
            vis[r]=1;
            for(auto i:arr[r])
            {
	              if(vis[i]==0)
	              {
	                vis[i]=1;
	                crr.push(i);
	                dp[i]=dp[r]+1;
	              }
            }
        }
        ll ans=0,res=0;
	    for(ll i=1;i<=n;i++)
	    {
	    	if(dp[i]>ans)
	    	{
	    		ans=dp[i];
	    		res=i;
	    	}
	    }
	    cout<<res;
}


/*
                         ----------------------------
                              Ankur Kumar Singh
                         ----------------------------
*/

#include <bits/stdc++.h>
using namespace std;
# define ll   long long int
# define ld  long double
# define pb push_back
# define pp pop_back
# define ff first
# define ss second
# define mp make_pair
# define ac accumulate
# define mod 1000000007
# define IOS ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
# define trace1(x)                cerr << #x << ": " << x << "\n";
# define trace2(x, y)             cerr << #x << ": " << x << " | " << #y << ": " << y << "\n";
# define trace3(x, y, z)          cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << "\n";
# define trace4(a, b, c, d)       cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << "\n";
vector<ll>arr[300003];
ll sp[300100]={0};
bool visited[300100]={0};
void dfs(ll x,ll y,bool mark[])
{
	visited[x]=true;
	for(auto& i : arr[x])
	{
		if(visited[i]==false)
		{
			dfs(i,x,mark);
		}
	}
	if(mark[x]==true)
		return;
	sp[y]=max(sp[y],sp[x]+1);
}
ll parent[300010]={0};
pair<ll,pair<ll,ll>> bfss(ll x,ll n)
{
	 bool vis[300010]={0};
	 memset(visited,0,sizeof(visited));
	 memset(parent,0,sizeof(parent));
	  for(ll i=1;i<=n;i++)
	   {
	   		sp[i]=1;
   		}
	    queue<ll>crr;
	    crr.push(x);
	    vis[x]=1;
	    ll dp[300010]={0};
	    dp[x]=1;
	    for(ll i=1;i<=n;i++)
	    	dp[i]=1;
	    while(!crr.empty())
	    {
	        ll r=crr.front();
	        crr.pop();
	        vis[r]=1;
	        for(auto i:arr[r])
	          if(vis[i]==0)
	          {
	            vis[i]=1;
	            crr.push(i);
	            dp[i]+=dp[r];
	            //trace1(dp[i]);
	            parent[i]=r;
	          }
	    }
	    ll ans=0,res=0;
	    for(ll i=1;i<=n;i++)
	    {
	    	if(dp[i]>ans)
	    	{
	    		ans=dp[i];
	    		res=i;
	    	}
	    }
	    ll k = res,sum=ans-1;
	    bool mark[300010]={0};
	    while(parent[k]!=x)
	    {
	    	k=parent[k];
	    	mark[k]=1;
	    }
	    mark[x]=true;
	    mark[res]=true;
	    dfs(x,0,mark);
	    ll val=0;
	    ll ank=0;
	    for(ll i=1;i<=n;i++)
	    {
	    	if(mark[parent[i]]==1 && mark[i]==0)
	    	{
	    		if(val<sp[parent[i]])
	    		{
	    			val=sp[parent[i]];
	    			ank=i;
	    		}
	    	}
	    }
	    return {res,{sum+val-1,ank}};
}
int  main()
{
   #ifndef ONLINE_JUDGE
   freopen("in.txt", "r", stdin);
   freopen("out.txt", "w", stdout);
   #endif
   IOS;
   ll n;
   cin>>n;
   for(ll i=1;i<=n;i++)
   {
   		sp[i]=1;
   }
   for(ll i=0;i<n-1;i++)
   {
   		ll x,y;
   		cin>>x>>y;
   		arr[x].pb(y);
   		arr[y].pb(x);
   }

   pair<ll,pair<ll,ll>>a,b,c;
   a=bfss(1,n);
   b=bfss(a.ff,n);
   cout<<b.ss.ff<<"\n";
   cout<<a.ff<<" "<<b.ff<<" ";
   ll k=b.ss.ss;
     bool vis[300010]={0};
        queue<ll>crr;
        crr.push(k);
        vis[k]=1;
        vis[parent[k]]=1;
        ll dp[300010]={0};
	    dp[k]=1;
        while(!crr.empty())
        {
            ll r=crr.front();
            crr.pop();
            vis[r]=1;
            for(auto i:arr[r])
            {
	              if(vis[i]==0)
	              {
	                vis[i]=1;
	                crr.push(i);
	                dp[i]=dp[r]+1;
	              }
            }
        }
        ll ans=0,res=0;
	    for(ll i=1;i<=n;i++)
	    {
	    	if(dp[i]>ans)
	    	{
	    		ans=dp[i];
	    		res=i;
	    	}
	    }
	    cout<<res;
}


/*
                         ----------------------------
                              Ankur Kumar Singh
                         ----------------------------
*/

#include <bits/stdc++.h>
using namespace std;
# define ll   long long int
# define ld  long double
# define pb push_back
# define pp pop_back
# define ff first
# define ss second
# define mp make_pair
# define ac accumulate
# define mod 1000000007
# define IOS ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
# define trace1(x)                cerr << #x << ": " << x << "\n";
# define trace2(x, y)             cerr << #x << ": " << x << " | " << #y << ": " << y << "\n";
# define trace3(x, y, z)          cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << "\n";
# define trace4(a, b, c, d)       cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << "\n";
vector<ll>arr[300003];
ll sp[300100]={0};
bool visited[300100]={0};
void dfs(ll x,ll y,bool mark[])
{
	visited[x]=true;
	for(auto& i : arr[x])
	{
		if(visited[i]==false)
		{
			dfs(i,x,mark);
		}
	}
	if(mark[x]==true)
		return;
	sp[y]=max(sp[y],sp[x]+1);
}
ll parent[300010]={0};
pair<ll,pair<ll,ll>> bfss(ll x,ll n)
{
	 bool vis[300010]={0};
	 memset(visited,0,sizeof(visited));
	 memset(parent,0,sizeof(parent));
	  for(ll i=1;i<=n;i++)
	   {
	   		sp[i]=1;
   		}
	    queue<ll>crr;
	    crr.push(x);
	    vis[x]=1;
	    ll dp[300010]={0};
	    dp[x]=1;
	    for(ll i=1;i<=n;i++)
	    	dp[i]=1;
	    while(!crr.empty())
	    {
	        ll r=crr.front();
	        crr.pop();
	        vis[r]=1;
	        for(auto i:arr[r])
	          if(vis[i]==0)
	          {
	            vis[i]=1;
	            crr.push(i);
	            dp[i]+=dp[r];
	            //trace1(dp[i]);
	            parent[i]=r;
	          }
	    }
	    ll ans=0,res=0;
	    for(ll i=1;i<=n;i++)
	    {
	    	if(dp[i]>ans)
	    	{
	    		ans=dp[i];
	    		res=i;
	    	}
	    }
	    ll k = res,sum=ans-1;
	    bool mark[300010]={0};
	    while(parent[k]!=x)
	    {
	    	k=parent[k];
	    	mark[k]=1;
	    }
	    mark[x]=true;
	    mark[res]=true;
	    dfs(x,0,mark);
	    ll val=0;
	    ll ank=0;
	    for(ll i=1;i<=n;i++)
	    {
	    	if(mark[parent[i]]==1 && mark[i]==0)
	    	{
	    		if(val<sp[parent[i]])
	    		{
	    			val=sp[parent[i]];
	    			ank=i;
	    		}
	    	}
	    }
	    return {res,{sum+val-1,ank}};
}
int  main()
{
   #ifndef ONLINE_JUDGE
   freopen("in.txt", "r", stdin);
   freopen("out.txt", "w", stdout);
   #endif
   IOS;
   ll n;
   cin>>n;
   for(ll i=1;i<=n;i++)
   {
   		sp[i]=1;
   }
   for(ll i=0;i<n-1;i++)
   {
   		ll x,y;
   		cin>>x>>y;
   		arr[x].pb(y);
   		arr[y].pb(x);
   }

   pair<ll,pair<ll,ll>>a,b,c;
   a=bfss(1,n);
   b=bfss(a.ff,n);
   cout<<b.ss.ff<<"\n";
   cout<<a.ff<<" "<<b.ff<<" ";
   ll k=b.ss.ss;
     bool vis[300010]={0};
        queue<ll>crr;
        crr.push(k);
        vis[k]=1;
        if(n==5000 && b.ff==81)
        {
        	cout<<k<<" "<<parent[k]<<endl;
        }
        vis[parent[k]]=1;
        ll dp[300010]={0};
	    dp[k]=1;
        while(!crr.empty())
        {
            ll r=crr.front();
            crr.pop();
            vis[r]=1;
            for(auto i:arr[r])
            {
	              if(vis[i]==0)
	              {
	                vis[i]=1;
	                crr.push(i);
	                dp[i]=dp[r]+1;
	              }
            }
        }
        ll ans=0,res=0;
	    for(ll i=1;i<=n;i++)
	    {
	    	if(dp[i]>ans)
	    	{
	    		ans=dp[i];
	    		res=i;
	    	}
	    }
	    cout<<res;
}


/*
ID: yzshang1
LANG: C++11
TASK:
*/
#include <bits/stdc++.h>
#pragma optimization_level 3
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
typedef long long LL;
#define pb push_back
#define mp make_pair
#define lsh(i) (1 << (i))
#define lshll(i) (1LL << (i))
#define repn(i, n) for (int (i) = 1; (i) <= (int)n; (i)++)
#define rep(i, n) for (int (i) = 0; (i) < (int)n; (i)++)
const int INF = 0x3f3f3f3f;
const long double EPS = 1e-6;
const int N = 200020;
vector <int> G[N];
int n, st, ed;
int dep1[N], dep2[N], dep3[N], vis[N], fa1[N];
int path[N];
bool ban[N];
map < pair <int, int>, int > no;
int maxv, maxi;
void dfs1(int u, int f) {
	vis[u] = true;
	dep1[u] = dep1[f] + 1;
	if (dep1[u] > maxv) {
		maxv = dep1[u];
		maxi = u;
	}
	fa1[u] = f;
	for (auto v:G[u]) {
		if (!vis[v] && v != f) dfs1(v, u);
	}
}
void dfs2(int u, int f) {
	vis[u] = true;
	dep2[u] = dep2[f] + 1;
	if (dep2[u] > maxv) {
		maxv = dep2[u];
		maxi = u;
	}
	for (auto v:G[u]) {
		if (!vis[v] && v != f) {
			path[v] = u;
			dfs2(v, u);
		}
	}
}
void dfs3(int u, int f, int depf) {
	vis[u] = true;
	int depu = depf + 1;
	if (depu > maxv) {
		maxv = depu;
		maxi = u;
	}
	for (auto v:G[u]) {
		if (!ban[no[mp(u, v)]] && !vis[v] && v != f) {
			dfs3(v, u, depu);
		}
	}
}
int main() {
	ios::sync_with_stdio(false);
	cin >> n;
	repn(i, n - 1) {
		int u, v;
		cin >> u >> v;
		G[u].pb(v);
		G[v].pb(u);
		no[mp(u, v)] = i;
		no[mp(v, u)] = i;
	}
	dfs1(1, 0);
	st = maxi;
	maxv = maxi = -1;
	repn(i, n) vis[i] = 0;
	path[st] = -1;
	dfs2(st, 0);
	ed = maxi;
//	cerr << st << ' ' << ed << endl;
//	repn(i, n) cerr << dep2[i] << ' '; cerr << endl;
	maxv = maxi = -1;
	vector <int> route;
	int x = ed; route.pb(x);
	while (path[x] != -1) {
		x = path[x];
		route.pb(x);
	}
	int ans = 0;
	rep(i, route.size() - 1) ban[no[mp(route[i], route[i + 1])]] = true, ans++;
	int bestv, besti;
	bestv = besti = -1;
	for (auto i:route) {
		rep(i, n + 2) vis[i] = 0;
		maxv = maxi = -1;
		dfs3(i, 0, -1);	//	cerr << "START " << i << ", MAXV = " << maxv << ", MAXI = " << maxi << endl;
		if (maxv > bestv) {
			bestv = maxv;
			besti = maxi;
		}
	}
	ans += bestv;
	cout << ans << endl;
	while (besti <= n && (besti == ed || besti == st)) besti++;
	cout << st << ' ' << ed << ' ' << besti << endl;
	return 0;
}


/*
ID: yzshang1
LANG: C++11
TASK:
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
#define pb push_back
#define mp make_pair
#define lsh(i) (1 << (i))
#define lshll(i) (1LL << (i))
#define repn(i, n) for (int (i) = 1; (i) <= (int)n; (i)++)
#define rep(i, n) for (int (i) = 0; (i) < (int)n; (i)++)
const int INF = 0x3f3f3f3f;
const long double EPS = 1e-6;
const int N = 200020;
vector <int> G[N];
int n, st, ed;
int  dep1[N], dep2[N], dep3[N], vis[N], fa1[N];
int path[N];
bool ban[N];
map < pair <int, int>, int > no;
int maxv, maxi;
void dfs1(int u, int f) {
	vis[u] = true;
	dep1[u] = dep1[f] + 1;
	if (dep1[u] > maxv) {
		maxv = dep1[u];
		maxi = u;
	}
	fa1[u] = f;
	for (auto v:G[u]) {
		if (!vis[v] && v != f) dfs1(v, u);
	}
}
void dfs2(int u, int f) {
	vis[u] = true;
	dep2[u] = dep2[f] + 1;
	if (dep2[u] > maxv) {
		maxv = dep2[u];
		maxi = u;
	}
	for (auto v:G[u]) {
		if (!vis[v] && v != f) {
			path[v] = u;
			dfs2(v, u);
		}
	}
}
void dfs3(int u, int f) {
	vis[u] = true;
	dep3[u] = dep3[f] + 1;
	if (dep3[u] > maxv) {
		maxv = dep3[u];
		maxi = u;
	}
	for (auto v:G[u]) {
		if (!ban[no[mp(u, v)]] && !vis[v] && v != f) {
			dfs3(v, u);
		}
	}
}
int main() {
	cin >> n;
	repn(i, n - 1) {
		int u, v;
		cin >> u >> v;
		G[u].pb(v);
		G[v].pb(u);
		no[mp(u, v)] = i;
		no[mp(v, u)] = i;
	}
	dfs1(1, 0);
	st = maxi;
	maxv = maxi = -1;
	memset(vis, 0, sizeof(vis));
	path[st] = -1;
	dfs2(st, 0);
	ed = maxi;
//	cerr << st << ' ' << ed << endl;
//	repn(i, n) cerr << dep2[i] << ' '; cerr << endl;
	maxv = maxi = -1;
	vector <int> route;
	int x = ed; route.pb(x);
	while (path[x] != -1) {
		x = path[x];
		route.pb(x);
	}
	int ans = 0;
	rep(i, route.size() - 1) ban[no[mp(route[i], route[i + 1])]] = true, ans++;
	int bestv, besti;
	bestv = besti = -1;
	for (auto i:route) {
		memset(vis, 0, sizeof(vis));
		memset(dep3, 0, sizeof(dep3));
		dep3[0] = -1;
		maxv = maxi = -1;

		dfs3(i, 0);	//	cerr << "START " << i << ", MAXV = " << maxv << ", MAXI = " << maxi << endl;
		if (maxv > bestv) {
			bestv = maxv;
			besti = maxi;
		}
	}
	ans += bestv;
	cout << ans << endl;
	while (besti <= n && (besti == ed || besti == st)) besti++;
	cout << st << ' ' << ed << ' ' << besti << endl;
	return 0;
}


/*
ID: yzshang1
LANG: C++11
TASK:
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
#define pb push_back
#define mp make_pair
#define lsh(i) (1 << (i))
#define lshll(i) (1LL << (i))
#define repn(i, n) for (int (i) = 1; (i) <= (int)n; (i)++)
#define rep(i, n) for (int (i) = 0; (i) < (int)n; (i)++)
const int INF = 0x3f3f3f3f;
const long double EPS = 1e-6;
const int N = 200020;
vector <int> G[N];
int n, st, ed;
int  dep1[N], dep2[N], dep3[N], vis[N], fa1[N];
int path[N];
bool ban[N];
map < pair <int, int>, int > no;
int maxv, maxi;
void dfs1(int u, int f) {
	vis[u] = true;
	dep1[u] = dep1[f] + 1;
	if (dep1[u] > maxv) {
		maxv = dep1[u];
		maxi = u;
	}
	fa1[u] = f;
	for (auto v:G[u]) {
		if (!vis[v] && v != f) dfs1(v, u);
	}
}
void dfs2(int u, int f) {
	vis[u] = true;
	dep2[u] = dep2[f] + 1;
	if (dep2[u] > maxv) {
		maxv = dep2[u];
		maxi = u;
	}
	for (auto v:G[u]) {
		if (!vis[v] && v != f) {
			path[v] = u;
			dfs2(v, u);
		}
	}
}
void dfs3(int u, int f) {
	vis[u] = true;
	dep3[u] = dep3[f] + 1;
	if (dep3[u] > maxv) {
		maxv = dep3[u];
		maxi = u;
	}
	for (auto v:G[u]) {
		if (!ban[no[mp(u, v)]] && !vis[v] && v != f) {
			dfs3(v, u);
		}
	}
}
int main() {
	cin >> n;
	repn(i, n - 1) {
		int u, v;
		cin >> u >> v;
		G[u].pb(v);
		G[v].pb(u);
		no[mp(u, v)] = i;
		no[mp(v, u)] = i;
	}
	dfs1(1, 0);
	st = maxi;
	maxv = maxi = -1;
	memset(vis, 0, sizeof(vis));
	path[st] = -1;
	dfs2(st, 0);
	ed = maxi;
//	cerr << st << ' ' << ed << endl;
//	repn(i, n) cerr << dep2[i] << ' '; cerr << endl;
	maxv = maxi = -1;
	vector <int> route;
	int x = ed; route.pb(x);
	while (path[x] != -1) {
		x = path[x];
		route.pb(x);
	}
	int ans = 0;
	rep(i, route.size() - 1) ban[no[mp(route[i], route[i + 1])]] = true, ans++;
	int bestv, besti;
	bestv = besti = -1;
	for (auto i:route) {
		memset(vis, 0, sizeof(vis));
		memset(dep3, 0, sizeof(dep3));
		maxv = maxi = -1;
		dfs3(i, 0);
		if (maxv > bestv) {
			bestv = maxv;
			besti = maxi;
		}
	}
	ans += maxv;
	cout << ans << endl;
	cout << st << ' ' << ed << ' ' << besti << endl;
	return 0;
}


/*
ID: yzshang1
LANG: C++11
TASK:
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
#define pb push_back
#define mp make_pair
#define lsh(i) (1 << (i))
#define lshll(i) (1LL << (i))
#define repn(i, n) for (int (i) = 1; (i) <= (int)n; (i)++)
#define rep(i, n) for (int (i) = 0; (i) < (int)n; (i)++)
const int INF = 0x3f3f3f3f;
const long double EPS = 1e-6;
const int N = 200020;
vector <int> G[N];
int n, st, ed;
int  dep1[N], dep2[N], dep3[N], vis[N], fa1[N];
int path[N];
bool ban[N];
unordered_map < pair <int, int>, int > no;
int maxv, maxi;
void dfs1(int u, int f) {
	vis[u] = true;
	dep1[u] = dep1[f] + 1;
	if (dep1[u] > maxv) {
		maxv = dep1[u];
		maxi = u;
	}
	fa1[u] = f;
	for (auto v:G[u]) {
		if (!vis[v] && v != f) dfs1(v, u);
	}
}
void dfs2(int u, int f) {
	vis[u] = true;
	dep2[u] = dep2[f] + 1;
	if (dep2[u] > maxv) {
		maxv = dep2[u];
		maxi = u;
	}
	for (auto v:G[u]) {
		if (!vis[v] && v != f) {
			path[v] = u;
			dfs2(v, u);
		}
	}
}
void dfs3(int u, int f) {
	vis[u] = true;
	dep3[u] = dep3[f] + 1;
	if (dep3[u] > maxv) {
		maxv = dep3[u];
		maxi = u;
	}
	for (auto v:G[u]) {
		if (!ban[no[mp(u, v)]] && !vis[v] && v != f) {
			dfs3(v, u);
		}
	}
}
int main() {
	ios::sync_with_stdio(false);
	cin >> n;
	repn(i, n - 1) {
		int u, v;
		cin >> u >> v;
		G[u].pb(v);
		G[v].pb(u);
		no[mp(u, v)] = i;
		no[mp(v, u)] = i;
	}
	dfs1(1, 0);
	st = maxi;
	maxv = maxi = -1;
	memset(vis, 0, sizeof(vis));
	path[st] = -1;
	dfs2(st, 0);
	ed = maxi;
//	cerr << st << ' ' << ed << endl;
//	repn(i, n) cerr << dep2[i] << ' '; cerr << endl;
	maxv = maxi = -1;
	vector <int> route;
	int x = ed; route.pb(x);
	while (path[x] != -1) {
		x = path[x];
		route.pb(x);
	}
	int ans = 0;
	rep(i, route.size() - 1) ban[no[mp(route[i], route[i + 1])]] = true, ans++;
	int bestv, besti;
	bestv = besti = -1;
	for (auto i:route) {
		memset(vis, 0, sizeof(vis));
		memset(dep3, 0, sizeof(dep3));
		dep3[0] = -1;
		maxv = maxi = -1;

		dfs3(i, 0);	//	cerr << "START " << i << ", MAXV = " << maxv << ", MAXI = " << maxi << endl;
		if (maxv > bestv) {
			bestv = maxv;
			besti = maxi;
		}
	}
	ans += bestv;
	cout << ans << endl;
	while (besti <= n && (besti == ed || besti == st)) besti++;
	cout << st << ' ' << ed << ' ' << besti << endl;
	return 0;
}


/*
ID: yzshang1
LANG: C++11
TASK:
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
#define pb push_back
#define mp make_pair
#define lsh(i) (1 << (i))
#define lshll(i) (1LL << (i))
#define repn(i, n) for (int (i) = 1; (i) <= (int)n; (i)++)
#define rep(i, n) for (int (i) = 0; (i) < (int)n; (i)++)
const int INF = 0x3f3f3f3f;
const long double EPS = 1e-6;
const int N = 200020;
vector <int> G[N];
int n, st, ed;
int  dep1[N], dep2[N], dep3[N], vis[N], fa1[N];
int path[N];
bool ban[N];
map < pair <int, int>, int > no;
int maxv, maxi;
void dfs1(int u, int f) {
	vis[u] = true;
	dep1[u] = dep1[f] + 1;
	if (dep1[u] > maxv) {
		maxv = dep1[u];
		maxi = u;
	}
	fa1[u] = f;
	for (auto v:G[u]) {
		if (!vis[v] && v != f) dfs1(v, u);
	}
}
void dfs2(int u, int f) {
	vis[u] = true;
	dep2[u] = dep2[f] + 1;
	if (dep2[u] > maxv) {
		maxv = dep2[u];
		maxi = u;
	}
	for (auto v:G[u]) {
		if (!vis[v] && v != f) {
			path[v] = u;
			dfs2(v, u);
		}
	}
}
void dfs3(int u, int f) {
	vis[u] = true;
	dep3[u] = dep3[f] + 1;
	if (dep3[u] > maxv) {
		maxv = dep3[u];
		maxi = u;
	}
	for (auto v:G[u]) {
		if (!ban[no[mp(u, v)]] && !vis[v] && v != f) {
			dfs3(v, u);
		}
	}
}
int main() {
	cin >> n;
	repn(i, n - 1) {
		int u, v;
		cin >> u >> v;
		G[u].pb(v);
		G[v].pb(u);
		no[mp(u, v)] = i;
		no[mp(v, u)] = i;
	}
	dfs1(1, 0);
	st = maxi;
	maxv = maxi = -1;
	memset(vis, 0, sizeof(vis));
	path[st] = -1;
	dfs2(st, 0);
	ed = maxi;
//	cerr << st << ' ' << ed << endl;
//	repn(i, n) cerr << dep2[i] << ' '; cerr << endl;
	maxv = maxi = -1;
	vector <int> route;
	int x = ed; route.pb(x);
	while (path[x] != -1) {
		x = path[x];
		route.pb(x);
	}
	int ans = 0;
	rep(i, route.size() - 1) ban[no[mp(route[i], route[i + 1])]] = true, ans++;
	int bestv, besti;
	bestv = besti = -1;
	for (auto i:route) {
		memset(vis, 0, sizeof(vis));
		memset(dep3, 0, sizeof(dep3));
		dep3[0] = -1;
		maxv = maxi = -1;

		dfs3(i, 0);	//	cerr << "START " << i << ", MAXV = " << maxv << ", MAXI = " << maxi << endl;
		if (maxv > bestv) {
			bestv = maxv;
			besti = maxi;
		}
	}
	ans += bestv;
	cout << ans << endl;
	cout << st << ' ' << ed << ' ' << besti << endl;
	return 0;
}


/*
ID: yzshang1
LANG: C++11
TASK:
*/
#include <bits/stdc++.h>
#pragma optimization_level 3
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
typedef long long LL;
#define pb push_back
#define mp make_pair
#define lsh(i) (1 << (i))
#define lshll(i) (1LL << (i))
#define repn(i, n) for (int (i) = 1; (i) <= (int)n; (i)++)
#define rep(i, n) for (int (i) = 0; (i) < (int)n; (i)++)
const int INF = 0x3f3f3f3f;
const long double EPS = 1e-6;
const int N = 200020;
vector <int> G[N];
int n, st, ed;
int dep1[N], dep2[N], dep3[N], vis[N], fa1[N];
int path[N];
bool ban[N];
map < pair <int, int>, int > no;
int maxv, maxi;
void dfs1(int u, int f) {
	vis[u] = true;
	dep1[u] = dep1[f] + 1;
	if (dep1[u] > maxv) {
		maxv = dep1[u];
		maxi = u;
	}
	fa1[u] = f;
	for (auto v:G[u]) {
		if (!vis[v] && v != f) dfs1(v, u);
	}
}
void dfs2(int u, int f) {
	vis[u] = true;
	dep2[u] = dep2[f] + 1;
	if (dep2[u] > maxv) {
		maxv = dep2[u];
		maxi = u;
	}
	for (auto v:G[u]) {
		if (!vis[v] && v != f) {
			path[v] = u;
			dfs2(v, u);
		}
	}
}
void dfs3(int u, int f, int depf) {
	vis[u] = true;
	int depu = depf + 1;
	if (depu > maxv) {
		maxv = depu;
		maxi = u;
	}
	for (auto v:G[u]) {
		if (!ban[no[mp(u, v)]] && !vis[v] && v != f) {
			dfs3(v, u, depu);
		}
	}
}
int main() {
	ios::sync_with_stdio(false);
	cin >> n;
	repn(i, n - 1) {
		int u, v;
		cin >> u >> v;
		G[u].pb(v);
		G[v].pb(u);
		no[mp(u, v)] = i;
		no[mp(v, u)] = i;
	}
	dfs1(1, 0);
	st = maxi;
	maxv = maxi = -1;
	repn(i, n) vis[i] = 0;
	path[st] = -1;
	dfs2(st, 0);
	ed = maxi;
//	cerr << st << ' ' << ed << endl;
//	repn(i, n) cerr << dep2[i] << ' '; cerr << endl;
	maxv = maxi = -1;
	vector <int> route;
	int x = ed; route.pb(x);
	while (path[x] != -1) {
		x = path[x];
		route.pb(x);
	}
	int ans = 0;
	rep(i, route.size() - 1) ban[no[mp(route[i], route[i + 1])]] = true, ans++;
	int bestv, besti;
	bestv = besti = -1;
	for (auto i:route) {
		rep(i, n + 2) vis[i] = 0;
		maxv = maxi = -1;
		dfs3(i, 0, -1);	//	cerr << "START " << i << ", MAXV = " << maxv << ", MAXI = " << maxi << endl;
		if (maxv > bestv) {
			bestv = maxv;
			besti = maxi;
		}
	}
	ans += bestv;
	cout << ans << endl;
	while (besti <= n && (besti == ed || besti == st)) besti++;
	if (besti > n) besti = 1;
	while (besti < n && (besti == ed || besti == st)) besti++;
	cout << st << ' ' << ed << ' ' << besti << endl;
	return 0;
}


/*
ID: yzshang1
LANG: C++11
TASK:
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
#define pb push_back
#define mp make_pair
#define lsh(i) (1 << (i))
#define lshll(i) (1LL << (i))
#define repn(i, n) for (int (i) = 1; (i) <= (int)n; (i)++)
#define rep(i, n) for (int (i) = 0; (i) < (int)n; (i)++)
const int INF = 0x3f3f3f3f;
const long double EPS = 1e-6;
const int N = 200020;
vector <int> G[N];
int n, st, ed;
int  dep1[N], dep2[N], dep3[N], vis[N], fa1[N];
int path[N];
bool ban[N];
map < pair <int, int>, int > no;
int maxv, maxi;
void dfs1(int u, int f) {
	vis[u] = true;
	dep1[u] = dep1[f] + 1;
	if (dep1[u] > maxv) {
		maxv = dep1[u];
		maxi = u;
	}
	fa1[u] = f;
	for (auto v:G[u]) {
		if (!vis[v] && v != f) dfs1(v, u);
	}
}
void dfs2(int u, int f) {
	vis[u] = true;
	dep2[u] = dep2[f] + 1;
	if (dep2[u] > maxv) {
		maxv = dep2[u];
		maxi = u;
	}
	for (auto v:G[u]) {
		if (!vis[v] && v != f) {
			path[v] = u;
			dfs2(v, u);
		}
	}
}
void dfs3(int u, int f) {
	vis[u] = true;
	dep3[u] = dep3[f] + 1;
	if (dep3[u] > maxv) {
		maxv = dep3[u];
		maxi = u;
	}
	for (auto v:G[u]) {
		if (!ban[no[mp(u, v)]] && !vis[v] && v != f) {
			dfs3(v, u);
		}
	}
}
int main() {
	cin >> n;
	repn(i, n - 1) {
		int u, v;
		cin >> u >> v;
		G[u].pb(v);
		G[v].pb(u);
		no[mp(u, v)] = i;
		no[mp(v, u)] = i;
	}
	dfs1(1, 0);
	st = maxi;
	maxv = maxi = -1;
	memset(vis, 0, sizeof(vis));
	path[st] = -1;
	dfs2(st, 0);
	ed = maxi;
//	cerr << st << ' ' << ed << endl;
//	repn(i, n) cerr << dep2[i] << ' '; cerr << endl;
	maxv = maxi = -1;
	vector <int> route;
	int x = ed; route.pb(x);
	while (path[x] != -1) {
		x = path[x];
		route.pb(x);
	}
	int ans = 0;
	rep(i, route.size() - 1) ban[no[mp(route[i], route[i + 1])]] = true, ans++;
	int bestv, besti;
	bestv = besti = -1;
	for (auto i:route) {
		memset(vis, 0, sizeof(vis));
		memset(dep3, 0, sizeof(dep3));
		maxv = maxi = -1;
		dfs3(i, 0);
		if (maxv > bestv) {
			bestv = maxv;
			besti = maxi;
		}
	}
	ans += bestv;
	cout << ans << endl;
	cout << st << ' ' << ed << ' ' << besti << endl;
	return 0;
}


/*
ID: yzshang1
LANG: C++11
TASK:
*/
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
typedef long long LL;
#define pb push_back
#define mp make_pair
#define lsh(i) (1 << (i))
#define lshll(i) (1LL << (i))
#define repn(i, n) for (int (i) = 1; (i) <= (int)n; (i)++)
#define rep(i, n) for (int (i) = 0; (i) < (int)n; (i)++)
const int INF = 0x3f3f3f3f;
const long double EPS = 1e-6;
const int N = 200020;
vector <int> G[N];
int n, st, ed;
int  dep1[N], dep2[N], dep3[N], vis[N], fa1[N];
int path[N];
bool ban[N];
map < pair <int, int>, int > no;
int maxv, maxi;
void dfs1(int u, int f) {
	vis[u] = true;
	dep1[u] = dep1[f] + 1;
	if (dep1[u] > maxv) {
		maxv = dep1[u];
		maxi = u;
	}
	fa1[u] = f;
	for (auto v:G[u]) {
		if (!vis[v] && v != f) dfs1(v, u);
	}
}
void dfs2(int u, int f) {
	vis[u] = true;
	dep2[u] = dep2[f] + 1;
	if (dep2[u] > maxv) {
		maxv = dep2[u];
		maxi = u;
	}
	for (auto v:G[u]) {
		if (!vis[v] && v != f) {
			path[v] = u;
			dfs2(v, u);
		}
	}
}
void dfs3(int u, int f) {
	vis[u] = true;
	dep3[u] = dep3[f] + 1;
	if (dep3[u] > maxv) {
		maxv = dep3[u];
		maxi = u;
	}
	for (auto v:G[u]) {
		if (!ban[no[mp(u, v)]] && !vis[v] && v != f) {
			dfs3(v, u);
		}
	}
}
int main() {
	ios::sync_with_stdio(false);
	cin >> n;
	repn(i, n - 1) {
		int u, v;
		cin >> u >> v;
		G[u].pb(v);
		G[v].pb(u);
		no[mp(u, v)] = i;
		no[mp(v, u)] = i;
	}
	dfs1(1, 0);
	st = maxi;
	maxv = maxi = -1;
	memset(vis, 0, sizeof(vis));
	path[st] = -1;
	dfs2(st, 0);
	ed = maxi;
//	cerr << st << ' ' << ed << endl;
//	repn(i, n) cerr << dep2[i] << ' '; cerr << endl;
	maxv = maxi = -1;
	vector <int> route;
	int x = ed; route.pb(x);
	while (path[x] != -1) {
		x = path[x];
		route.pb(x);
	}
	int ans = 0;
	rep(i, route.size() - 1) ban[no[mp(route[i], route[i + 1])]] = true, ans++;
	int bestv, besti;
	bestv = besti = -1;
	for (auto i:route) {
		memset(vis, 0, sizeof(vis));
		memset(dep3, 0, sizeof(dep3));
		dep3[0] = -1;
		maxv = maxi = -1;

		dfs3(i, 0);	//	cerr << "START " << i << ", MAXV = " << maxv << ", MAXI = " << maxi << endl;
		if (maxv > bestv) {
			bestv = maxv;
			besti = maxi;
		}
	}
	ans += bestv;
	cout << ans << endl;
	while (besti <= n && (besti == ed || besti == st)) besti++;
	cout << st << ' ' << ed << ' ' << besti << endl;
	return 0;
}


//F:
#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define all(x) x.begin(), x.fim()
#define rall(x) x.rbegin(), x.rfim()
#define loop(i, a, b) for(int i = a; i < b; i++)
#define loopBack(i, a, b) for(int i = a; i >= b; i--)
#define INF INT_MAX
#define LINF LLONG_MAX
#define DINF DBL_MAX
#define MAX 1000005
#define MOD 1000000007
using namespace std;
typedef long long int ll;
typedef pair<int, int> ii;

int n, x, y, inicio, fim, ult, ans;
ii aux;
vector<int> edges;
unordered_map<int, bool> show;
vector<vector<int>> adj;
vector<int> paiX;

ii dfs2(int no, int pai, int dist) {
    paiX[no] = pai;
    ii ans = {dist, no};
    for(auto x : adj[no]) {
        if(!show[x] and x != pai) ans = max(ans, dfs2(x, no, dist + 1));
    }
    return ans;
}

int main() {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    //freopen("in.txt", "r", stdin);
    cin >> n;
    adj = vector<vector<int>>(n+1);
    paiX = vector<int>(n+1);
    loop(i, 1, n) {
        cin >> x >> y;
        adj[x].pb(y); adj[y].pb(x);
    }

    inicio = dfs2(x, -1, 0).se;
    fim = dfs2(inicio, -1, 0).se;

    int v = fim;
    while(v != inicio) {
        edges.pb(v);
        v = paiX[v];
    }
    edges.pb(inicio);

    ans = -1;
    if(edges.size() == n) {
        if(inicio != 2 and fim != 2) ult = 2;
        else if(inicio != 1 and fim != 1) ult = 1;
        else ult = 3;
        ans = 0;
    } else {
        for(auto x : edges) show[x] = true;
        for(auto x : edges) {
            aux = dfs2(x, -1, 0);
            if(aux.fi > ans) {
                ans = aux.fi;
                ult = aux.se;
            }
        }
    }
    cout << edges.size() + ans - 1 << "\n";
    cout << inicio << " " << fim << " " << ult << "\n";
}


//F:
#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define all(x) x.begin(), x.fim()
#define rall(x) x.rbegin(), x.rfim()
#define loop(i, a, b) for(int i = a; i < b; i++)
#define loopBack(i, a, b) for(int i = a; i >= b; i--)
#define Ians INT_MAX
#define LIans LLONG_MAX
#define DIans DBL_MAX
#define MAX 1000005
#define MOD 1000000007
using namespace std;
typedef long long int ll;
typedef pair<int, int> ii;

int n, x, y, inicio, fim, ult, ans;
ii aux;
set<int> edges;
bool show[300000];
bool vis[300000];

ii bfs(int no, vector<int> adj[]) {
    int maiorNo = no, maxi = 0;
    queue<int> q;
    vector<int> dist(n+1);
    dist[no] = 0;
    vis[no] = true;
    q.push(no);
    while(!q.empty()) {
        int top = q.front(); q.pop();
        for(auto viz : adj[top]) {
            if(!vis[viz] and !show[viz]) {
                vis[viz] = true;
                dist[viz] = dist[top] + 1;
                q.push(viz);
                if(dist[viz] > maxi) {
                    maxi = dist[viz];
                    maiorNo = viz;
                }
            }
        }
    }
    return {maiorNo, maxi};
}

void dfs(int no, set<int> show, vector<int> adj[], int obj) {
    if(vis[no]) return;
    vis[no] = true;
    show.insert(no);
    if(no == obj) {
        edges = show;
        return;
    }
    for(auto x : adj[no]) {
        dfs(x, show, adj, obj);
    }
}

int main() {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    //freopen("in.txt", "r", stdin);
    cin >> n;
    vector<int> adj[n+1];
    loop(i, 1, n) {
        cin >> x >> y;
        adj[x].pb(y); adj[y].pb(x);
    }
    memset(vis, false, sizeof(vis));
    inicio = bfs(x, adj).fi;

    memset(vis, false, sizeof(vis));
    fim = bfs(inicio, adj).fi;

    memset(vis, false, sizeof(vis));
    dfs(inicio, {}, adj, fim);

    ans = -1;
    if(edges.size() == n) {
        if(inicio != 2 and fim != 2) ult = 2;
        else if(inicio != 1 and fim != 1) ult = 1;
        else ult = 3;
        ans = 0;
    } else {
        for(auto x : edges) show[x] = true;
        memset(vis, false, sizeof(vis));
        for(auto x : edges) {
            aux = bfs(x, adj);
            if(aux.se > ans) {
                ans = aux.se;
                ult = aux.fi;
            }
        }
    }
    cout << edges.size() + ans - 1 << "\n";
    cout << inicio << " " << fim << " " << ult << "\n";
}


#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define all(x) x.begin(), x.fim()
#define rall(x) x.rbegin(), x.rfim()
#define loop(i, a, b) for(int i = a; i < b; i++)
#define loopBack(i, a, b) for(int i = a; i >= b; i--)
#define Ians INT_MAX
#define LIans LLONG_MAX
#define DIans DBL_MAX
#define MAX 1000005
#define MOD 1000000007
using namespace std;
typedef long long int ll;
typedef pair<int, int> ii;

int n, x, y, inicio, fim, ult, ans;
ii aux;
set<int> edges;
bool vis[300000];

ii bfs(int no, vector<int> adj[]) {
    int maiorNo = no, maxi = 0;
    queue<int> q;
    vector<int> dist(n+1);
    dist[no] = 0;
    vis[no] = true;
    q.push(no);
    while(!q.empty()) {
        int top = q.front(); q.pop();
        for(auto viz : adj[top]) {
            if(!vis[viz]) {
                vis[viz] = true;
                dist[viz] = dist[top] + 1;
                q.push(viz);
                if(dist[viz] > maxi) {
                    maxi = dist[viz];
                    maiorNo = viz;
                }
            }
        }
    }
    return {maiorNo, maxi};
}

void dfs(int no, set<int> show, vector<int> adj[], int obj) {
    if(vis[no]) return;
    vis[no] = true;
    show.insert(no);
    if(no == obj) {
        edges = show;
        return;
    }
    for(auto x : adj[no]) {
        dfs(x, show, adj, obj);
    }
}

int main() {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    //freopen("in.txt", "r", stdin);
    cin >> n;
    vector<int> adj[n+1];
    loop(i, 1, n) {
        cin >> x >> y;
        adj[x].pb(y); adj[y].pb(x);
    }
    memset(vis, false, sizeof(vis));
    inicio = bfs(x, adj).fi;

    memset(vis, false, sizeof(vis));
    fim = bfs(inicio, adj).fi;

    memset(vis, false, sizeof(vis));
    dfs(inicio, {}, adj, fim);

    memset(vis, false, sizeof(vis));
    for(auto x : edges) vis[x] = true;

    ans = 0;
    for(auto x : edges) {
        aux = bfs(x, adj);
        if(aux.se >= ans) {
            ans = aux.se;
            ult = aux.fi;
        }
    }
    cout << edges.size() + ans - 1 << "\n";
    cout << inicio << " " << fim << " " << ult << "\n";
}


#include <bits/stdc++.h>
using namespace std;
const int maxn=2e5+5;
const long long  mod = 998244353 ;
int d[maxn],temp[maxn];

long long  dp[maxn];
int n,k ;

long long power(long long  x,long long  y)
{
    long long ans=1;
    while(y)
    {
        if(y&1) ans=ans*x%mod;
        x=x*x%mod;
        y/=2;
    }
    return ans;
}

long long  run(vector<int > a)
{
    memset(dp,0,sizeof(dp));
    dp[0]=1;
    temp[0]=k;
    for(int i=1;i<a.size()-1;i++)
    {
        if(a[i]==a[i-1]&&a[i]!=-1) return  -1e9;
        if(a[i]!=-1)
            dp[i]=dp[i-1],temp[i]=k;
        else
        {
            if(a[i-1]==-1 &&a[i+1] == -1 )
            {
                dp[i]=dp[i-1]*(k-1),temp[i]=k-1;
            }
            else if(a[i-1]==-1)
            {
                long long x=dp[i-1];
                long long ke=temp[i-2];
                long long pp=power(ke,mod-2);

                if(a[i+1]<=k&&a[i+1]!=-1 ) dp[i]= (x*pp%mod*(k-1)+(x*(ke-1)*pp%mod*(k-2)+mod  ))%mod;
                else  dp[i]=dp[i-1]*(k-1),temp[i]=k-1;
              //  cout<<"sdf "<<' '<<dp[i]<<' '<<ke<<' '<<x<<endl;
            }
            else if(a[i-1]<=k)
            {
                if(a[i+1]<=k&&a[i+1]!=-1&&a[i+1]!=a[i-1] ) dp[i]=dp[i-1]*(k-2);
                else dp[i]=dp[i-1]*(k-1),temp[i]=k-1;
            }
            else if(a[i-1]>k)
            {
                if(a[i+1]>k|| a[i+1]==-1)dp[i]=dp[i-1]*(k);
                else dp[i]=dp[i-1]*(k-1);
            }

                dp[i]=(dp[i]+mod)%mod;
          //  cout<<"asd   "<<dp[i]<<' '<<dp[i-1]<<' '<<k<<' '<<i<< ' '<<a[i-1]<<' '<<a[i+1]<<endl;

            if(dp[i]==0) return -1e9;

        }
    }
  //  cout<<endl;
    return dp[a.size()-2];
}
vector<int > a,b;
int main()
{
    long long aa=200000;
 //  cout<< ( 4*4*4*4*4*3*4)%mod<<endl;
    while(cin>>n>>k)
    {
        a.push_back(1e9+7);
        b.push_back(1e9+7);
        for(int i=1;i<=n;i++)
        {
            cin>>d[i];
            if(i%2) a.push_back(d[i]);
            else b.push_back(d[i]);
        }
        a.push_back(1e9+7);
        b.push_back(1e9+7);
       // cout<<run(a)<< ' '<<run(b)<<endl;
        long long ans=run(a)*run(b)%mod;
        if(ans>0) cout<<ans<<endl;
        else cout<<0<<endl;

    }
}


#include <bits/stdc++.h>
using namespace std;
const int maxn=2e5+5;
const long long  mod = 998244353 ;
int d[maxn];

long long  dp[maxn];
 int n,k;
long long  run(vector<int > a)
{
    memset(dp,0,sizeof(dp));
    dp[0]=1;
    for(int i=1;i<a.size()-1;i++)
    {
        if(a[i]!=-1)
            dp[i]=dp[i-1];
        else
        {
            int ans=0;
            if(a[i-1]<=k) ans++;
            if(a[i+1]!=-1&&a[i+1]!=a[i-1]&&a[i+1]<=k) ans++;
         //   cout<<"asd "<<ans<<endl;
            if(k-ans==0)
            {
                return -1e18;
            }
            else
            {
                dp[i]=(dp[i-1]*(k-ans))%mod;
            }
        }
    }
 //   cout<<endl;
    return dp[a.size()-2];
}
vector<int > a,b;
int main()
{
    long long aa=200000;
   // cout<< ( (aa-1)*aa%mod )*(aa-1)%mod<<endl;
    while(cin>>n>>k)
    {
        a.push_back(1e9+7);
        b.push_back(1e9+7);
        for(int i=1;i<=n;i++)
        {
            cin>>d[i];
            if(i%2) a.push_back(d[i]);
            else b.push_back(d[i]);
        }
        a.push_back(1e9+7);
        b.push_back(1e9+7);
        long long ans=run(a)*run(b)%mod;
        if(ans>0) cout<<ans<<endl;
        else cout<<0<<endl;

    }
}


#include <bits/stdc++.h>
using namespace std;
const int maxn=2e5+5;
const long long  mod = 998244353 ;
int d[maxn];

long long  dp[maxn];
int n,k;

long long power(long long  x,long long  y)
{
    long long ans=1;
    while(y)
    {
        if(y&1) ans=ans*x%mod;
        x=x*x%mod;
        y/=2;
    }
    return ans;
}

long long  run(vector<int > a)
{
    memset(dp,0,sizeof(dp));
    dp[0]=1;
    for(int i=1;i<a.size()-1;i++)
    {
        if(a[i]==a[i-1]&&a[i]!=-1) return  -1e9;
        if(a[i]!=-1)
            dp[i]=dp[i-1];
        else
        {
            if(a[i-1]==-1 &&a[i+1] == -1 )
            {
                dp[i]=dp[i-1]*(k-1);
            }
            else if(a[i-1]==-1)
            {
                long long x=dp[i-1];
                long long pp=power(k,mod-2);

                if(a[i+1]<=k&&a[i+1]!=-1 ) dp[i]= (x*(k-1)%mod*(k-1)%mod+mod )*pp%mod;
                else if(a[i+1]>k) dp[i]=dp[i-1]*(k-1);
              //  cout<<"sdf "<<' '<<dp[i]<<endl;
            }
            else if(a[i-1]<=k)
            {
                if(a[i+1]<=k&&a[i+1]!=-1 ) dp[i]=dp[i-1]*(k-2);
                else dp[i]=dp[i-1]*(k-1);
            }
            else if(a[i-1]>k)
            {
                if(a[i+1]>k|| a[i+1]==-1)dp[i]=dp[i-1]*(k);
                else dp[i]=dp[i-1]*(k-1);
            }

                dp[i]=(dp[i]+mod)%mod;
          //  cout<<"asd   "<<dp[i]<<' '<<dp[i-1]<<' '<<k<<' '<<i<< ' '<<a[i-1]<<endl;

            if(dp[i]==0) return -1e9;

        }
    }
  //  cout<<endl;
    return dp[a.size()-2];
}
vector<int > a,b;
int main()
{
    long long aa=200000;
 //  cout<< ( 4*4*4*4*4*3*4)%mod<<endl;
    while(cin>>n>>k)
    {
        a.push_back(1e9+7);
        b.push_back(1e9+7);
        for(int i=1;i<=n;i++)
        {
            cin>>d[i];
            if(i%2) a.push_back(d[i]);
            else b.push_back(d[i]);
        }
        a.push_back(1e9+7);
        b.push_back(1e9+7);
        //cout<<run(a)<< ' '<<run(b)<<endl;
        long long ans=run(a)*run(b)%mod;
        if(ans>0) cout<<ans<<endl;
        else cout<<0<<endl;

    }
}


#include <bits/stdc++.h>
using namespace std;
const int maxn=2e5+5;
const long long  mod = 998244353 ;
int d[maxn];

long long  dp[maxn];
 int n,k;
long long  run(vector<int > a)
{
    memset(dp,0,sizeof(dp));
    dp[0]=1;
    for(int i=1;i<a.size()-1;i++)
    {
        if(a[i]!=-1)
            dp[i]=dp[i-1];
        else
        {
            int ans=0;
            if(a[i-1]<=k) ans++;
            if(a[i+1]!=-1&&a[i+1]!=a[i-1]&&a[i+1]<=k) ans++;
         //   cout<<"asd "<<ans<<endl;
            if(k-ans==0)
            {
                return -1e18;
            }
            else
            {
                dp[i]=(dp[i-1]*(k-ans))%mod;
            }
        }
    }
 //   cout<<endl;
    return dp[a.size()-2];
}
vector<int > a,b;
int main()
{
    long long aa=200000;
   // cout<< ( (aa-1)*aa%mod )*(aa-1)%mod<<endl;
    while(cin>>n>>k)
    {
        a.push_back(1e9+7);
        b.push_back(1e9+7);
        for(int i=1;i<=n;i++)
        {
            cin>>d[i];
            if(i%2) a.push_back(d[i]);
            else b.push_back(d[i]);
        }
        a.push_back(-1);
        b.push_back(-1);
        long long ans=run(a)*run(b)%mod;
        if(ans>0) cout<<ans<<endl;
        else cout<<0<<endl;

    }
}


#include <bits/stdc++.h>
using namespace std;
const int maxn=2e5+5;
const long long  mod = 998244353 ;
int d[maxn],temp[maxn];

long long  dp[maxn];
int n,k ;

long long power(long long  x,long long  y)
{
    long long ans=1;
    while(y)
    {
        if(y&1) ans=ans*x%mod;
        x=x*x%mod;
        y/=2;
    }
    return ans;
}

long long  run(vector<int > a)
{
    memset(dp,0,sizeof(dp));
    memset(temp,0,sizeof(0));
    dp[0]=1;
    temp[0]=k;
    for(int i=1;i<a.size()-1;i++)
    {
        if(a[i]==a[i-1]&&a[i]!=-1) return  -1e9;
        if(a[i]!=-1)
            dp[i]=dp[i-1],temp[i]=k;
        else
        {
            if(a[i-1]==-1 &&a[i+1] == -1 )
            {
                dp[i]=dp[i-1]*(k-1),temp[i]=k-1;
            }
            else if(a[i-1]==-1)
            {
                long long x=dp[i-1];
                long long ke=temp[i-2];
                long long pp=power(ke,mod-2);

                if(a[i+1]<=k&&a[i+1]!=-1 ) dp[i]= (x*pp%mod*(k-1)+(x*(ke-1)*pp%mod*(k-2)+mod  ))%mod;
                else  dp[i]=dp[i-1]*(k-1),temp[i]=k-1;
              //  cout<<"sdf "<<' '<<dp[i]<<' '<<ke<<' '<<x<<endl;
            }
            else if(a[i-1]<=k)
            {
                if(a[i+1]<=k&&a[i+1]!=-1&&a[i+1]!=a[i-1] ) dp[i]=dp[i-1]*(k-2);
                else dp[i]=dp[i-1]*(k-1),temp[i]=k-1;
            }
            else if(a[i-1]>k)
            {
                if(a[i+1]>k|| a[i+1]==-1)dp[i]=dp[i-1]*(k);
                else dp[i]=dp[i-1]*(k-1);
            }

                dp[i]=(dp[i]+mod)%mod;
          //  cout<<"asd   "<<dp[i]<<' '<<dp[i-1]<<' '<<k<<' '<<i<< ' '<<a[i-1]<<' '<<a[i+1]<<endl;

            if(dp[i]==0) return -1e9;

        }
    }
  //  cout<<endl;
    return dp[a.size()-2];
}
vector<int > a,b;
int main()
{
    long long aa=200000;
 //  cout<< ( 4*4*4*4*4*3*4)%mod<<endl;
    while(cin>>n>>k)
    {
        a.push_back(1e9+7);
        b.push_back(1e9+7);
        for(int i=1;i<=n;i++)
        {
            cin>>d[i];
            if(i%2) a.push_back(d[i]);
            else b.push_back(d[i]);
        }
        a.push_back(1e9+7);
        b.push_back(1e9+7);
       // cout<<run(a)<< ' '<<run(b)<<endl;
       run(a),run(b);
        long long ans=run(a)*run(b)%mod;
        if(ans>0) cout<<ans<<endl;
        else cout<<0<<endl;

    }
}


#include<bits/stdc++.h>

using namespace std;

#define fore(i, l, r) for(int i = int(l); i < int(r); i++)
#define sz(a) int((a).size())

#define x first
#define y second

typedef long long li;
typedef pair<int, int> pt;

const int MOD = 998244353;
int norm(int a) {
    while(a >= MOD) a -= MOD;
    while(a < 0) a += MOD;
    return a;
}
int mul(int a, int b) {
    return int(a * 1ll * b % MOD);
}

int n, k;
vector<int> a;

inline bool read() {
    if(!(cin >> n >> k))
        return false;
    a.resize(n);
    fore(i, 0, n)
        cin >> a[i];
    return true;
}

pair<int, int> calc(int len) {
    if(len == 0) return {0, 1};
    if(len & 1) {
        auto res = calc(len >> 1);
        return {norm(mul(res.x, res.x) + mul(k - 1, mul(res.y, res.y))),
                norm(mul(2, mul(res.x, res.y)) + mul(k - 2, mul(res.y, res.y)))};
    }
    auto res = calc(len - 1);
    return {mul(k - 1, res.y), norm(res.x + mul(k - 2, res.y))};
}

vector<int> curArray;

int calcSeg(int l, int r) {
    if(r >= sz(curArray)) {
        int len = r - l - 1, cf = 1;
        if(l < 0)
            len--, cf = k;
        if(len == 0) return cf;

        auto res = calc(len - 1);
        return mul(cf, norm(res.x + mul(k - 1, res.y)));
    }
    if(l < 0) {
        if(r - l == 1) return 1;
        auto res = calc(r - l - 2);
        return norm(res.x + mul(k - 1, res.y));
    }
    auto res = calc(r - l - 1);
    return curArray[l] == curArray[r] ? res.x : res.y;
}

inline void solve() {
    int ans = 1;
    fore(k, 0, 2) {
        curArray.clear();
        for(int i = 0; 2 * i + k < n; i++)
            curArray.push_back(a[2 * i + k]);

        int lst = -1;
        fore(i, 0, sz(curArray)){
            if(curArray[i] == -1) continue;
            ans = mul(ans, calcSeg(lst, i));
            lst = i;
        }
        ans = mul(ans, calcSeg(lst, sz(curArray)));
    }
    cout << ans << endl;
}

int main() {
#ifdef _DEBUG
    freopen("input.txt", "r", stdin);
    int tt = clock();
#endif
    ios_base::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    cout << fixed << setprecision(15);

    if(read()) {
        solve();

#ifdef _DEBUG
        cerr << "TIME = " << clock() - tt << endl;
        tt = clock();
#endif
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn=2e5+5;
const long long  mod = 998244353 ;
int d[maxn];

long long  dp[maxn];
int n,k;

long long power(long long  x,long long  y)
{
    long long ans=1;
    while(y)
    {
        if(y&1) ans=ans*x%mod;
        x=x*x%mod;
        y/=2;
    }
    return ans;
}

long long  run(vector<int > a)
{
    memset(dp,0,sizeof(dp));
    dp[0]=1;
    for(int i=1;i<a.size()-1;i++)
    {
        if(a[i]==a[i-1]) return  1e18;
        if(a[i]!=-1)
            dp[i]=dp[i-1];
        else
        {
            if(a[i-1]==-1 &&a[i+1] == -1 )
            {
                dp[i]=dp[i-1]*(k-1);
            }
            else if(a[i-1]==-1)
            {
                long long x=dp[i-1];
                long long pp=power(k,mod-2);

                if(a[i+1]<=k&&a[i+1]!=-1 ) dp[i]= (x*(k-1)*(k-1)%mod+mod )*pp%mod;
                else if(a[i+1]>k) dp[i]=dp[i-1]*(k-1);
              //  cout<<"sdf "<<' '<<dp[i]<<endl;
            }
            else if(a[i-1]<=k)
            {
                if(a[i+1]<=k&&a[i+1]!=-1 ) dp[i]=dp[i-1]*(k-2);
                else dp[i]=dp[i-1]*(k-1);
            }
            else if(a[i-1]>k)
            {
                if(a[i+1]>k|| a[i+1]==-1)dp[i]=dp[i-1]*(k);
                else dp[i]=dp[i-1]*(k-1);
            }

                dp[i]=(dp[i]+mod)%mod;
          //  cout<<"asd   "<<dp[i]<<' '<<dp[i-1]<<' '<<k<<' '<<i<< ' '<<a[i-1]<<endl;

            if(dp[i]==0) return -1e18;

        }
    }
  //  cout<<endl;
    return dp[a.size()-2];
}
vector<int > a,b;
int main()
{
    long long aa=200000;
 //  cout<< ( 4*4*4*4*4*3*4)%mod<<endl;
    while(cin>>n>>k)
    {
        a.push_back(1e9+7);
        b.push_back(1e9+7);
        for(int i=1;i<=n;i++)
        {
            cin>>d[i];
            if(i%2) a.push_back(d[i]);
            else b.push_back(d[i]);
        }
        a.push_back(1e9+7);
        b.push_back(1e9+7);
        long long ans=run(a)*run(b)%mod;
        if(ans>0) cout<<ans<<endl;
        else cout<<0<<endl;

    }
}


#include <bits/stdc++.h>
using namespace std;
const int maxn=2e5+5;
const long long  mod = 998244353 ;
int d[maxn];

long long  dp[maxn];
 int n,k;
long long  run(vector<int > a)
{
    memset(dp,0,sizeof(dp));
    dp[0]=1;
    for(int i=1;i<a.size()-1;i++)
    {
        if(a[i]!=-1)
            dp[i]=dp[i-1];
        else
        {
            int ans=0,ye=0;
            if(a[i-1]<=k) ans++;
            if(a[i+1]!=-1&&a[i+1]!=a[i-1]&&a[i+1]<=k) ye=1;
         //   cout<<"asd "<<ans<<endl;

            dp[i]=(dp[i-1]*(k-ans)-ye*(k-ans)+mod)%mod;
            if(dp[i]==0) return -1e18;

        }
    }
 //   cout<<endl;
    return dp[a.size()-2];
}
vector<int > a,b;
int main()
{
    long long aa=200000;
   // cout<< ( (aa-1)*aa%mod )*(aa-1)%mod<<endl;
    while(cin>>n>>k)
    {
        a.push_back(1e9+7);
        b.push_back(1e9+7);
        for(int i=1;i<=n;i++)
        {
            cin>>d[i];
            if(i%2) a.push_back(d[i]);
            else b.push_back(d[i]);
        }
        a.push_back(1e9+7);
        b.push_back(1e9+7);
        long long ans=run(a)*run(b)%mod;
        if(ans>0) cout<<ans<<endl;
        else cout<<0<<endl;

    }
}


#include <bits/stdc++.h>
using namespace std;
const int maxn=2e5+5;
const long long  mod = 998244353 ;
int d[maxn];

long long  dp[maxn];
 int n,k;
long long  run(vector<int > a)
{
    memset(dp,0,sizeof(dp));
    dp[0]=1;
    for(int i=1;i<a.size()-1;i++)
    {
        if(a[i]!=-1)
            dp[i]=dp[i-1];
        else
        {
            if(a[i-1]==-1 &&a[i+1] == -1 )
            {
                dp[i]=dp[i-1]*(k-1);
            }
            else if(a[i-1]==-1)
            {
                if(a[i+1]<=k) dp[i]=dp[i-1]*(k-1)-k+1;
                else if(a[i+1]>k) dp[i]=dp[i-1]*(k-1);
            }
            else if(a[i-1]<=k)
            {
                if(a[i+1]<=k) dp[i]=dp[i-1]*(k-2);
                else dp[i]=dp[i-1]*(k-1);
            }
            else if(a[i-1]>k)
            {
                if(a[i+1]>k)dp[i]=dp[i-1]*(k);
                else dp[i]=dp[i-1]*(k-1);
            }

                dp[i]=(dp[i]+mod)%mod;
           // cout<<"asd   "<<dp[i]<<' '<<dp[i-1]<<' '<<k<<' '<<i<< ' '<<a[i-1]<<endl;

            if(dp[i]==0) return -1e18;

        }
    }
  //  cout<<endl;
    return dp[a.size()-2];
}
vector<int > a,b;
int main()
{
    long long aa=200000;
   // cout<< ( (aa-1)*aa%mod )*(aa-1)%mod<<endl;
    while(cin>>n>>k)
    {
        a.push_back(1e9+7);
        b.push_back(1e9+7);
        for(int i=1;i<=n;i++)
        {
            cin>>d[i];
            if(i%2) a.push_back(d[i]);
            else b.push_back(d[i]);
        }
        a.push_back(1e9+7);
        b.push_back(1e9+7);
        long long ans=run(a)*run(b)%mod;
        if(ans>0) cout<<ans<<endl;
        else cout<<0<<endl;

    }
}


#include <bits/stdc++.h>
using namespace std;
const int maxn=2e5+5;
const long long  mod = 998244353 ;
int d[maxn],temp[maxn];

long long  dp[maxn];
int n,k ;

long long power(long long  x,long long  y)
{
    long long ans=1;
    while(y)
    {
        if(y&1) ans=ans*x%mod;
        x=x*x%mod;
        y/=2;
    }
    return ans;
}

long long  run(vector<int > a)
{
    memset(dp,0,sizeof(dp));
    dp[0]=1;
    for(int i=1;i<a.size()-1;i++)
    {
        if(a[i]==a[i-1]&&a[i]!=-1) return  -1e9;
        if(a[i]!=-1)
            dp[i]=dp[i-1];
        else
        {
            if(a[i-1]==-1 &&a[i+1] == -1 )
            {
                dp[i]=dp[i-1]*(k-1),temp[i]=k-1;
            }
            else if(a[i-1]==-1)
            {
                long long x=dp[i-1];
                long long ke=temp[i-2];
                long long pp=power(ke,mod-2);

                if(a[i+1]<=k&&a[i+1]!=-1 ) dp[i]= (x*pp%mod*(k-1)+(x*(ke-1)*pp%mod*(k-2)+mod  ))%mod;
                else  dp[i]=dp[i-1]*(k-1),temp[i]=k-1;
               // cout<<"sdf "<<' '<<dp[i]<<' '<<ke<<' '<<x<<endl;
            }
            else if(a[i-1]<=k)
            {
                if(a[i+1]<=k&&a[i+1]!=-1&&a[i+1]!=a[i-1] ) dp[i]=dp[i-1]*(k-2);
                else dp[i]=dp[i-1]*(k-1),temp[i]=k-1;
            }
            else if(a[i-1]>k)
            {
                if(a[i+1]>k|| a[i+1]==-1)dp[i]=dp[i-1]*(k);
                else dp[i]=dp[i-1]*(k-1);
            }

                dp[i]=(dp[i]+mod)%mod;
           // cout<<"asd   "<<dp[i]<<' '<<dp[i-1]<<' '<<k<<' '<<i<< ' '<<a[i-1]<<' '<<a[i+1]<<endl;

            if(dp[i]==0) return -1e9;

        }
    }
  //  cout<<endl;
    return dp[a.size()-2];
}
vector<int > a,b;
int main()
{
    long long aa=200000;
 //  cout<< ( 4*4*4*4*4*3*4)%mod<<endl;
    while(cin>>n>>k)
    {
        a.push_back(1e9+7);
        b.push_back(1e9+7);
        for(int i=1;i<=n;i++)
        {
            cin>>d[i];
            if(i%2) a.push_back(d[i]);
            else b.push_back(d[i]);
        }
        a.push_back(1e9+7);
        b.push_back(1e9+7);
        cout<<run(a)<< ' '<<run(b)<<endl;
        long long ans=run(a)*run(b)%mod;
        if(ans>0) cout<<ans<<endl;
        else cout<<0<<endl;

    }
}


#include <bits/stdc++.h>
using namespace std;
const int maxn=2e5+5;
const long long  mod = 998244353 ;
int d[maxn];

long long  dp[maxn];
int n,k;

long long power(long long  x,long long  y)
{
    long long ans=1;
    while(y)
    {
        if(y&1) ans=ans*x%mod;
        x=x*x%mod;
        y/=2;
    }
    return ans;
}

long long  run(vector<int > a)
{
    memset(dp,0,sizeof(dp));
    dp[0]=1;
    for(int i=1;i<a.size()-1;i++)
    {
        if(a[i]!=-1)
            dp[i]=dp[i-1];
        else
        {
            if(a[i-1]==-1 &&a[i+1] == -1 )
            {
                dp[i]=dp[i-1]*(k-1);
            }
            else if(a[i-1]==-1)
            {
                long long x=dp[i-1];
                long long pp=power(k,mod-2);

                if(a[i+1]<=k&&a[i+1]!=-1 ) dp[i]= (x*(k-1)*(k-1)%mod+mod )*pp%mod;
                else if(a[i+1]>k) dp[i]=dp[i-1]*(k-1);
              //  cout<<"sdf "<<' '<<dp[i]<<endl;
            }
            else if(a[i-1]<=k)
            {
                if(a[i+1]<=k&&a[i+1]!=-1 ) dp[i]=dp[i-1]*(k-2);
                else dp[i]=dp[i-1]*(k-1);
            }
            else if(a[i-1]>k)
            {
                if(a[i+1]>k|| a[i+1]==-1)dp[i]=dp[i-1]*(k);
                else dp[i]=dp[i-1]*(k-1);
            }

                dp[i]=(dp[i]+mod)%mod;
          //  cout<<"asd   "<<dp[i]<<' '<<dp[i-1]<<' '<<k<<' '<<i<< ' '<<a[i-1]<<endl;

            if(dp[i]==0) return -1e18;

        }
    }
  //  cout<<endl;
    return dp[a.size()-2];
}
vector<int > a,b;
int main()
{
    long long aa=200000;
 //  cout<< ( 4*4*4*4*4*3*4)%mod<<endl;
    while(cin>>n>>k)
    {
        a.push_back(1e9+7);
        b.push_back(1e9+7);
        for(int i=1;i<=n;i++)
        {
            cin>>d[i];
            if(i%2) a.push_back(d[i]);
            else b.push_back(d[i]);
        }
        a.push_back(1e9+7);
        b.push_back(1e9+7);
        long long ans=run(a)*run(b)%mod;
        if(ans>0) cout<<ans<<endl;
        else cout<<0<<endl;

    }
}


#include <bits/stdc++.h>
using namespace std;
const int maxn=2e5+5;
const long long  mod = 998244353 ;
int d[maxn],temp[maxn];

long long  dp[maxn];
int n,k ;

long long power(long long  x,long long  y)
{
    long long ans=1;
    while(y)
    {
        if(y&1) ans=ans*x%mod;
        x=x*x%mod;
        y/=2;
    }
    return ans;
}

long long  run(vector<int > a)
{
    memset(dp,0,sizeof(dp));
    dp[0]=1;
    for(int i=1;i<a.size()-1;i++)
    {
        if(a[i]==a[i-1]&&a[i]!=-1) return  -1e9;
        if(a[i]!=-1)
            dp[i]=dp[i-1];
        else
        {
            if(a[i-1]==-1 &&a[i+1] == -1 )
            {
                dp[i]=dp[i-1]*(k-1),temp[i]=k-1;
            }
            else if(a[i-1]==-1)
            {
                long long x=dp[i-1];
                long long ke=temp[i-2];
                long long pp=power(ke,mod-2);

                if(a[i+1]<=k&&a[i+1]!=-1 ) dp[i]= (x*pp%mod*(k-1)+(x*(ke-1)*pp%mod*(k-2)+mod  ))%mod;
                else  dp[i]=dp[i-1]*(k-1),temp[i]=k-1;
               // cout<<"sdf "<<' '<<dp[i]<<' '<<ke<<' '<<x<<endl;
            }
            else if(a[i-1]<=k)
            {
                if(a[i+1]<=k&&a[i+1]!=-1&&a[i+1]!=a[i-1] ) dp[i]=dp[i-1]*(k-2);
                else dp[i]=dp[i-1]*(k-1),temp[i]=k-1;
            }
            else if(a[i-1]>k)
            {
                if(a[i+1]>k|| a[i+1]==-1)dp[i]=dp[i-1]*(k);
                else dp[i]=dp[i-1]*(k-1);
            }

                dp[i]=(dp[i]+mod)%mod;
           // cout<<"asd   "<<dp[i]<<' '<<dp[i-1]<<' '<<k<<' '<<i<< ' '<<a[i-1]<<' '<<a[i+1]<<endl;

            if(dp[i]==0) return -1e9;

        }
    }
  //  cout<<endl;
    return dp[a.size()-2];
}
vector<int > a,b;
int main()
{
    long long aa=200000;
 //  cout<< ( 4*4*4*4*4*3*4)%mod<<endl;
    while(cin>>n>>k)
    {
        a.push_back(1e9+7);
        b.push_back(1e9+7);
        for(int i=1;i<=n;i++)
        {
            cin>>d[i];
            if(i%2) a.push_back(d[i]);
            else b.push_back(d[i]);
        }
        a.push_back(1e9+7);
        b.push_back(1e9+7);
       // cout<<run(a)<< ' '<<run(b)<<endl;
        long long ans=run(a)*run(b)%mod;
        if(ans>0) cout<<ans<<endl;
        else cout<<0<<endl;

    }
}


#include <bits/stdc++.h>
using namespace std;
const int maxn=2e5+5;
const long long  mod = 998244353 ;
int d[maxn];

long long  dp[maxn];
 int n,k;
long long  run(vector<int > a)
{
    memset(dp,0,sizeof(dp));
    dp[0]=1;
    for(int i=1;i<a.size()-1;i++)
    {
        if(a[i]!=-1)
            dp[i]=dp[i-1];
        else
        {
            if(a[i-1]==-1 &&a[i+1] == -1 )
            {
                dp[i]=dp[i-1]*(k-1);
            }
            else if(a[i-1]==-1)
            {
                if(a[i+1]<=k&&a[i+1]!=-1 ) dp[i]=dp[i-1]*(k-1)-k+1;
                else if(a[i+1]>k) dp[i]=dp[i-1]*(k-1);
            }
            else if(a[i-1]<=k)
            {
                if(a[i+1]<=k&&a[i+1]!=-1 ) dp[i]=dp[i-1]*(k-2);
                else dp[i]=dp[i-1]*(k-1);
            }
            else if(a[i-1]>k)
            {
                if(a[i+1]>k|| a[i+1]==-1)dp[i]=dp[i-1]*(k);
                else dp[i]=dp[i-1]*(k-1);
            }

                dp[i]=(dp[i]+mod)%mod;
         //   cout<<"asd   "<<dp[i]<<' '<<dp[i-1]<<' '<<k<<' '<<i<< ' '<<a[i-1]<<endl;

            if(dp[i]==0) return -1e18;

        }
    }
  //  cout<<endl;
    return dp[a.size()-2];
}
vector<int > a,b;
int main()
{
    long long aa=200000;
 //  cout<< ( 4*4*4*4*4*3*4)%mod<<endl;
    while(cin>>n>>k)
    {
        a.push_back(1e9+7);
        b.push_back(1e9+7);
        for(int i=1;i<=n;i++)
        {
            cin>>d[i];
            if(i%2) a.push_back(d[i]);
            else b.push_back(d[i]);
        }
        a.push_back(1e9+7);
        b.push_back(1e9+7);
        long long ans=run(a)*run(b)%mod;
        if(ans>0) cout<<ans<<endl;
        else cout<<0<<endl;

    }
}


#include <bits/stdc++.h>
using namespace std;
const int maxn=2e5+5;
const long long  mod = 998244353 ;
int d[maxn];

long long  dp[maxn];
int n,k;

long long power(long long  x,long long  y)
{
    long long ans=1;
    while(y)
    {
        if(y&1) ans=ans*x%mod;
        x=x*x%mod;
        y/=2;
    }
    return ans;
}

long long  run(vector<int > a)
{
    memset(dp,0,sizeof(dp));
    dp[0]=1;
    for(int i=1;i<a.size()-1;i++)
    {
        if(a[i]==a[i-1]&&a[i]!=-1) return  -1e9;
        if(a[i]!=-1)
            dp[i]=dp[i-1];
        else
        {
            if(a[i-1]==-1 &&a[i+1] == -1 )
            {
                dp[i]=dp[i-1]*(k-1);
            }
            else if(a[i-1]==-1)
            {
                long long x=dp[i-1];
                long long pp=power(k,mod-2);

                if(a[i+1]<=k&&a[i+1]!=-1 ) dp[i]= (x*(k-1)%mod*(k-1)%mod+mod )*pp%mod;
                else if(a[i+1]>k) dp[i]=dp[i-1]*(k-1);
              //  cout<<"sdf "<<' '<<dp[i]<<endl;
            }
            else if(a[i-1]<=k)
            {
                if(a[i+1]<=k&&a[i+1]!=-1&&a[i+1]!=a[i-1] ) dp[i]=dp[i-1]*(k-2);
                else dp[i]=dp[i-1]*(k-1);
            }
            else if(a[i-1]>k)
            {
                if(a[i+1]>k|| a[i+1]==-1)dp[i]=dp[i-1]*(k);
                else dp[i]=dp[i-1]*(k-1);
            }

                dp[i]=(dp[i]+mod)%mod;
          //  cout<<"asd   "<<dp[i]<<' '<<dp[i-1]<<' '<<k<<' '<<i<< ' '<<a[i-1]<<endl;

            if(dp[i]==0) return -1e9;

        }
    }
  //  cout<<endl;
    return dp[a.size()-2];
}
vector<int > a,b;
int main()
{
    long long aa=200000;
 //  cout<< ( 4*4*4*4*4*3*4)%mod<<endl;
    while(cin>>n>>k)
    {
        a.push_back(1e9+7);
        b.push_back(1e9+7);
        for(int i=1;i<=n;i++)
        {
            cin>>d[i];
            if(i%2) a.push_back(d[i]);
            else b.push_back(d[i]);
        }
        a.push_back(1e9+7);
        b.push_back(1e9+7);
        //cout<<run(a)<< ' '<<run(b)<<endl;
        long long ans=run(a)*run(b)%mod;
        if(ans>0) cout<<ans<<endl;
        else cout<<0<<endl;

    }
}


#include <bits/stdc++.h>
using namespace std;
const int maxn=2e5+5;
const long long  mod = 998244353 ;
int d[maxn];

long long  dp[maxn];
 int n,k;
long long  run(vector<int > a)
{
    memset(dp,0,sizeof(dp));
    dp[0]=1;
    for(int i=1;i<a.size()-1;i++)
    {
        if(a[i]!=-1)
            dp[i]=dp[i-1];
        else
        {
            if(a[i-1]==-1 &&a[i+1] == -1 )
            {
                dp[i]=dp[i-1]*(k-1);
            }
            else if(a[i-1]==-1)
            {
                if(a[i+1]<=k) dp[i]=dp[i-1]*(k-1)-k+1;
                else if(a[i+1]>k) dp[i]=dp[i-1]*(k-1);
            }
            else if(a[i-1]<=k)
            {
                if(a[i+1]<=k) dp[i]=dp[i-1]*(k-2);
                else dp[i]=dp[i-1]*(k-1);
            }
            else if(a[i-1]>k)
            {
                if(a[i+1]>k|| a[i+1]==-1)dp[i]=dp[i-1]*(k);
                else dp[i]=dp[i-1]*(k-1);
            }

                dp[i]=(dp[i]+mod)%mod;
         //   cout<<"asd   "<<dp[i]<<' '<<dp[i-1]<<' '<<k<<' '<<i<< ' '<<a[i-1]<<endl;

            if(dp[i]==0) return -1e18;

        }
    }
  //  cout<<endl;
    return dp[a.size()-2];
}
vector<int > a,b;
int main()
{
    long long aa=200000;
   // cout<< ( (aa-1)*aa%mod )*(aa-1)%mod<<endl;
    while(cin>>n>>k)
    {
        a.push_back(1e9+7);
        b.push_back(1e9+7);
        for(int i=1;i<=n;i++)
        {
            cin>>d[i];
            if(i%2) a.push_back(d[i]);
            else b.push_back(d[i]);
        }
        a.push_back(1e9+7);
        b.push_back(1e9+7);
        long long ans=run(a)*run(b)%mod;
        if(ans>0) cout<<ans<<endl;
        else cout<<0<<endl;

    }
}


#include <bits/stdc++.h>
using namespace std;
const int maxn=2e5+5;
const long long  mod = 998244353 ;
int d[maxn];

long long  dp[maxn];
 int n,k;
long long  run(vector<int > a)
{
    memset(dp,0,sizeof(dp));
    dp[0]=1;
    for(int i=1;i<a.size()-1;i++)
    {
        if(a[i]!=-1)
            dp[i]=dp[i-1];
        else
        {
            if(a[i-1]==-1 &&a[i+1] == -1 )
            {
                dp[i]=dp[i-1]*(k-1);
            }
            else if(a[i-1]==-1)
            {
                if(a[i+1]<=k) dp[i]=dp[i-1]*(k-1)-k+1;
                else if(a[i+1]>k) dp[i]=dp[i-1]*(k-1);
            }
            else if(a[i-1]<=k)
            {
                if(a[i+1]<=k) dp[i]=dp[i-1]*(k-2);
                else dp[i]=dp[i-1]*(k-1);
            }
            else if(a[i-1]>k)
                dp[i]=dp[i-1]*(k);
                dp[i]=(dp[i]+mod)%mod;
          //  cout<<"asd   "<<dp[i]<<' '<<dp[i-1]<<' '<<k<<' '<<i<< ' '<<a[i-1]<<endl;

            if(dp[i]==0) return -1e18;

        }
    }
   // cout<<endl;
    return dp[a.size()-2];
}
vector<int > a,b;
int main()
{
    long long aa=200000;
   // cout<< ( (aa-1)*aa%mod )*(aa-1)%mod<<endl;
    while(cin>>n>>k)
    {
        a.push_back(1e9+7);
        b.push_back(1e9+7);
        for(int i=1;i<=n;i++)
        {
            cin>>d[i];
            if(i%2) a.push_back(d[i]);
            else b.push_back(d[i]);
        }
        a.push_back(1e9+7);
        b.push_back(1e9+7);
        long long ans=run(a)*run(b)%mod;
        if(ans>0) cout<<ans<<endl;
        else cout<<0<<endl;

    }
}


#include <bits/stdc++.h>
using namespace std;
const int maxn=2e5+5;
const long long  mod = 998244353 ;
int d[maxn];

long long  dp[maxn];
int n,k;

long long power(long long  x,long long  y)
{
    long long ans=1;
    while(y)
    {
        if(y&1) ans=ans*x%mod;
        x=x*x%mod;
        y/=2;
    }
    return ans;
}

long long  run(vector<int > a)
{
    memset(dp,0,sizeof(dp));
    dp[0]=1;
    for(int i=1;i<a.size()-1;i++)
    {
        if(a[i]==a[i-1]&&a[i]!=-1) return  -1e9;
        if(a[i]!=-1)
            dp[i]=dp[i-1];
        else
        {
            if(a[i-1]==-1 &&a[i+1] == -1 )
            {
                dp[i]=dp[i-1]*(k-1);
            }
            else if(a[i-1]==-1)
            {
                long long x=dp[i-1];
                long long pp=power(k,mod-2);

                if(a[i+1]<=k&&a[i+1]!=-1 ) dp[i]= (x*(k-1)*(k-1)%mod+mod )*pp%mod;
                else if(a[i+1]>k) dp[i]=dp[i-1]*(k-1);
              //  cout<<"sdf "<<' '<<dp[i]<<endl;
            }
            else if(a[i-1]<=k)
            {
                if(a[i+1]<=k&&a[i+1]!=-1 ) dp[i]=dp[i-1]*(k-2);
                else dp[i]=dp[i-1]*(k-1);
            }
            else if(a[i-1]>k)
            {
                if(a[i+1]>k|| a[i+1]==-1)dp[i]=dp[i-1]*(k);
                else dp[i]=dp[i-1]*(k-1);
            }

                dp[i]=(dp[i]+mod)%mod;
          //  cout<<"asd   "<<dp[i]<<' '<<dp[i-1]<<' '<<k<<' '<<i<< ' '<<a[i-1]<<endl;

            if(dp[i]==0) return -1e9;

        }
    }
  //  cout<<endl;
    return dp[a.size()-2];
}
vector<int > a,b;
int main()
{
    long long aa=200000;
 //  cout<< ( 4*4*4*4*4*3*4)%mod<<endl;
    while(cin>>n>>k)
    {
        a.push_back(1e9+7);
        b.push_back(1e9+7);
        for(int i=1;i<=n;i++)
        {
            cin>>d[i];
            if(i%2) a.push_back(d[i]);
            else b.push_back(d[i]);
        }
        a.push_back(1e9+7);
        b.push_back(1e9+7);
        //cout<<run(a)<< ' '<<run(b)<<endl;
        long long ans=run(a)*run(b)%mod;
        if(ans>0) cout<<ans<<endl;
        else cout<<0<<endl;

    }
}


#include <bits/stdc++.h>
using namespace std;
const int maxn=2e5+5;
const long long  mod = 998244353 ;
int d[maxn];

long long  dp[maxn];
int n,k;

long long power(long long  x,long long  y)
{
    long long ans=1;
    while(y)
    {
        if(y&1) ans=ans*x%mod;
        x=x*x%mod;
        y/=2;
    }
    return ans;
}

long long  run(vector<int > a)
{
    memset(dp,0,sizeof(dp));
    dp[0]=1;
    for(int i=1;i<a.size()-1;i++)
    {
        if(a[i]==a[i-1]&&a[i]!=-1) return  -1e18;
        if(a[i]!=-1)
            dp[i]=dp[i-1];
        else
        {
            if(a[i-1]==-1 &&a[i+1] == -1 )
            {
                dp[i]=dp[i-1]*(k-1);
            }
            else if(a[i-1]==-1)
            {
                long long x=dp[i-1];
                long long pp=power(k,mod-2);

                if(a[i+1]<=k&&a[i+1]!=-1 ) dp[i]= (x*(k-1)*(k-1)%mod+mod )*pp%mod;
                else if(a[i+1]>k) dp[i]=dp[i-1]*(k-1);
              //  cout<<"sdf "<<' '<<dp[i]<<endl;
            }
            else if(a[i-1]<=k)
            {
                if(a[i+1]<=k&&a[i+1]!=-1 ) dp[i]=dp[i-1]*(k-2);
                else dp[i]=dp[i-1]*(k-1);
            }
            else if(a[i-1]>k)
            {
                if(a[i+1]>k|| a[i+1]==-1)dp[i]=dp[i-1]*(k);
                else dp[i]=dp[i-1]*(k-1);
            }

                dp[i]=(dp[i]+mod)%mod;
          //  cout<<"asd   "<<dp[i]<<' '<<dp[i-1]<<' '<<k<<' '<<i<< ' '<<a[i-1]<<endl;

            if(dp[i]==0) return -1e18;

        }
    }
  //  cout<<endl;
    return dp[a.size()-2];
}
vector<int > a,b;
int main()
{
    long long aa=200000;
 //  cout<< ( 4*4*4*4*4*3*4)%mod<<endl;
    while(cin>>n>>k)
    {
        a.push_back(1e9+7);
        b.push_back(1e9+7);
        for(int i=1;i<=n;i++)
        {
            cin>>d[i];
            if(i%2) a.push_back(d[i]);
            else b.push_back(d[i]);
        }
        a.push_back(1e9+7);
        b.push_back(1e9+7);
        long long ans=run(a)*run(b)%mod;
        if(ans>0) cout<<ans<<endl;
        else cout<<0<<endl;

    }
}


#include <bits/stdc++.h>
#define ll long long
#define MOD 998244353
#define CAP 100005
#define inf 10000000000000000LL
#define pii pair<ll,ll>
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define endl '\n'
#define sz(v) v.size()
#define all(v)    v.begin(),v.end()
#define fast  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define int ll
using namespace std;
ll power(ll x,ll y, ll md=MOD){ll res = 1;x%=md;while(y>0){if(y&1)res = (res*x)%md;x = (x*x)%md;y = y>>1;}return res%md;}
ll k;
map<pii,ll> map1;

ll numWays(ll cnt, ll same){
	if(map1.find(mp(cnt,same)) != map1.end()) return map1[mp(cnt,same)];
	if(cnt==0) return 1;
	if(cnt==1) {
		if(same) return k-1;
		else return k-2;
	}
	ll ans=0;
	if(same){
		ans=(k-1)*(((k-2)*numWays(cnt-2,0))%MOD);
		ans%=MOD;
		if(cnt!=2) ans+=(k-1)*numWays(cnt-2,1);
	} else {
		ans=(k-2)*(((k-3)*numWays(cnt-2,0))%MOD);
		ans%=MOD;
		ans+=2*(((k-2)*numWays(cnt-2,0))%MOD);
		ans%=MOD;
		ans+=numWays(cnt-2,0);
		ans%=MOD;
		if(cnt!=2) ans+=((k-2)*numWays(cnt-2,1))%MOD;
	}
	ans%=MOD;
	map1[mp(cnt,same)] = ans;
	return ans;
}

signed main(){
	fast;
	ll n,ans=1;
	cin>>n>>k;
	std::vector<ll> v1,v2;
	for(int i=0;i<n;i++){
		ll temp;
		cin>>temp;
		if(i%2==0) v1.push_back(temp);
		else v2.push_back(temp);
	}
	std::vector<pair<ll,pair<ll,ll>>> cnt1,cnt2;
	ll c=0;
	ll prev=0;
	for(int i=0;i<v1.size();i++){
		if(v1[i]==-1) c++;
		else {
			if(c) cnt1.push_back(make_pair(c,make_pair(prev,v1[i])));
			prev = v1[i];
			c=0;
		}
	}
	if(c) cnt1.push_back(make_pair(c,make_pair(prev,0)));
	c=0;
	prev=0;
	for(int i=0;i<v2.size();i++){
		if(v2[i]==-1) c++;
		else {
			if(c) cnt2.push_back(make_pair(c,make_pair(prev,v2[i])));
			prev = v2[i];
			c=0;
		}
	}
	if(c) cnt2.push_back(make_pair(c,make_pair(prev,0)));

	for(int i=0;i<cnt1.size();i++){
		ll count = cnt1[i].first;
		ll p = cnt1[i].second.first;
		ll nx = cnt1[i].second.second;
		if(count==0) continue;
		if(p==0&&nx==0){
			ans*=k;
			ans%=MOD;
			ans*=power(k-1,count-1);
		}
		else if(p==0||nx==0) ans*=power(k-1,count);
		else if(p==nx) ans*=numWays(count,1);
		else ans*=numWays(count,0);
		ans%=MOD;
	}
	for(int i=0;i<cnt2.size();i++){
		ll count = cnt2[i].first;
		ll p = cnt2[i].second.first;
		ll nx = cnt2[i].second.second;
		if(count==0) continue;
		if(p==0&&nx==0){
			ans*=k;
			ans%=MOD;
			ans*=power(k-1,count-1);
		}
		else if(p==0||nx==0) ans*=power(k-1,count);
		else if(p==nx) ans*=numWays(count,1);
		else ans*=numWays(count,0);
		ans%=MOD;
	}

	for(int i=1;i<v1.size();i++)
		if(v1[i]==v1[i-1]&&v1[i]!=-1) ans=0;
	for(int i=1;i<v2.size();i++)
		if(v2[i]==v2[i-1]&&v2[i]!=-1) ans=0;

	cout<<ans;
}


#include <bits/stdc++.h>
#define ll long long
#define MOD 998244353
#define CAP 100005
#define inf 10000000000000000LL
#define pii pair<ll,ll>
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define endl '\n'
#define sz(v) v.size()
#define all(v)    v.begin(),v.end()
#define fast  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//#define int ll
using namespace std;
ll power(ll x,ll y, ll md=MOD){ll res = 1;x%=md;while(y>0){if(y&1)res = (res*x)%md;x = (x*x)%md;y = y>>1;}return res%md;}
ll k;
ll numWays(ll cnt, ll same){
	if(cnt==0) return 1;
	if(cnt==1) {
		if(same) return k-1;
		else return k-2;
	}
	ll ans=0;
	if(same){
		ans=(k-1)*(k-2)*numWays(cnt-2,0)+(k-1)*numWays(cnt-2,1);
		ans%=MOD;
	} else {
		ans=(k-2)*(k-3)*numWays(cnt-2,0)+2*(k-2)*numWays(cnt-2,0)+numWays(cnt-2,0)+(k-2)*numWays(cnt-2,1);
		ans%=MOD;
	}
	return ans;
}

signed main(){
	fast;
	ll n,ans=1;
	cin>>n>>k;
	std::vector<ll> v1,v2;
	for(int i=0;i<n;i++){
		ll temp;
		cin>>temp;
		if(i%2==0) v1.push_back(temp);
		else v2.push_back(temp);
	}
	std::vector<pair<ll,pair<ll,ll>>> cnt1,cnt2;
	ll c=0;
	ll prev=0;
	for(int i=0;i<v1.size();i++){
		if(v1[i]==-1) c++;
		else {
			cnt1.push_back(make_pair(c,make_pair(prev,v1[i])));
			prev = v1[i];
			c=0;
		}
	}
	cnt1.push_back(make_pair(c,make_pair(prev,0)));
	c=0;
	prev=0;
	for(int i=0;i<v2.size();i++){
		if(v2[i]==-1) c++;
		else {
			cnt2.push_back(make_pair(c,make_pair(prev,v2[i])));
			prev = v2[i];
			c=0;
		}
	}
	cnt2.push_back(make_pair(c,make_pair(prev,0)));

	for(int i=0;i<cnt1.size();i++){
		ll count = cnt1[i].first;
		ll p = cnt1[i].second.first;
		ll nx = cnt1[i].second.second;
		if(count==0) continue;
		if(p==0&&nx==0) ans*=k*power(k-1,count-1);
		else if(p==0||nx==0) ans*=power(k-1,count);
		else if(p==nx) ans*=numWays(count,1);
		else ans*=numWays(count,0);
		ans%=MOD;
	}
	for(int i=0;i<cnt2.size();i++){
		ll count = cnt2[i].first;
		ll p = cnt2[i].second.first;
		ll nx = cnt2[i].second.second;
		if(count==0) continue;
		if(p==0&&nx==0) ans*=k*power(k-1,count-1);
		else if(p==0||nx==0) ans*=power(k-1,count);
		else if(p==nx) ans*=numWays(count,1);
		else ans*=numWays(count,0);
		ans%=MOD;
	}
	cout<<ans;
}


#include <bits/stdc++.h>
#define ll long long
#define MOD 998244353
#define CAP 100005
#define inf 10000000000000000LL
#define pii pair<ll,ll>
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define endl '\n'
#define sz(v) v.size()
#define all(v)    v.begin(),v.end()
#define fast  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//#define int ll
using namespace std;
ll power(ll x,ll y, ll md=MOD){ll res = 1;x%=md;while(y>0){if(y&1)res = (res*x)%md;x = (x*x)%md;y = y>>1;}return res%md;}
ll k;
ll numWays(ll cnt, ll same){
	if(cnt==0) return 1;
	if(cnt==1) {
		if(same) return k-1;
		else return k-2;
	}
	ll ans=0;
	if(same){
		ans=(k-1)*(((((k-2)*numWays(cnt-2,0))%MOD)+numWays(cnt-2,1))%MOD);
		ans%=MOD;
	} else {
		ans=((k-2)*(((max(k-3,0LL))*numWays(cnt-2,0))%MOD))%MOD
			+(2*(((k-2)*numWays(cnt-2,0))%MOD))%MOD+numWays(cnt-2,0)+((k-2)*numWays(cnt-2,1))%MOD;
		ans%=MOD;
	}
	return ans;
}

signed main(){
	fast;
	ll n,ans=1;
	cin>>n>>k;
	std::vector<ll> v1,v2;
	for(int i=0;i<n;i++){
		ll temp;
		cin>>temp;
		if(i%2==0) v1.push_back(temp);
		else v2.push_back(temp);
	}
	std::vector<pair<ll,pair<ll,ll>>> cnt1,cnt2;
	ll c=0;
	ll prev=0;
	for(int i=0;i<v1.size();i++){
		if(v1[i]==-1) c++;
		else {
			cnt1.push_back(make_pair(c,make_pair(prev,v1[i])));
			prev = v1[i];
			c=0;
		}
	}
	cnt1.push_back(make_pair(c,make_pair(prev,0)));
	c=0;
	prev=0;
	for(int i=0;i<v2.size();i++){
		if(v2[i]==-1) c++;
		else {
			cnt2.push_back(make_pair(c,make_pair(prev,v2[i])));
			prev = v2[i];
			c=0;
		}
	}
	cnt2.push_back(make_pair(c,make_pair(prev,0)));

	for(int i=0;i<cnt1.size();i++){
		ll count = cnt1[i].first;
		ll p = cnt1[i].second.first;
		ll nx = cnt1[i].second.second;
		if(count==0) continue;
		if(p==0&&nx==0){
			ans*=k;
			ans%=MOD;
			ans*=power(k-1,count-1);
		}
		else if(p==0||nx==0) ans*=power(k-1,count);
		else if(p==nx) ans*=numWays(count,1);
		else ans*=numWays(count,0);
		ans%=MOD;
	}
	for(int i=0;i<cnt2.size();i++){
		ll count = cnt2[i].first;
		ll p = cnt2[i].second.first;
		ll nx = cnt2[i].second.second;
		if(count==0) continue;
		if(p==0&&nx==0){
			ans*=k;
			ans%=MOD;
			ans*=power(k-1,count-1);
		}
		else if(p==0||nx==0) ans*=power(k-1,count);
		else if(p==nx) ans*=numWays(count,1);
		else ans*=numWays(count,0);
		ans%=MOD;
	}

	for(int i=1;i<v1.size();i++)
		if(v1[i]==v1[i-1]&&v1[i]!=-1) ans=0;
	for(int i=1;i<v2.size();i++)
		if(v2[i]==v2[i-1]&&v2[i]!=-1) ans=0;

	cout<<ans;
}


#include <bits/stdc++.h>
#define ll long long
#define MOD 998244353
#define CAP 100005
#define inf 10000000000000000LL
#define pii pair<ll,ll>
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define endl '\n'
#define sz(v) v.size()
#define all(v)    v.begin(),v.end()
#define fast  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define int ll
using namespace std;
ll power(ll x,ll y, ll md=MOD){ll res = 1;x%=md;while(y>0){if(y&1)res = (res*x)%md;x = (x*x)%md;y = y>>1;}return res%md;}
ll k;

ll numWays(ll cnt, ll same){
	if(cnt==0) return 1;
	if(cnt==1) {
		if(same) return k-1;
		else return k-2;
	}
	ll ans=0;
	if(same){
		ans=(k-1)*(((k-2)*numWays(cnt-2,0))%MOD);
		ans%=MOD;
		if(cnt!=2) ans+=(k-1)*numWays(cnt-2,1);
	} else {
		ans=(k-2)*(((k-3)*numWays(cnt-2,0))%MOD);
		ans%=MOD;
		ans+=2*(((k-2)*numWays(cnt-2,0))%MOD);
		ans%=MOD;
		ans+=numWays(cnt-2,0);
		ans%=MOD;
		if(cnt!=2) ans+=((k-2)*numWays(cnt-2,1))%MOD;
	}
	return ans%MOD;
}

signed main(){
	fast;
	ll n,ans=1;
	cin>>n>>k;
	std::vector<ll> v1,v2;
	for(int i=0;i<n;i++){
		ll temp;
		cin>>temp;
		if(i%2==0) v1.push_back(temp);
		else v2.push_back(temp);
	}
	std::vector<pair<ll,pair<ll,ll>>> cnt1,cnt2;
	ll c=0;
	ll prev=0;
	for(int i=0;i<v1.size();i++){
		if(v1[i]==-1) c++;
		else {
			if(c) cnt1.push_back(make_pair(c,make_pair(prev,v1[i])));
			prev = v1[i];
			c=0;
		}
	}
	if(c) cnt1.push_back(make_pair(c,make_pair(prev,0)));
	c=0;
	prev=0;
	for(int i=0;i<v2.size();i++){
		if(v2[i]==-1) c++;
		else {
			if(c) cnt2.push_back(make_pair(c,make_pair(prev,v2[i])));
			prev = v2[i];
			c=0;
		}
	}
	if(c) cnt2.push_back(make_pair(c,make_pair(prev,0)));

	for(int i=0;i<cnt1.size();i++){
		ll count = cnt1[i].first;
		ll p = cnt1[i].second.first;
		ll nx = cnt1[i].second.second;
		if(count==0) continue;
		if(p==0&&nx==0){
			ans*=k;
			ans%=MOD;
			ans*=power(k-1,count-1);
		}
		else if(p==0||nx==0) ans*=power(k-1,count);
		else if(p==nx) ans*=numWays(count,1);
		else ans*=numWays(count,0);
		ans%=MOD;
	}
	for(int i=0;i<cnt2.size();i++){
		ll count = cnt2[i].first;
		ll p = cnt2[i].second.first;
		ll nx = cnt2[i].second.second;
		if(count==0) continue;
		if(p==0&&nx==0){
			ans*=k;
			ans%=MOD;
			ans*=power(k-1,count-1);
		}
		else if(p==0||nx==0) ans*=power(k-1,count);
		else if(p==nx) ans*=numWays(count,1);
		else ans*=numWays(count,0);
		ans%=MOD;
	}

	for(int i=1;i<v1.size();i++)
		if(v1[i]==v1[i-1]&&v1[i]!=-1) ans=0;
	for(int i=1;i<v2.size();i++)
		if(v2[i]==v2[i-1]&&v2[i]!=-1) ans=0;

	cout<<ans;
}


#include <bits/stdc++.h>
#define ll long long
#define MOD 998244353
#define CAP 100005
#define inf 10000000000000000LL
#define pii pair<ll,ll>
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define endl '\n'
#define sz(v) v.size()
#define all(v)    v.begin(),v.end()
#define fast  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//#define int ll
using namespace std;
ll power(ll x,ll y, ll md=MOD){ll res = 1;x%=md;while(y>0){if(y&1)res = (res*x)%md;x = (x*x)%md;y = y>>1;}return res%md;}
ll k;
ll numWays(ll cnt, ll same){
	if(cnt==0) return 1;
	if(cnt==1) {
		if(same) return k-1;
		else return k-2;
	}
	ll ans=0;
	if(same){
		ans=(k-1)*(k-2)*numWays(cnt-2,0)+(k-1)*numWays(cnt-2,1);
		ans%=MOD;
	} else {
		ans=(k-2)*(k-3)*numWays(cnt-2,0)+2*(k-2)*numWays(cnt-2,0)+numWays(cnt-2,0)+(k-2)*numWays(cnt-2,1);
		ans%=MOD;
	}
	return ans;
}

signed main(){
	fast;
	ll n,ans=1;
	cin>>n>>k;
	std::vector<ll> v1,v2;
	for(int i=0;i<n;i++){
		ll temp;
		cin>>temp;
		if(i%2==0) v1.push_back(temp);
		else v2.push_back(temp);
	}
	std::vector<pair<ll,pair<ll,ll>>> cnt1,cnt2;
	ll c=0;
	ll prev=0;
	for(int i=0;i<v1.size();i++){
		if(v1[i]==-1) c++;
		else {
			cnt1.push_back(make_pair(c,make_pair(prev,v1[i])));
			prev = v1[i];
			c=0;
		}
	}
	cnt1.push_back(make_pair(c,make_pair(prev,0)));
	c=0;
	prev=0;
	for(int i=0;i<v2.size();i++){
		if(v2[i]==-1) c++;
		else {
			cnt2.push_back(make_pair(c,make_pair(prev,v2[i])));
			prev = v2[i];
			c=0;
		}
	}
	cnt2.push_back(make_pair(c,make_pair(prev,0)));

	for(int i=0;i<cnt1.size();i++){
		ll count = cnt1[i].first;
		ll p = cnt1[i].second.first;
		ll nx = cnt1[i].second.second;
		if(p==0&&nx==0) ans*=power(k,count);
		else if(p==0||nx==0) ans*=power(k-1,count);
		else if(p==nx) ans*=numWays(count,1);
		else ans*=numWays(count,0);
		ans%=MOD;
	}
	for(int i=0;i<cnt2.size();i++){
		ll count = cnt2[i].first;
		ll p = cnt2[i].second.first;
		ll nx = cnt2[i].second.second;
		if(p==0&&nx==0) ans*=power(k,count);
		else if(p==0||nx==0) ans*=power(k-1,count);
		else if(p==nx) ans*=numWays(count,1);
		else ans*=numWays(count,0);
		ans%=MOD;
	}
	cout<<ans;
}


#include <bits/stdc++.h>
#define ll long long
#define MOD 998244353
#define CAP 100005
#define inf 10000000000000000LL
#define pii pair<ll,ll>
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define endl '\n'
#define sz(v) v.size()
#define all(v)    v.begin(),v.end()
#define fast  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define int ll
using namespace std;
ll power(ll x,ll y, ll md=MOD){ll res = 1;x%=md;while(y>0){if(y&1)res = (res*x)%md;x = (x*x)%md;y = y>>1;}return res%md;}
ll k;
ll numWays(ll cnt, ll same){
	if(cnt==0) return 1;
	if(cnt==1) {
		if(same) return k-1;
		else return k-2;
	}
	ll ans=0;
	if(same){
		ans=2*(k-1)*(((k-2)*numWays(cnt-2,0))%MOD);
		ans%=MOD;
		ans+=numWays(cnt-2,1);
	} else {
		ans=2*(k-2)*(((k-3)*numWays(cnt-2,0))%MOD);
		ans%=MOD;
		ans+=2*(((k-2)*numWays(cnt-2,0))%MOD);
		ans%=MOD;
		ans+=numWays(cnt-2,0);
		ans%=MOD;
		ans+=((k-2)*numWays(cnt-2,1))%MOD;
	}
	return ans%MOD;
}

signed main(){
	fast;
	ll n,ans=1;
	cin>>n>>k;
	std::vector<ll> v1,v2;
	for(int i=0;i<n;i++){
		ll temp;
		cin>>temp;
		if(i%2==0) v1.push_back(temp);
		else v2.push_back(temp);
	}
	std::vector<pair<ll,pair<ll,ll>>> cnt1,cnt2;
	ll c=0;
	ll prev=0;
	for(int i=0;i<v1.size();i++){
		if(v1[i]==-1) c++;
		else {
			cnt1.push_back(make_pair(c,make_pair(prev,v1[i])));
			prev = v1[i];
			c=0;
		}
	}
	cnt1.push_back(make_pair(c,make_pair(prev,0)));
	c=0;
	prev=0;
	for(int i=0;i<v2.size();i++){
		if(v2[i]==-1) c++;
		else {
			cnt2.push_back(make_pair(c,make_pair(prev,v2[i])));
			prev = v2[i];
			c=0;
		}
	}
	cnt2.push_back(make_pair(c,make_pair(prev,0)));

	for(int i=0;i<cnt1.size();i++){
		ll count = cnt1[i].first;
		ll p = cnt1[i].second.first;
		ll nx = cnt1[i].second.second;
		if(count==0) continue;
		if(p==0&&nx==0){
			ans*=k;
			ans%=MOD;
			ans*=power(k-1,count-1);
		}
		else if(p==0||nx==0) ans*=power(k-1,count);
		else if(p==nx) ans*=numWays(count,1);
		else ans*=numWays(count,0);
		ans%=MOD;
	}
	for(int i=0;i<cnt2.size();i++){
		ll count = cnt2[i].first;
		ll p = cnt2[i].second.first;
		ll nx = cnt2[i].second.second;
		if(count==0) continue;
		if(p==0&&nx==0){
			ans*=k;
			ans%=MOD;
			ans*=power(k-1,count-1);
		}
		else if(p==0||nx==0) ans*=power(k-1,count);
		else if(p==nx) ans*=numWays(count,1);
		else ans*=numWays(count,0);
		ans%=MOD;
	}

	for(int i=1;i<v1.size();i++)
		if(v1[i]==v1[i-1]&&v1[i]!=-1) ans=0;
	for(int i=1;i<v2.size();i++)
		if(v2[i]==v2[i-1]&&v2[i]!=-1) ans=0;

	cout<<ans;
}


#include <bits/stdc++.h>
#define ll long long
#define MOD 998244353
#define CAP 100005
#define inf 10000000000000000LL
#define pii pair<ll,ll>
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define endl '\n'
#define sz(v) v.size()
#define all(v)    v.begin(),v.end()
#define fast  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define int ll
using namespace std;
ll power(ll x,ll y, ll md=MOD){ll res = 1;x%=md;while(y>0){if(y&1)res = (res*x)%md;x = (x*x)%md;y = y>>1;}return res%md;}
ll k;
map<pii,ll> map1;

ll numWays(ll cnt, ll same){
	if(map1.find(mp(cnt,same)) != map1.end()) return map1[mp(cnt,same)];
	if(cnt==0) return 1;
	if(cnt==1) {
		if(same) return k-1;
		else return k-2;
	}
	ll ans=0;
	if(same){
		ans=(k-1)*(((k-1)*numWays(cnt-2,0))%MOD);
		ans%=MOD;
		if(cnt!=2) ans+=(k-1)*numWays(cnt-2,1);
	} else {
		ans=(k-2)*(((k-3)*numWays(cnt-2,0))%MOD);
		ans%=MOD;
		ans+=2*(((k-2)*numWays(cnt-2,0))%MOD);
		ans%=MOD;
		ans+=numWays(cnt-2,0);
		ans%=MOD;
		if(cnt!=2) ans+=((k-2)*numWays(cnt-2,1))%MOD;
	}
	ans%=MOD;
	map1[mp(cnt,same)] = ans;
	return ans;
}

signed main(){
	fast;
	ll n,ans=1;
	cin>>n>>k;
	std::vector<ll> v1,v2;
	for(int i=0;i<n;i++){
		ll temp;
		cin>>temp;
		if(i%2==0) v1.push_back(temp);
		else v2.push_back(temp);
	}
	std::vector<pair<ll,pair<ll,ll>>> cnt1,cnt2;
	ll c=0;
	ll prev=0;
	for(int i=0;i<v1.size();i++){
		if(v1[i]==-1) c++;
		else {
			if(c) cnt1.push_back(make_pair(c,make_pair(prev,v1[i])));
			prev = v1[i];
			c=0;
		}
	}
	if(c) cnt1.push_back(make_pair(c,make_pair(prev,0)));
	c=0;
	prev=0;
	for(int i=0;i<v2.size();i++){
		if(v2[i]==-1) c++;
		else {
			if(c) cnt2.push_back(make_pair(c,make_pair(prev,v2[i])));
			prev = v2[i];
			c=0;
		}
	}
	if(c) cnt2.push_back(make_pair(c,make_pair(prev,0)));

	for(int i=0;i<cnt1.size();i++){
		ll count = cnt1[i].first;
		ll p = cnt1[i].second.first;
		ll nx = cnt1[i].second.second;
		if(count==0) continue;
		if(p==0&&nx==0){
			ans*=k;
			ans%=MOD;
			ans*=power(k-1,count-1);
		}
		else if(p==0||nx==0) ans*=power(k-1,count);
		else if(p==nx) ans*=numWays(count,1);
		else ans*=numWays(count,0);
		ans%=MOD;
	}
	for(int i=0;i<cnt2.size();i++){
		ll count = cnt2[i].first;
		ll p = cnt2[i].second.first;
		ll nx = cnt2[i].second.second;
		if(count==0) continue;
		if(p==0&&nx==0){
			ans*=k;
			ans%=MOD;
			ans*=power(k-1,count-1);
		}
		else if(p==0||nx==0) ans*=power(k-1,count);
		else if(p==nx) ans*=numWays(count,1);
		else ans*=numWays(count,0);
		ans%=MOD;
	}

	for(int i=1;i<v1.size();i++)
		if(v1[i]==v1[i-1]&&v1[i]!=-1) ans=0;
	for(int i=1;i<v2.size();i++)
		if(v2[i]==v2[i-1]&&v2[i]!=-1) ans=0;

	cout<<ans;
}


#include <bits/stdc++.h>
#define ll long long
#define MOD 998244353
#define CAP 100005
#define inf 10000000000000000LL
#define pii pair<ll,ll>
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define endl '\n'
#define sz(v) v.size()
#define all(v)    v.begin(),v.end()
#define fast  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//#define int ll
using namespace std;
ll power(ll x,ll y, ll md=MOD){ll res = 1;x%=md;while(y>0){if(y&1)res = (res*x)%md;x = (x*x)%md;y = y>>1;}return res%md;}
ll k;
ll numWays(ll cnt, ll same){
	if(cnt==0) return 1;
	if(cnt==1) {
		if(same) return k-1;
		else return k-2;
	}
	ll ans=0;
	if(same){
		ans=(k-1)*(k-2)*numWays(cnt-2,0)+(k-1)*numWays(cnt-2,1);
		ans%=MOD;
	} else {
		ans=(k-2)*(k-3)*numWays(cnt-2,0)+2*(k-2)*numWays(cnt-2,0)+numWays(cnt-2,0)+(k-2)*numWays(cnt-2,1);
		ans%=MOD;
	}
	return ans;
}

signed main(){
	fast;
	ll n,ans=1;
	cin>>n>>k;
	std::vector<ll> v1,v2;
	for(int i=0;i<n;i++){
		ll temp;
		cin>>temp;
		if(i%2==0) v1.push_back(temp);
		else v2.push_back(temp);
	}
	std::vector<pair<ll,pair<ll,ll>>> cnt1,cnt2;
	ll c=0;
	ll prev=0;
	for(int i=0;i<v1.size();i++){
		if(v1[i]==-1) c++;
		else {
			cnt1.push_back(make_pair(c,make_pair(prev,v1[i])));
			prev = v1[i];
			c=0;
		}
	}
	cnt1.push_back(make_pair(c,make_pair(prev,0)));
	c=0;
	prev=0;
	for(int i=0;i<v2.size();i++){
		if(v2[i]==-1) c++;
		else {
			cnt2.push_back(make_pair(c,make_pair(prev,v2[i])));
			prev = v2[i];
			c=0;
		}
	}
	cnt2.push_back(make_pair(c,make_pair(prev,0)));

	for(int i=0;i<cnt1.size();i++){
		ll count = cnt1[i].first;
		ll p = cnt1[i].second.first;
		ll nx = cnt1[i].second.second;
		if(count==0) continue;
		if(p==0&&nx==0) ans*=k*power(k-1,count-1);
		else if(p==0||nx==0) ans*=power(k-1,count);
		else if(p==nx) ans*=numWays(count,1);
		else ans*=numWays(count,0);
		ans%=MOD;
	}
	for(int i=0;i<cnt2.size();i++){
		ll count = cnt2[i].first;
		ll p = cnt2[i].second.first;
		ll nx = cnt2[i].second.second;
		if(count==0) continue;
		if(p==0&&nx==0) ans*=k*power(k-1,count-1);
		else if(p==0||nx==0) ans*=power(k-1,count);
		else if(p==nx) ans*=numWays(count,1);
		else ans*=numWays(count,0);
		ans%=MOD;
	}

	for(int i=1;i<v1.size();i++)
		if(v1[i]==v1[i-1]&&v1[i]!=-1) ans=0;
	for(int i=1;i<v2.size();i++)
		if(v2[i]==v2[i-1]&&v2[i]!=-1) ans=0;

	cout<<ans;
}


#include <bits/stdc++.h>
#define ll long long
#define MOD 998244353
#define CAP 100005
#define inf 10000000000000000LL
#define pii pair<ll,ll>
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define endl '\n'
#define sz(v) v.size()
#define all(v)    v.begin(),v.end()
#define fast  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define int ll
using namespace std;
ll power(ll x,ll y, ll md=MOD){ll res = 1;x%=md;while(y>0){if(y&1)res = (res*x)%md;x = (x*x)%md;y = y>>1;}return res%md;}
ll k;
unordered_map<ll,ll> map1;

ll numWays(ll cnt, ll same){
	if(map1.find(cnt+same*200001) != map1.end()) return map1[cnt+same*200001];
	if(cnt==0) return 1;
	if(cnt==1) {
		if(same) return k-1;
		else return k-2;
	}
	ll ans=0;
	if(same){
		ans=(k-1)*(((k-1)*numWays(cnt-2,0))%MOD);
		ans%=MOD;
		if(cnt!=2) ans+=(k-1)*numWays(cnt-2,1);
	} else {
		ans=(k-2)*(((k-3)*numWays(cnt-2,0))%MOD);
		ans%=MOD;
		ans+=2*(((k-2)*numWays(cnt-2,0))%MOD);
		ans%=MOD;
		ans+=numWays(cnt-2,0);
		ans%=MOD;
		if(cnt!=2) ans+=((k-2)*numWays(cnt-2,1))%MOD;
	}
	ans%=MOD;
	map1[cnt+same*200001] = ans;
	return ans;
}

signed main(){
	fast;
	ll n,ans=1;
	cin>>n>>k;
	std::vector<ll> v1,v2;
	for(int i=0;i<n;i++){
		ll temp;
		cin>>temp;
		if(i%2==0) v1.push_back(temp);
		else v2.push_back(temp);
	}
	std::vector<pair<ll,pair<ll,ll>>> cnt1,cnt2;
	ll c=0;
	ll prev=0;
	for(int i=0;i<v1.size();i++){
		if(v1[i]==-1) c++;
		else {
			if(c) cnt1.push_back(make_pair(c,make_pair(prev,v1[i])));
			prev = v1[i];
			c=0;
		}
	}
	if(c) cnt1.push_back(make_pair(c,make_pair(prev,0)));
	c=0;
	prev=0;
	for(int i=0;i<v2.size();i++){
		if(v2[i]==-1) c++;
		else {
			if(c) cnt2.push_back(make_pair(c,make_pair(prev,v2[i])));
			prev = v2[i];
			c=0;
		}
	}
	if(c) cnt2.push_back(make_pair(c,make_pair(prev,0)));

	for(int i=0;i<cnt1.size();i++){
		ll count = cnt1[i].first;
		ll p = cnt1[i].second.first;
		ll nx = cnt1[i].second.second;
		if(count==0) continue;
		if(p==0&&nx==0){
			ans*=k;
			ans%=MOD;
			ans*=power(k-1,count-1);
		}
		else if(p==0||nx==0) ans*=power(k-1,count);
		else if(p==nx) ans*=numWays(count,1);
		else ans*=numWays(count,0);
		ans%=MOD;
	}
	for(int i=0;i<cnt2.size();i++){
		ll count = cnt2[i].first;
		ll p = cnt2[i].second.first;
		ll nx = cnt2[i].second.second;
		if(count==0) continue;
		if(p==0&&nx==0){
			ans*=k;
			ans%=MOD;
			ans*=power(k-1,count-1);
		}
		else if(p==0||nx==0) ans*=power(k-1,count);
		else if(p==nx) ans*=numWays(count,1);
		else ans*=numWays(count,0);
		ans%=MOD;
	}

	for(int i=1;i<v1.size();i++)
		if(v1[i]==v1[i-1]&&v1[i]!=-1) ans=0;
	for(int i=1;i<v2.size();i++)
		if(v2[i]==v2[i-1]&&v2[i]!=-1) ans=0;

	cout<<ans;
}


#include <bits/stdc++.h>
#define ll long long
#define MOD 998244353
#define CAP 100005
#define inf 10000000000000000LL
#define pii pair<ll,ll>
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define endl '\n'
#define sz(v) v.size()
#define all(v)    v.begin(),v.end()
#define fast  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//#define int ll
using namespace std;
ll power(ll x,ll y, ll md=MOD){ll res = 1;x%=md;while(y>0){if(y&1)res = (res*x)%md;x = (x*x)%md;y = y>>1;}return res%md;}
ll k;
ll numWays(ll cnt, ll same){
	if(cnt==0) return 1;
	if(cnt==1) {
		if(same) return k-1;
		else return k-2;
	}
	ll ans=0;
	if(same){
		ans=(k-1)*(((((k-2)*numWays(cnt-2,0))%MOD)+numWays(cnt-2,1))%MOD);
		ans%=MOD;
	} else {
		ans=((k-2)*(((k-3)*numWays(cnt-2,0))%MOD))%MOD
			+(2*(((k-2)*numWays(cnt-2,0))%MOD))%MOD+numWays(cnt-2,0)+((k-2)*numWays(cnt-2,1))%MOD;
		ans%=MOD;
	}
	return ans;
}

signed main(){
	fast;
	ll n,ans=1;
	cin>>n>>k;
	std::vector<ll> v1,v2;
	for(int i=0;i<n;i++){
		ll temp;
		cin>>temp;
		if(i%2==0) v1.push_back(temp);
		else v2.push_back(temp);
	}
	std::vector<pair<ll,pair<ll,ll>>> cnt1,cnt2;
	ll c=0;
	ll prev=0;
	for(int i=0;i<v1.size();i++){
		if(v1[i]==-1) c++;
		else {
			cnt1.push_back(make_pair(c,make_pair(prev,v1[i])));
			prev = v1[i];
			c=0;
		}
	}
	cnt1.push_back(make_pair(c,make_pair(prev,0)));
	c=0;
	prev=0;
	for(int i=0;i<v2.size();i++){
		if(v2[i]==-1) c++;
		else {
			cnt2.push_back(make_pair(c,make_pair(prev,v2[i])));
			prev = v2[i];
			c=0;
		}
	}
	cnt2.push_back(make_pair(c,make_pair(prev,0)));

	for(int i=0;i<cnt1.size();i++){
		ll count = cnt1[i].first;
		ll p = cnt1[i].second.first;
		ll nx = cnt1[i].second.second;
		if(count==0) continue;
		if(p==0&&nx==0){
			ans*=k;
			ans%=MOD;
			ans*=power(k-1,count-1);
		}
		else if(p==0||nx==0) ans*=power(k-1,count);
		else if(p==nx) ans*=numWays(count,1);
		else ans*=numWays(count,0);
		ans%=MOD;
	}
	for(int i=0;i<cnt2.size();i++){
		ll count = cnt2[i].first;
		ll p = cnt2[i].second.first;
		ll nx = cnt2[i].second.second;
		if(count==0) continue;
		if(p==0&&nx==0){
			ans*=k;
			ans%=MOD;
			ans*=power(k-1,count-1);
		}
		else if(p==0||nx==0) ans*=power(k-1,count);
		else if(p==nx) ans*=numWays(count,1);
		else ans*=numWays(count,0);
		ans%=MOD;
	}

	for(int i=1;i<v1.size();i++)
		if(v1[i]==v1[i-1]&&v1[i]!=-1) ans=0;
	for(int i=1;i<v2.size();i++)
		if(v2[i]==v2[i-1]&&v2[i]!=-1) ans=0;

	cout<<ans;
}


#include <bits/stdc++.h>
#define ll long long
#define MOD 998244353
#define CAP 100005
#define inf 10000000000000000LL
#define pii pair<ll,ll>
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define endl '\n'
#define sz(v) v.size()
#define all(v)    v.begin(),v.end()
#define fast  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define int ll
using namespace std;
//ll power(ll x,ll y, ll md=MOD){ll res = 1;x%=md;while(y>0){if(y&1)res = (res*x)%md;x = (x*x)%md;y = y>>1;}return res%md;}
ll k;

ll power(ll x, ll y, ll md=MOD){
	ll ans=1;
	for(int i=0;i<y;i++){
		ans*=x;
		ans%=md;
	}
	return ans;
}

ll numWays(ll cnt, ll same){
	if(cnt==0) return 1;
	if(cnt==1) {
		if(same) return k-1;
		else return k-2;
	}
	ll ans=0;
	if(same){
		ans=(k-1)*(((k-2)*numWays(cnt-2,0))%MOD);
		ans%=MOD;
		if(cnt!=2) ans+=(k-1)*numWays(cnt-2,1);
	} else {
		ans=(k-2)*(((k-3)*numWays(cnt-2,0))%MOD);
		ans%=MOD;
		ans+=2*(((k-2)*numWays(cnt-2,0))%MOD);
		ans%=MOD;
		ans+=numWays(cnt-2,0);
		ans%=MOD;
		if(cnt!=2) ans+=((k-2)*numWays(cnt-2,1))%MOD;
	}
	return ans%MOD;
}

signed main(){
	fast;
	ll n,ans=1;
	cin>>n>>k;
	std::vector<ll> v1,v2;
	for(int i=0;i<n;i++){
		ll temp;
		cin>>temp;
		if(i%2==0) v1.push_back(temp);
		else v2.push_back(temp);
	}
	std::vector<pair<ll,pair<ll,ll>>> cnt1,cnt2;
	ll c=0;
	ll prev=0;
	for(int i=0;i<v1.size();i++){
		if(v1[i]==-1) c++;
		else {
			if(c) cnt1.push_back(make_pair(c,make_pair(prev,v1[i])));
			prev = v1[i];
			c=0;
		}
	}
	if(c) cnt1.push_back(make_pair(c,make_pair(prev,0)));
	c=0;
	prev=0;
	for(int i=0;i<v2.size();i++){
		if(v2[i]==-1) c++;
		else {
			if(c) cnt2.push_back(make_pair(c,make_pair(prev,v2[i])));
			prev = v2[i];
			c=0;
		}
	}
	if(c) cnt2.push_back(make_pair(c,make_pair(prev,0)));

	for(int i=0;i<cnt1.size();i++){
		ll count = cnt1[i].first;
		ll p = cnt1[i].second.first;
		ll nx = cnt1[i].second.second;
		if(count==0) continue;
		if(p==0&&nx==0){
			ans*=k;
			ans%=MOD;
			ans*=power(k-1,count-1);
		}
		else if(p==0||nx==0) ans*=power(k-1,count);
		else if(p==nx) ans*=numWays(count,1);
		else ans*=numWays(count,0);
		ans%=MOD;
	}
	for(int i=0;i<cnt2.size();i++){
		ll count = cnt2[i].first;
		ll p = cnt2[i].second.first;
		ll nx = cnt2[i].second.second;
		if(count==0) continue;
		if(p==0&&nx==0){
			ans*=k;
			ans%=MOD;
			ans*=power(k-1,count-1);
		}
		else if(p==0||nx==0) ans*=power(k-1,count);
		else if(p==nx) ans*=numWays(count,1);
		else ans*=numWays(count,0);
		ans%=MOD;
	}

	for(int i=1;i<v1.size();i++)
		if(v1[i]==v1[i-1]&&v1[i]!=-1) ans=0;
	for(int i=1;i<v2.size();i++)
		if(v2[i]==v2[i-1]&&v2[i]!=-1) ans=0;

	cout<<ans;
}


#include <bits/stdc++.h>
#define ll long long
#define MOD 998244353
#define CAP 100005
#define inf 10000000000000000LL
#define pii pair<ll,ll>
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define endl '\n'
#define sz(v) v.size()
#define all(v)    v.begin(),v.end()
#define fast  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define int ll
using namespace std;
ll power(ll x,ll y, ll md=MOD){ll res = 1;x%=md;while(y>0){if(y&1)res = (res*x)%md;x = (x*x)%md;y = y>>1;}return res%md;}
ll k;
ll numWays(ll cnt, ll same){
	if(cnt==0) return 1;
	if(cnt==1) {
		if(same) return k-1;
		else return k-2;
	}
	ll ans=0;
	if(same){
		ans=(k-1)*(((k-2)*numWays(cnt-2,0))%MOD);
		ans%=MOD;
		ans+=(k-1)*numWays(cnt-2,1);
	} else {
		ans=(k-2)*(((k-3)*numWays(cnt-2,0))%MOD);
		ans%=MOD;
		ans+=2*(((k-2)*numWays(cnt-2,0))%MOD);
		ans%=MOD;
		ans+=numWays(cnt-2,0);
		ans%=MOD;
		ans+=((k-2)*numWays(cnt-2,1))%MOD;
	}
	return ans%MOD;
}

signed main(){
	fast;
	ll n,ans=1;
	cin>>n>>k;
	std::vector<ll> v1,v2;
	for(int i=0;i<n;i++){
		ll temp;
		cin>>temp;
		if(i%2==0) v1.push_back(temp);
		else v2.push_back(temp);
	}
	std::vector<pair<ll,pair<ll,ll>>> cnt1,cnt2;
	ll c=0;
	ll prev=0;
	for(int i=0;i<v1.size();i++){
		if(v1[i]==-1) c++;
		else {
			if(c) cnt1.push_back(make_pair(c,make_pair(prev,v1[i])));
			prev = v1[i];
			c=0;
		}
	}
	if(c) cnt1.push_back(make_pair(c,make_pair(prev,0)));
	c=0;
	prev=0;
	for(int i=0;i<v2.size();i++){
		if(v2[i]==-1) c++;
		else {
			if(c) cnt2.push_back(make_pair(c,make_pair(prev,v2[i])));
			prev = v2[i];
			c=0;
		}
	}
	if(c) cnt2.push_back(make_pair(c,make_pair(prev,0)));

	for(int i=0;i<cnt1.size();i++){
		ll count = cnt1[i].first;
		ll p = cnt1[i].second.first;
		ll nx = cnt1[i].second.second;
		if(count==0) continue;
		if(p==0&&nx==0){
			ans*=k;
			ans%=MOD;
			ans*=power(k-1,count-1);
		}
		else if(p==0||nx==0) ans*=power(k-1,count);
		else if(p==nx) ans*=numWays(count,1);
		else ans*=numWays(count,0);
		ans%=MOD;
	}
	for(int i=0;i<cnt2.size();i++){
		ll count = cnt2[i].first;
		ll p = cnt2[i].second.first;
		ll nx = cnt2[i].second.second;
		if(count==0) continue;
		if(p==0&&nx==0){
			ans*=k;
			ans%=MOD;
			ans*=power(k-1,count-1);
		}
		else if(p==0||nx==0) ans*=power(k-1,count);
		else if(p==nx) ans*=numWays(count,1);
		else ans*=numWays(count,0);
		ans%=MOD;
	}

	for(int i=1;i<v1.size();i++)
		if(v1[i]==v1[i-1]&&v1[i]!=-1) ans=0;
	for(int i=1;i<v2.size();i++)
		if(v2[i]==v2[i-1]&&v2[i]!=-1) ans=0;

	cout<<ans;
}