Sky Full of Stars CodeForces - 997C

On one of the planets of Solar system, in Atmosphere University, many students are fans of bingo game.

It is well known that one month on this planet consists of n2

days, so calendars, represented as square matrix n by n

are extremely popular.

Weather conditions are even more unusual. Due to the unique composition of the atmosphere, when interacting with sunlight, every day sky takes one of three colors: blue, green or red.

To play the bingo, you need to observe the sky for one month — after each day, its cell is painted with the color of the sky in that day, that is, blue, green or red.

At the end of the month, students examine the calendar. If at least one row or column contains only cells of one color, that month is called lucky.

Let's call two colorings of calendar different, if at least one cell has different colors in them. It is easy to see that there are 3n⋅n

different colorings. How much of them are lucky? Since this number can be quite large, print it modulo 998244353

.

Input

The first and only line of input contains a single integer n

(1≤n≤1000000

) — the number of rows and columns in the calendar.

Output

Print one number — number of lucky colorings of the calendar modulo 998244353

 

Examples

Input

1

Output

3

Input

2

Output

63

Input

3

Output

9933

Note

In the first sample any coloring is lucky, since the only column contains cells of only one color.

In the second sample, there are a lot of lucky colorings, in particular, the following colorings are lucky:

While these colorings are not lucky:

 

容斥原理:

知道可以求     \large \left | A_{1}\bigcup A_{2}\bigcup ...\bigcup A_{n}\bigcup B_{1}\bigcup B_{2}\bigcup ...\bigcup B_{n} \right |, 数学式子不会列

 

\large \sum_{i,j,i+j>0}^{n}\binom{n}{i}\binom{n}{j} (-1)^{(i+j+1)}f(i,j)

还是将行和列分开来看,看看选几行 几列, 然后讨论  \large f(i,j) 的取值

1. 只取行或者列

2.行和列都取

 

然后化简,将两个变量换成一个变量

 

#include<bits/stdc++.h>
using namespace std;

typedef long long LL;
#define rep(i,a,b) for(int i=a;i<b;++i)

const LL mod=998244353;

LL pow_mod(LL base,LL n){

	if(base==0)return 0;

	LL res=1;
	while(n){
		if(n&1){
			res=res*base%mod;
		}
		base=base*base%mod;
		n>>=1;
	}
	return res;
}


int main(){

	//printf("%lld\n",mod*mod%mod);

	LL n;
	scanf("%lld",&n);

	LL res=0,pre=1,tag;
	rep(i,1,n+1){
		if(i&1)tag=1;
		else tag=-1;
		pre=pre*(n-(i-1))%mod*pow_mod(i,mod-2)%mod;

		//printf("***i:%d pre:%lld\n",i,pre);

		LL tmp=pre*pow_mod(3,n*(n-i)+i)%mod;
		res=(res+tag*tmp+mod)%mod;
	}

	res=res*2%mod;

	LL res2=0;pre=1;
	int f;
	rep(i,0,n){
		if(i&1)f=-1;
		else f=1;

		LL tmp=((-1)*pow_mod(3,i)%mod+mod)%mod;

		LL t1=pow_mod(tmp+1,n),t2=pow_mod(tmp,n);

		//printf("**tmp:%lld t1:%lld t2:%lld\n",tmp,t1,t2);

		t1=(t1+mod-t2)%mod;

		res2=(res2+pre*f*t1%mod+mod)%mod;

		pre=pre*(n-i)%mod*pow_mod(i+1,mod-2)%mod;
	}

	res2=res2*3%mod;

	//printf("***res:%lld res2:%lld\n",res,res2);

	res=(res-res2+mod)%mod;
	printf("%lld\n",res);
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值