#include
using namespace std;
const int N=1e6+10;
typedef long long ll;
const ll mod=998244353;
ll fac[N],inv_fac[N];
ll qpow(ll a,ll b){
ll res=1,tmp=a;
for(;b;b>>=1,(tmp*=tmp)%=mod)
if(b&1) (res*=tmp)%=mod;
return res;
}
void init(){
fac[0]=1;
for(int i=1;i1]*i%mod;
inv_fac[N-1]=qpow(fac[N-1],mod-2);
for(int i=N-2;i>=0;i--) inv_fac[i]=inv_fac[i+1]*(i+1)%mod;
}
ll C(int n,int m){
return (fac[n]*inv_fac[n-m]%mod)*inv_fac[m]%mod;
}
ll add(ll a,ll b) {
a=(a+b)%mod;
if(a<0) return a+mod;
return a;
}
int main(){
int n;scanf("%d",&n);init();ll ans=0;
if(n==1){printf("3\n");return 0;}
int s=1;
ll pow3=1;
ll p3n = qpow(3, n);
for(ll i=1;i<=n;i++,s*=-1){
ll tmp=s*C(n,i)*qpow(3,i)%mod*qpow(3,n*(n-i)%(mod-1))%mod;
ans=add(ans,tmp);
}
ans=add(ans,ans);
s=(n&1)?1:-1;
for(ll i=0;i1){
ll tmp=add(qpow(qpow(3,i)-1,n),-qpow(3,1ll*n*i%(mod-1)));
(tmp*=3*s*C(n,i))%=mod;
ans=add(ans,tmp);
}
printf("%I64d\n",ans);
return 0;
}