#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("fast-math")
#pragma GCC target("sse,sse2,sse3,ssse3,popcnt,abm,mmx,tune=native")
using namespace std;
const long long MOD = 998244353;
const long long MAXN = 1e6 + 7;
long long f[MAXN];
inline long long mod(long long n) {
  n %= MOD;
  if (n < 0)
    return n + MOD;
  else
    return n;
}
void precalc() {
  f[0] = 1;
  for (long long i = 1; i < MAXN; ++i) {
    f[i] = mod(f[i - 1] * i);
  }
}
long long fp(long long a, long long b) {
  if (b == 0) return 1;
  long long t = fp(a, b / 2);
  if (b % 2 == 0)
    return mod(t * t);
  else
    return mod(mod(t * t) * a);
}
long long mdiv(long long a, long long b) { return mod(a * fp(b, MOD - 2)); }
long long c(long long n, long long k) {
  return mdiv(f[n], mod(f[k] * f[n - k]));
}
long long get(long long n, long long t) { return mod(fp(t + 1, n) - fp(t, n)); }
double gett() { return (double)clock() / CLOCKS_PER_SEC; }
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  precalc();
  long long n;
  cin >> n;
  long long ans = 0;
  for (long long i = 0; i < n; ++i) {
    long long a_ = c(n, i);
    long long b_;
    if (i & 1)
      b_ = 1;
    else
      b_ = -1;
    long long c_ = get(n, -fp(3, i));
    ans = mod(ans + mod(mod(a_ * b_) * c_));
  }
  ans = mod(ans * 3);
  long long add = 0;
  for (long long i = 1; i <= n; ++i) {
    long long a_ = c(n, i);
    long long b_;
    if (i & 1)
      b_ = 1;
    else
      b_ = -1;
    long long c_ = fp(3, i);
    long long d_ = fp(3, n * (n - i));
    add = mod(add + mod(mod(mod(a_ * b_) * c_) * d_));
  }
  add = mod(add * 2);
  cout << mod(ans + add) << '\n';
  return 0;
}
