#include <bits/stdc++.h>
using namespace std;
const long long N = 1e6 + 100, OO = 1e18, M = 998244353, M2 = 998244353,
                sq = 350, lg = 23;
long long fac[N], inv[N];
long long pw(long long x, long long y) {
  if (y == 0) return 1;
  long long cnt = pw(x, y / 2);
  cnt = (cnt * cnt) % M;
  cnt = (cnt * (y % 2 == 1 ? x : 1)) % M;
  return cnt;
}
void prepro() {
  fac[0] = 1;
  for (long long i = 1; i <= N - lg; i++) fac[i] = (fac[i - 1] * i) % M;
  inv[N - lg] = pw(fac[N - lg], M - 2);
  for (long long i = N - lg - 1; i > -1; i--)
    inv[i] = (inv[i + 1] * (i + 1)) % M;
}
long long c(long long x, long long y) {
  return ((fac[x] * inv[y] % M) * inv[x - y]) % M;
}
int32_t main() {
  prepro();
  long long n;
  cin >> n;
  long long ans = 0;
  for (long long i = 0; i < n; i++) {
    long long t = c(n, i), f = -1 * pw(3, i);
    long long k = pw((1 + f) % M, n) - pw(f, n);
    k = (k + M) % M;
    k = (k * t) % M;
    if (i % 2 == 0)
      ans -= k;
    else
      ans += k;
    ans = (ans + M) % M;
  }
  ans *= 3;
  ans %= M;
  long long cnt = 0;
  for (long long i = 1; i <= n; i++) {
    long long t = c(n, i);
    long long k = pw(3, i), f = pw(3, (n - i) * n);
    k = (k * t) % M;
    k = (k * f) % M;
    if (i % 2 == 1)
      cnt += k;
    else
      cnt -= k;
    cnt = (cnt + M) % M;
  }
  cnt *= 2;
  cnt += ans;
  cnt += M;
  cnt %= M;
  cout << cnt << endl;
  return 0;
}
