#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  long long x = 0;
  char ch = getchar();
  bool f = 0;
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-') f = 1;
  for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0';
  return f ? -x : x;
}
void write(long long x) {
  if (x < 0) putchar('-'), x = -x;
  if (x >= 10) write(x / 10);
  putchar(x % 10 + '0');
}
void writeln(long long x) {
  write(x);
  puts("");
}
const int N = 1000005, mo = 998244353;
int n;
long long ans, fac[N], inv[N];
long long ksm(long long x, long long p) {
  long long res = 1;
  for (; p; p >>= 1, x = x * x % mo)
    if (p & 1) res = res * x % mo;
  return res;
}
long long C(int n, int m) { return fac[n] * inv[m] % mo * inv[n - m] % mo; }
int main() {
  n = read();
  fac[0] = 1;
  for (int i = 1; i <= n; i++) fac[i] = fac[i - 1] * 1ll * i % mo;
  inv[n] = ksm(fac[n], mo - 2);
  for (int i = n - 1; i >= 0; i--) inv[i] = 1ll * (i + 1) * inv[i + 1] % mo;
  ans = (ksm(3ll, 1ll * n * n) - ksm(ksm(3, n) - 3 + mo, 1ll * n) + mo) % mo;
  long long sum = 0;
  for (int i = 1, op = -1; i <= n; i++) {
    long long t =
        op * C(n, i) *
        ((3ll * ksm((ksm(3ll, (long long)n - i) - 1 + mo) % mo, 1ll * n) % mo +
          (ksm(3ll, 1ll * i) - 3 + mo) % mo * ksm(3ll, 1ll * n * (n - i)) %
              mo) %
         mo) %
        mo;
    sum = (sum + t + mo) % mo;
    op *= -1;
  }
  write((ans - sum + mo) % mo);
  return 0;
}
