#include <bits/stdc++.h>
using namespace std;
int n;
long long mod = 998244353;
long long fact[1000005], inver[1000005];
inline long long mulMod(long long a, long long b) { return a * b % mod; }
inline long long addMod(long long a, long long b) { return (a + b) % mod; }
inline long long subMod(long long a, long long b) {
  return (a + mod - b) % mod;
}
long long powMod(long long a, long long x) {
  if (x == 0) return 1;
  if (x & 1) return mulMod(powMod(a, x - 1), a);
  long long temp = powMod(a, x >> 1);
  return mulMod(temp, temp);
}
inline long long c(int k, int x) {
  return mulMod(fact[x], mulMod(inver[k], inver[x - k]));
}
void setup() {
  cin >> n;
  fact[0] = 1;
  for (int i = 1; i <= n; ++i) fact[i] = mulMod(fact[i - 1], 1LL * i);
  inver[n] = powMod(fact[n], mod - 2);
  for (int i = n - 1; i >= 0; --i) inver[i] = mulMod(inver[i + 1], i + 1);
}
void process() {
  long long res = 0;
  long long temp = 1;
  for (int k = 1; k <= n; ++k) {
    long long t = mulMod(2, c(k, n));
    t = mulMod(t, powMod(3, k));
    t = mulMod(t, powMod(3, 1LL * n * n - 1LL * k * n));
    if (temp == -1) t = mod - t;
    res = addMod(res, t);
    temp *= -1;
  }
  temp = 1;
  for (int i = 1; i <= n; ++i) {
    long long t = powMod(3, n - i);
    t--;
    if (t < 0) t = mod - t;
    t = powMod(t, n);
    t = subMod(t, powMod(powMod(3, n - i), n));
    t = mulMod(3, t);
    t = mulMod(t, c(i, n));
    if (temp == -1) t = mod - t;
    res = addMod(res, t);
    temp *= -1;
  }
  cout << res << endl;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  setup();
  process();
  return 0;
}
