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. idea 编辑器Git暂存区的使用

    平时在开发时候 一般线上环境和线下环境区别会很大,所以一下线下的自己测试环境的代码没有如果提交会影响线上环境,所以一般都会使用git的一个暂存区作为临时存放不需要提交的代码,这样每次提交代码都可以在不 ...

  2. mybatis的BLOB存储与读取

    http://blog.csdn.net/luyinchangdejiqing/article/details/45096689 简单介绍一下背景环境,web开发避免不了照片附件之类的东东,原先是存到 ...

  3. boost random library的使用

      生成满足一定分布的随机数,是统计模拟.系统仿真等应用中最基本的要求.matlab中提供了函数可以生成各种常见分布的随机数,c++使用boost random库也可以很容易实现. 一.例子 boos ...

  4. 查找最大和次大元素(JAVA版)(分治法)

    问题描述:对于给定的含有n个元素的无序序列,求这个序列中最大和次大的两个不同元素. 问题求解分析(分治法):先给出无序序列数组a[low...high].第一种情况为当数组中只有一个元素时,此时只存在 ...

  5. HTML中由于DIV(块元素)浮动,导致的父元素高度塌陷问题的解决方案

    费话不多说,直接上问题: 1.开始时,页面只有两个DIV的嵌套(见图) 运行结果是: 现在看运行的是正常的,但是当我设置让 class="box2" 的DIV浮动时 运行结果是这样 ...

  6. css四种定位

    概述:元素定位属性主要包括的模式和边偏移两部分,也就是说以后定位要和边偏移量搭配使用.边偏移加定位定位模式才能构成一个完整的定义方式. 偏移量说明: 边偏移属性 描述 top: 顶端偏移,定义元素相对 ...

  7. Google自动打印

    浏览器打印功能,有很多小伙伴可能不太清楚,这里我们可以学习一下. 情景:开发一个需要打印小票的项目.(在订单页里,给客户添加一个打印的操作) 1.假设打印机已经连接好了 2.我们这一节用的浏览器是Go ...

  8. mybatis整合spring下的的各种配置文件

    1.applicationContext.xml <?xml version="1.0" encoding="UTF-8"?> <beans ...

  9. ASR测试方法---字错率(WER)、句错率(SER)统计

    一.基础概念 1.1.语音识别(ASR) 语音识别(speech recognition)技术,也被称为自动语音识别(英语:Automatic Speech Recognition, ASR), 狭隘 ...

  10. NORDIC 错误文件

    ble_err.h sdk_errors.h nrf_error.h app_error.h