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 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

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:

有一个n×n的空白网格图,要求将每个格子染成红色、蓝色或者绿色,并且至少有一行或者一列的颜色相同。两种染色方案不同当且仅当至少有一个格子的染色不同。问不同的染色方案数。 n<=1e6

XJByy一下括号里的内容,3*(3n-i-1)n,其中3表示所有相同的横行的种类数,(3n-i-1)n就是列不能是那种颜色的方案数,3i-3就是至少两个横行颜色不同的方案数,3n*(n-i)就是剩下格子随便填的方案数

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
inline ll read()
{
ll s=; bool f=; char ch=' ';
while(!isdigit(ch)) {f|=(ch=='-'); ch=getchar();}
while(isdigit(ch)) {s=(s<<)+(s<<)+(ch^); ch=getchar();}
return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{
if(x<) {putchar('-'); x=-x;}
if(x<) {putchar(x+''); return;}
write(x/); putchar((x%)+'');
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('\n')
const int N=;
const ll Mod=;
ll n,fac[N],invf[N];
ll Ksm(ll x,ll y)
{
ll ans=1ll;
while(y)
{
if(y&) ans=ans*x%Mod; x=x*x%Mod; y>>=;
}
return ans;
}
inline void Ad(ll &x,ll y)
{
x+=y; x-=(x>=Mod)?Mod:; x+=(x<)?Mod:;
}
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[]=1ll; for(i=;i<=n;i++) fac[i]=fac[i-]*i%Mod;
invf[n]=Ksm(fac[n],Mod-); for(i=n-;i>=;i--) invf[i]=invf[i+]*(i+)%Mod;
ans=Ksm(,n*n); Ad(ans,-Ksm((Ksm(,n)+Mod-)%Mod,n));
ll opt=-;
// cout<<"ans="<<ans<<endl;
for(i=;i<=n;i++)
{
ll Sum;
// cout<<n<<' '<<i<<' '<<C(n,i)<<endl;
Sum=opt*C(n,i)*(((*Ksm((Ksm(,n-i)-),n))%Mod+(Ksm(,i)-)*Ksm(,n*(n-i))%Mod)%Mod)%Mod;
Ad(ans,-Sum);
opt=-opt;
}
Wl(ans%Mod);
return ;
}
/*
Input
6
Output
977299444
*/

codeforces997C的更多相关文章

  1. Codeforces997C Sky Full of Stars 【FMT】【组合数】

    题目大意: 一个$n*n$的格子,每个格子由你填色,有三种允许填色的方法,问有一行或者一列相同的方案数. 题目分析: 标题的FMT是我吓人用的. 一行或一列的问题不好解决,转成它的反面,没有一行和一列 ...

  2. codeforces997C Sky full of stars

    传送门:http://codeforces.com/problemset/problem/997/C [题解] 注意在把$i=0$或$j=0$分开考虑的时候,3上面的指数应该是$n(n-j)+j$ 至 ...

  3. HDU5977 Garden of Eden 【FMT】【树形DP】

    题目大意:求有所有颜色的路径数. 题目分析:参考codeforces997C,先利用基的FMT的性质在$O(2^k)$做FMT,再利用只还原一位的特点在$O(2^k)$还原,不知道为什么网上都要点分治 ...

  4. Noip前的大抱佛脚----赛前任务

    赛前任务 tags:任务清单 前言 现在xzy太弱了,而且他最近越来越弱了,天天被爆踩,天天被爆踩 题单不会在作业部落发布,所以可(yi)能(ding)会不及时更新 省选前的练习莫名其妙地成为了Noi ...

随机推荐

  1. Job和Service

    Job及CronJob: ---apiVersion: batch/v1kind: Jobmetadata:  name: job-demospec:  template:    metadata:  ...

  2. 植物大战僵尸:查找植物叠加种植CALL

    实验目标:我们都知道植物大战僵尸游戏中植物是不可以叠加种植的,也就是一个格子只能种植一个植物,今天我们将实现一个格子里种植无限多的植物. 我们首先需要找到植物的种植CALL,然后在逐步测试观察功能之间 ...

  3. java怎么解除文件占用(Dom4j操作完xml后怎么关流)

    一.背景 项目中要解析xml,由于Dom4j的诸多优点,我就用Dom4j解析xml,代码如下: public void readXML() { SAXReader reader = new SAXRe ...

  4. LeetCode:1179.重新格式化部门表

    题目链接:https://leetcode-cn.com/problems/reformat-department-table/ 题目 部门表 Department: +--------------- ...

  5. nodejs request module里的json参数的一个坑

    今天工作的时候遇到一个坑,在客户端用nodejs给服务器发送HTTP请求,服务器老是报错:In the context of Data Services an unknown internal ser ...

  6. Delphi 重载方法与重定义方法

  7. Linux基础知识之文件的权限(一)

    Linux基础知识之文件权限(一) Linux优点之一就是它拥有多用户多任务的环境,在提供文件共享的同时也能保证用户文件的安全性.所以,设置文件的权限管理变得尤为重要. 权限讲解 [der@Der ~ ...

  8. myBatis的坑 01 %的坑 框架内置的小BUG

    <select id="queryUserLikeUserName" resultType="cn.itcast.pojo.User"> selec ...

  9. JDBC连接数据库报错:Loading class `com.mysql.jdbc.Driver'. This is deprecated.

    使用JDBC连接数据库时出现报错, 报错内容:Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver cla ...

  10. [六省联考2017]分手是祝愿——期望DP

    原题戳这里 首先可以确定的是最优策略一定是从大到小开始,遇到亮的就关掉,因此我们可以\(O(nlogn)\)的预处理出初始局面需要的最小操作次数\(tot\). 然后容(hen)易(nan)发现即使加 ...