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. Kafka实际使用过程中遇到的一些问题及解决方法

    Kafka实际使用过程中遇到的一些问题及解决方法: 1.关于Kafka的分区: 开始使用Kafka的时候,没有分区的概念,以为类似于传统的MQ中间件一样,就直接从程序中获取Kafka中的数据. 后来程 ...

  2. MyBatis 示例-主键回填

    测试类:com.yjw.demo.PrimaryKeyTest 自增长列 数据库表的主键为自增长列,在写业务代码的时候,经常需要在表中新增一条数据后,能获得这条数据的主键 ID,MyBatis 提供了 ...

  3. Git 集成 Araxis Merge 作为比较和合并GUI工具的配置 参考自https://www.kancloud.cn/leviio/git/369125

    Git 集成 Araxis Merge Win10下修改git全部配置文件方法Git 集成 Araxis Merge 作为比较和合并GUI工具的配置 那global对应的 ,gitconfig文件在哪 ...

  4. 出现 HTTP 错误 500.19 错误代码 0x800700b7

    这个内容出现主要问题是在IIS上,我们一般程序开发 iis中默认的路径只是http://localhost/,相当于环境变量中已定义好了,如果自己创建的项目直接将路径定义到这,就会替换图二中的路径,然 ...

  5. Docker本地镜像发布到阿里云和从阿里云拉取镜像

    登录阿里云官网,找到容器镜像服务 进入镜像仓库,创建仓库 输入信息 选择本地仓库 这里我要将这个镜像提交到仓库 回到仓库列表,点击管理 docker login --username=cn丶moti ...

  6. 许愿墙JQ

    <!doctype html> <html> <head>     <meta charset="utf-8">     <t ...

  7. Flutter 36: 图解自定义 View 之 Canvas (三)

    小菜继续学习 Canvas 的相关方法: drawVertices 绘制顶点 小菜上次没有整理 drawVertices 的绘制方法,这次补上:Vertice 即顶点,通过绘制多个顶点,在进行连线,多 ...

  8. 如何使用Chrome开发者工具找到Marketing Cloud某个contact的guid

    我们使用nodejs对contact进行修改时,需要指定待修改contact实例的guid. 这个guid属于technical属性,在Marketing Cloud UI上默认情况下不可见.如何找到 ...

  9. 第三章 Lambda表达式

    第三章 Lambda表达式 3.1 函数式编程思想概述 在数学中,函数就是有输入量.输出量的一套计算方案,也就是“拿什么东西做什么事情”.相对而言,面向对象过分强调“必须通过对象的形式来做事情”,而函 ...

  10. 学习手写vue,理解原理

    class Compiler{ constructor(el,vm){ // 判断el属性 是不是 一个元素, 如果不是就获取 this.el = this.isElementNode(el)?el: ...