codeforces997C
Sky Full of Stars
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 n2n2 days, so calendars, represented as square matrix nn by nn 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⋅n3n⋅n different colorings. How much of them are lucky? Since this number can be quite large, print it modulo 998244353998244353.
Input
The first and only line of input contains a single integer nn (1≤n≤10000001≤n≤1000000) — the number of rows and columns in the calendar.
Output
Print one number — number of lucky colorings of the calendar modulo 998244353998244353
Examples
1
3
2
63
3
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:
有一个n×n的空白网格图,要求将每个格子染成红色、蓝色或者绿色,并且至少有一行或者一列的颜色相同。两种染色方案不同当且仅当至少有一个格子的染色不同。问不同的染色方案数。 n<=1e6
XJByy一下括号里的内容,3*(3n-i-1)n,其中3表示所有相同的横行的种类数,(3n-i-1)n,就是列不能是那种颜色的方案数,3i-3就是至少两个横行颜色不同的方案数,3n*(n-i)就是剩下格子随便填的方案数
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <bits/stdc++.h> using namespace std; typedef long long ll; inline ll read() { ll s=0; bool f=0; char ch=' '; while(!isdigit(ch)) {f|=(ch=='-'); ch=getchar();} while(isdigit(ch)) {s=(s<<3)+(s<<1)+(ch^48); ch=getchar();} return (f)?(-s):(s); } #define R(x) x=read() inline void write(ll x) { if(x<0) {putchar('-'); x=-x;} if(x<10) {putchar(x+'0'); return;} write(x/10); putchar((x%10)+'0'); } #define W(x) write(x),putchar(' ') #define Wl(x) write(x),putchar('\n') const int N=1000005; const ll Mod=998244353; ll n,fac[N],invf[N]; ll Ksm(ll x,ll y) { ll ans=1ll; while(y) { if(y&1) ans=ans*x%Mod; x=x*x%Mod; y>>=1; } return ans; } inline void Ad(ll &x,ll y) { x+=y; x-=(x>=Mod)?Mod:0; x+=(x<0)?Mod:0; } inline ll C(ll n,ll m) { return fac[n]*invf[m]%Mod*invf[n-m]%Mod; } int main() { // freopen("data.in","r",stdin); ll i,ans; R(n); fac[0]=1ll; for(i=1;i<=n;i++) fac[i]=fac[i-1]*i%Mod; invf[n]=Ksm(fac[n],Mod-2); for(i=n-1;i>=0;i--) invf[i]=invf[i+1]*(i+1)%Mod; ans=Ksm(3,n*n); Ad(ans,-Ksm((Ksm(3,n)+Mod-3)%Mod,n)); ll opt=-1; // cout<<"ans="<<ans<<endl; for(i=1;i<=n;i++) { ll Sum; // cout<<n<<' '<<i<<' '<<C(n,i)<<endl; Sum=opt*C(n,i)*(((3*Ksm((Ksm(3,n-i)-1),n))%Mod+(Ksm(3,i)-3)*Ksm(3,n*(n-i))%Mod)%Mod)%Mod; Ad(ans,-Sum); opt=-opt; } Wl(ans%Mod); return 0; } /* Input 6 Output 977299444 */